1
This commit is contained in:
parent
1a37d9670a
commit
8547844fa7
@ -23,6 +23,10 @@ node ../../tools/pbtools/app.js
|
|||||||
|
|
||||||
main包里所有的函数、字段都小写开头
|
main包里所有的函数、字段都小写开头
|
||||||
|
|
||||||
|
# imserver
|
||||||
|
|
||||||
|
SMLogin
|
||||||
|
|
||||||
# 参考
|
# 参考
|
||||||
|
|
||||||
https://gorm.io/zh_CN/docs/
|
https://gorm.io/zh_CN/docs/
|
||||||
|
@ -13,14 +13,20 @@ import (
|
|||||||
|
|
||||||
type player struct {
|
type player struct {
|
||||||
cs.MsgHandlerImpl
|
cs.MsgHandlerImpl
|
||||||
socket f5.WspCliConn
|
socket f5.WspCliConn
|
||||||
accountId string
|
accountId string
|
||||||
sessionId string
|
sessionId string
|
||||||
chatChannel int
|
chatChannel int
|
||||||
privateTargetAccountId string // 私聊对象 accountId
|
privateTargetAccountId string // 私聊对象 accountId
|
||||||
worldChannelLastId uint64 // 世界聊天 last id
|
worldChannelLastId uint64 // 世界聊天 last id
|
||||||
guildChannelLastId uint64 // 公会聊天 last id
|
guildChannelLastId uint64 // 公会聊天 last id
|
||||||
privateChatLastId 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 {
|
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)
|
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() {
|
if this.socket.IsValid() {
|
||||||
GetPlayerMgr().UnBindSocket(this.socket)
|
GetPlayerMgr().UnBindSocket(this.socket)
|
||||||
}
|
}
|
||||||
@ -999,3 +1005,8 @@ func (this *player) IncrPrivateChatLastId() uint64 {
|
|||||||
return p.privateChatLastId
|
return p.privateChatLastId
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
func newPlayer() *player {
|
||||||
|
hum := new(player)
|
||||||
|
return hum
|
||||||
|
}
|
||||||
|
@ -14,10 +14,19 @@ import (
|
|||||||
. "main/global"
|
. "main/global"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type pendingLoginRequest struct {
|
||||||
|
hdr *f5.MsgHdr
|
||||||
|
msg *cs.CMLogin
|
||||||
|
addTick int64
|
||||||
|
reqId int64
|
||||||
|
}
|
||||||
|
|
||||||
type playerMgr struct {
|
type playerMgr struct {
|
||||||
cs.MsgHandlerImpl
|
cs.MsgHandlerImpl
|
||||||
accountIdHash map[string]*player
|
accountIdHash map[string]*player
|
||||||
socketHash map[f5.WspCliConn]*player
|
socketHash map[f5.WspCliConn]*player
|
||||||
|
pendingLoginHash map[string]*pendingLoginRequest
|
||||||
|
currReqId int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *playerMgr) init() {
|
func (this *playerMgr) init() {
|
||||||
@ -57,6 +66,51 @@ func (this *playerMgr) unInit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *playerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) {
|
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{
|
params := map[string]string{
|
||||||
"c": "User",
|
"c": "User",
|
||||||
"a": "detailInfo",
|
"a": "detailInfo",
|
||||||
@ -69,30 +123,46 @@ func (this *playerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) {
|
|||||||
url,
|
url,
|
||||||
params,
|
params,
|
||||||
func(rsp f5.HttpCliResponse) {
|
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) {
|
func (this *playerMgr) apiAuthCb(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.HttpCliResponse, reqId int64) {
|
||||||
resObj := common.LoginRsp{}
|
pendingReq := this.getPendingRequest(msg.GetAccountId())
|
||||||
err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj)
|
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 {
|
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)
|
f5.GetSysLog().Info("Api服务器JSON 解析错误:%s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if resObj.Errcode != 0 {
|
if rspObj.Errcode != 0 {
|
||||||
f5.GetSysLog().Error("Api服务器errcode:%d", resObj.Errcode)
|
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
|
return
|
||||||
}
|
}
|
||||||
accountId := msg.GetAccountId()
|
hum := newPlayer()
|
||||||
player := player{
|
hum.init(pendingReq, &rspObj)
|
||||||
socket: hdr.GetSocket(),
|
|
||||||
accountId: accountId,
|
|
||||||
sessionId: msg.GetSessionId(),
|
|
||||||
}
|
|
||||||
// Add to online user
|
// Add to online user
|
||||||
this.addPlayer(&player)
|
//this.addPlayer(&player)
|
||||||
this.addSocketHash(hdr.GetSocket(), &player)
|
//this.addSocketHash(hdr.GetSocket(), &player)
|
||||||
/*
|
/*
|
||||||
// Add player profile
|
// Add player profile
|
||||||
playerProfile := &PlayerProfile{
|
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 {
|
func (this *playerMgr) GetPlayers() map[string]*player {
|
||||||
return this.accountIdHash
|
return this.accountIdHash
|
||||||
}
|
}
|
||||||
@ -163,6 +247,22 @@ func (this *playerMgr) GetPlayerByAccountId(accountId string) *player {
|
|||||||
return nil
|
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 {
|
func (this *playerMgr) getPlayerBySocket(socket f5.WspCliConn) *player {
|
||||||
player, ok := this.socketHash[socket]
|
player, ok := this.socketHash[socket]
|
||||||
if ok {
|
if ok {
|
||||||
@ -198,7 +298,7 @@ func (this *playerMgr) CMReconnect(hdr *f5.MsgHdr, msg *cs.CMReconnect) {
|
|||||||
GetWspListener().SendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
GetWspListener().SendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hum.ReBind(hdr.GetSocket())
|
hum.reBind(hdr.GetSocket())
|
||||||
GetWspListener().SendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
GetWspListener().SendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,6 +167,8 @@ message SMLogin
|
|||||||
{
|
{
|
||||||
optional string server_info = 1; //服务器信息(重连时使用)
|
optional string server_info = 1; //服务器信息(重连时使用)
|
||||||
optional string account_id = 2;
|
optional string account_id = 2;
|
||||||
|
optional int32 errcode = 3; //错误码 0:成功 1:重连失败
|
||||||
|
optional string errmsg = 4; //错误描述
|
||||||
}
|
}
|
||||||
|
|
||||||
//断线重连
|
//断线重连
|
||||||
|
Loading…
x
Reference in New Issue
Block a user