1
This commit is contained in:
parent
1a37d9670a
commit
8547844fa7
@ -23,6 +23,10 @@ node ../../tools/pbtools/app.js
|
||||
|
||||
main包里所有的函数、字段都小写开头
|
||||
|
||||
# imserver
|
||||
|
||||
SMLogin
|
||||
|
||||
# 参考
|
||||
|
||||
https://gorm.io/zh_CN/docs/
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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; //错误描述
|
||||
}
|
||||
|
||||
//断线重连
|
||||
|
Loading…
x
Reference in New Issue
Block a user