From 8547844fa79a4b20fedef6d2fbe2df508cb0671d Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Thu, 21 Mar 2024 11:18:00 +0800 Subject: [PATCH] 1 --- README.md | 4 + server/imserver_new/player/player.go | 27 +++-- server/imserver_new/player/playermgr.go | 130 ++++++++++++++++++++--- server/imserver_new/proto/cs_proto.proto | 2 + 4 files changed, 140 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 845a5bb3..64e8e6f1 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ node ../../tools/pbtools/app.js main包里所有的函数、字段都小写开头 +# imserver + +SMLogin + # 参考 https://gorm.io/zh_CN/docs/ diff --git a/server/imserver_new/player/player.go b/server/imserver_new/player/player.go index 67be5d78..ceef8efe 100644 --- a/server/imserver_new/player/player.go +++ b/server/imserver_new/player/player.go @@ -13,14 +13,20 @@ import ( type player struct { cs.MsgHandlerImpl - socket f5.WspCliConn - accountId string - sessionId string - chatChannel int + socket f5.WspCliConn + accountId string + sessionId string + chatChannel int privateTargetAccountId string // 私聊对象 accountId - worldChannelLastId uint64 // 世界聊天 last id - guildChannelLastId uint64 // 公会聊天 last id - privateChatLastId uint64 // 私聊 last id + worldChannelLastId uint64 // 世界聊天 last id + guildChannelLastId uint64 // 公会聊天 last id + privateChatLastId uint64 // 私聊 last id +} + +func (this *player) init(req *pendingLoginRequest, rspObj *common.LoginRsp){ + this.socket = req.hdr.GetSocket() + this.accountId = req.msg.GetAccountId() + this.sessionId = req.msg.GetSessionId() } func (this *player) GetAccountId() string { @@ -87,7 +93,7 @@ func (this *player) SendMsg(rspMsg proto.Message) { GetWspListener().SendProxyMsg(this.socket.Conn, this.socket.SocketHandle, rspMsg) } -func (this *player) ReBind(socket f5.WspCliConn) { +func (this *player) reBind(socket f5.WspCliConn) { if this.socket.IsValid() { GetPlayerMgr().UnBindSocket(this.socket) } @@ -999,3 +1005,8 @@ func (this *player) IncrPrivateChatLastId() uint64 { return p.privateChatLastId } */ + +func newPlayer() *player { + hum := new(player) + return hum +} diff --git a/server/imserver_new/player/playermgr.go b/server/imserver_new/player/playermgr.go index aaf33c03..2abdbe38 100644 --- a/server/imserver_new/player/playermgr.go +++ b/server/imserver_new/player/playermgr.go @@ -14,10 +14,19 @@ import ( . "main/global" ) +type pendingLoginRequest struct { + hdr *f5.MsgHdr + msg *cs.CMLogin + addTick int64 + reqId int64 +} + type playerMgr struct { cs.MsgHandlerImpl accountIdHash map[string]*player socketHash map[f5.WspCliConn]*player + pendingLoginHash map[string]*pendingLoginRequest + currReqId int64 } func (this *playerMgr) init() { @@ -57,6 +66,51 @@ func (this *playerMgr) unInit() { } func (this *playerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) { + { + oldHum := this.internalGetPlayerBySocket(hdr.GetSocket()) + if oldHum != nil { + rspMsg := cs.SMLogin{} + rspMsg.Errcode = proto.Int32(0) + rspMsg.Errmsg = proto.String("") + rspMsg.ServerInfo = proto.String(mt.Table.IMCluster.GetServerInfo()) + oldHum.reBind(hdr.GetSocket()) + oldHum.SendMsg(&rspMsg) + return + } + } + { + oldHum := this.internalGetPlayerByAccountId(msg.GetAccountId()) + if oldHum != nil { + rspMsg := cs.SMLogin{} + rspMsg.Errcode = proto.Int32(0) + rspMsg.Errmsg = proto.String("") + rspMsg.ServerInfo = proto.String(mt.Table.IMCluster.GetServerInfo()) + oldHum.reBind(hdr.GetSocket()) + oldHum.SendMsg(&rspMsg) + return + } + } + reqId := this.genSeqId() + pendingReq := this.getPendingRequest(msg.GetAccountId()) + if pendingReq == nil { + this.pendingLoginHash[msg.GetAccountId()] = &pendingLoginRequest{ + hdr: hdr, + msg: msg, + addTick: q5.GetTickCount(), + reqId: reqId, + } + } else { + if pendingReq.msg.GetAccountId() == msg.GetAccountId() && + pendingReq.msg.GetSessionId() == msg.GetSessionId() { + pendingReq.hdr = hdr + pendingReq.msg = msg + return + } else { + pendingReq.hdr = hdr + pendingReq.msg = msg + pendingReq.reqId = reqId + } + } params := map[string]string{ "c": "User", "a": "detailInfo", @@ -69,30 +123,46 @@ func (this *playerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) { url, params, func(rsp f5.HttpCliResponse) { - this.apiAuthCb(hdr, msg, rsp) + this.apiAuthCb(hdr, msg, rsp, reqId) }) } -func (this *playerMgr) apiAuthCb(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.HttpCliResponse) { - resObj := common.LoginRsp{} - err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj) +func (this *playerMgr) apiAuthCb(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.HttpCliResponse, reqId int64) { + pendingReq := this.getPendingRequest(msg.GetAccountId()) + if pendingReq == nil || pendingReq.reqId != reqId { + return + } + delete(this.pendingLoginHash, msg.GetAccountId()) + + rspMsg := cs.SMLogin{} + if rsp.GetErr() != nil { + rspMsg.Errcode = proto.Int32(2) + rspMsg.Errmsg = proto.String("server internal error") + GetWspListener().SendProxyMsg(pendingReq.hdr.Conn, pendingReq.hdr.SocketHandle, &rspMsg) + f5.GetSysLog().Info("Api服务器JSON 解析错误1\n") + return + } + rspObj := common.LoginRsp{} + err := json.Unmarshal([]byte(rsp.GetRawData()), &rspObj) if err != nil { + rspMsg.Errcode = proto.Int32(2) + rspMsg.Errmsg = proto.String("server internal error") + GetWspListener().SendProxyMsg(pendingReq.hdr.Conn, pendingReq.hdr.SocketHandle, &rspMsg) f5.GetSysLog().Info("Api服务器JSON 解析错误:%s\n", err) return } - if resObj.Errcode != 0 { - f5.GetSysLog().Error("Api服务器errcode:%d", resObj.Errcode) + if rspObj.Errcode != 0 { + rspMsg.Errcode = proto.Int32(1) + rspMsg.Errmsg = proto.String("invalid session_id") + GetWspListener().SendProxyMsg(pendingReq.hdr.Conn, pendingReq.hdr.SocketHandle, &rspMsg) + f5.GetSysLog().Error("Api服务器errcode:%d", rspObj.Errcode) return } - accountId := msg.GetAccountId() - player := player{ - socket: hdr.GetSocket(), - accountId: accountId, - sessionId: msg.GetSessionId(), - } + hum := newPlayer() + hum.init(pendingReq, &rspObj) // Add to online user - this.addPlayer(&player) - this.addSocketHash(hdr.GetSocket(), &player) + //this.addPlayer(&player) + //this.addSocketHash(hdr.GetSocket(), &player) /* // Add player profile playerProfile := &PlayerProfile{ @@ -147,6 +217,20 @@ func (this *playerMgr) reportServerState(masterIp string, masterPort int32) { }) } +func (this *playerMgr) getPendingRequest(accountId string) *pendingLoginRequest { + req, ok := this.pendingLoginHash[accountId] + if ok { + return req + } + return nil +} + +func (this *playerMgr) genSeqId() int64 { + this.currReqId++ + reqId := this.currReqId + return reqId +} + func (this *playerMgr) GetPlayers() map[string]*player { return this.accountIdHash } @@ -163,6 +247,22 @@ func (this *playerMgr) GetPlayerByAccountId(accountId string) *player { return nil } +func (this *playerMgr) internalGetPlayerByAccountId(accountId string) *player { + player, ok := this.accountIdHash[accountId] + if ok { + return player + } + return nil +} + +func (this *playerMgr) internalGetPlayerBySocket(socket f5.WspCliConn) *player { + player, ok := this.socketHash[socket] + if ok { + return player + } + return nil +} + func (this *playerMgr) getPlayerBySocket(socket f5.WspCliConn) *player { player, ok := this.socketHash[socket] if ok { @@ -198,7 +298,7 @@ func (this *playerMgr) CMReconnect(hdr *f5.MsgHdr, msg *cs.CMReconnect) { GetWspListener().SendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg) return } - hum.ReBind(hdr.GetSocket()) + hum.reBind(hdr.GetSocket()) GetWspListener().SendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg) } diff --git a/server/imserver_new/proto/cs_proto.proto b/server/imserver_new/proto/cs_proto.proto index 67d1862b..da45b1ec 100644 --- a/server/imserver_new/proto/cs_proto.proto +++ b/server/imserver_new/proto/cs_proto.proto @@ -167,6 +167,8 @@ message SMLogin { optional string server_info = 1; //服务器信息(重连时使用) optional string account_id = 2; + optional int32 errcode = 3; //错误码 0:成功 1:重连失败 + optional string errmsg = 4; //错误描述 } //断线重连