|
|
@@ -1,6 +1,10 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "git.wanbits.io/joe/franklin/comp"
|
|
|
"git.wanbits.io/joe/franklin/mod"
|
|
|
pb "git.wanbits.io/joe/franklin/protos"
|
|
|
"git.wanbits.io/joe/kettle/log"
|
|
|
@@ -8,6 +12,7 @@ import (
|
|
|
"github.com/go-redis/redis"
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
"go.uber.org/zap"
|
|
|
+ "google.golang.org/grpc"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
@@ -32,7 +37,7 @@ func dispatch(ses nnet.ISession, pkt *CSPacket) {
|
|
|
if !ok {
|
|
|
// route to lobbies
|
|
|
if pkt.MsgId <= 5000 {
|
|
|
- PostLobby(pkt.UserId, pkt)
|
|
|
+ ForwardLobby(pkt)
|
|
|
return
|
|
|
}
|
|
|
logicId, err:= mod.LoadUserLogic(pkt.UserId)
|
|
|
@@ -42,7 +47,7 @@ func dispatch(ses nnet.ISession, pkt *CSPacket) {
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
- PostLogic(logicId, pkt)
|
|
|
+ ForwardLogic(logicId, pkt)
|
|
|
return
|
|
|
}
|
|
|
handler(ses, pkt)
|
|
|
@@ -57,7 +62,7 @@ func on_C_Heartbeat(ses nnet.ISession, p *CSPacket) {
|
|
|
|
|
|
func on_C_Login(ses nnet.ISession, p *CSPacket) {
|
|
|
prm := &pb.C_Login{}
|
|
|
- ses.SetId(p.UserId)
|
|
|
+ ses.SetId(p.UserId) // IMPORTANT
|
|
|
err := proto.Unmarshal(p.Req, prm)
|
|
|
if err != nil {
|
|
|
response_c_login(ses, pb.ErrCode_BAD_FORMAT, nil)
|
|
|
@@ -102,7 +107,6 @@ func response_c_login(ses nnet.ISession, errCode pb.ErrCode, res *pb.S_Login) {
|
|
|
reason = pb.NetCloseReason_KICK
|
|
|
return
|
|
|
}
|
|
|
- // check user status
|
|
|
// check user kicked off
|
|
|
if mod.LoadKick(ses.Id()) {
|
|
|
ec = pb.ErrCode_FORBIDDEN
|
|
|
@@ -110,18 +114,18 @@ func response_c_login(ses nnet.ISession, errCode pb.ErrCode, res *pb.S_Login) {
|
|
|
return
|
|
|
}
|
|
|
//TODO: check if have logged in any agents already
|
|
|
-
|
|
|
+ doCheckUserLoginAlready(ses.Id())
|
|
|
// check reconnect state
|
|
|
logicId, err := mod.LoadOffline(ses.Id())
|
|
|
if err == nil {
|
|
|
// route to logic
|
|
|
- PostLogic(logicId, ses.Id(), &pb.Q_Online{UserId: ses.Id()})
|
|
|
+ SendToLogic(logicId, ses.Id(), &pb.Q_Online{UserId: ses.Id()})
|
|
|
} else {
|
|
|
if err != redis.Nil {
|
|
|
log.Error("", zap.Error(err))
|
|
|
}
|
|
|
// route to a lobby
|
|
|
- PostLobby(ses.Id(), &pb.Q_Online{UserId: ses.Id()})
|
|
|
+ SendToLobby(ses.Id(), &pb.Q_Online{UserId: ses.Id()})
|
|
|
}
|
|
|
// update user location
|
|
|
err = mod.SaveUserLogin(ses.Id(), g_conf.Id)
|
|
|
@@ -162,3 +166,57 @@ func on_C_LeaveRoom(ses nnet.ISession, p *CSPacket) {
|
|
|
func on_C_LeaveTable(ses nnet.ISession, p *CSPacket) {
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+// check if specified user had logged in any agents, if its, request older login to logout
|
|
|
+// and keep this login process going on
|
|
|
+func doCheckUserLoginAlready(userId uint64) {
|
|
|
+ agentId, err := mod.LoadUserAgent(userId)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if agentId == g_conf.Id {
|
|
|
+ // logged in self
|
|
|
+ ses, err := g_server.GetSession(userId)
|
|
|
+ if err != nil {
|
|
|
+ log.Error("logic error", zap.Uint64("userId", userId), zap.Uint64("agentId", agentId))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ KillUserSession(ses, userId, pb.NetCloseReason_DUP)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // agentId != g_conf.Id
|
|
|
+ key := fmt.Sprintf("%s/%v", comp.PathAgents(g_conf), g_conf.Id)
|
|
|
+ resp, err := comp.GEtcdc.Get(key)
|
|
|
+ if err != nil {
|
|
|
+ log.Error("", zap.Error(err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(resp.Kvs) <= 0 {
|
|
|
+ log.Error("")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ agentConf := &pb.AppConfConf{}
|
|
|
+ err = json.Unmarshal(resp.Kvs[0].Value, agentConf)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // grpc call
|
|
|
+ conn, err := grpc.Dial(agentConf.RpcAddr, grpc.WithInsecure(), grpc.WithBlock())
|
|
|
+ if err != nil {
|
|
|
+ log.Error("", zap.Error(err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer conn.Close()
|
|
|
+ client := pb.NewRpcAgentClient(conn)
|
|
|
+ ctx, cancel := context.WithTimeout(context.Background(), 3 * time.Second)
|
|
|
+ defer cancel()
|
|
|
+
|
|
|
+ _, err = client.Kick(ctx, &pb.KickUser{
|
|
|
+ UserId: userId,
|
|
|
+ Reason: int32(pb.NetCloseReason_DUP),
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ log.Error("", zap.Error(err))
|
|
|
+ }
|
|
|
+}
|