1
This commit is contained in:
parent
b4191a43eb
commit
50df272959
@ -8,7 +8,7 @@ import (
|
||||
. "main/global"
|
||||
)
|
||||
|
||||
type Player struct {
|
||||
type player struct {
|
||||
cs.MsgHandlerImpl
|
||||
socket f5.WspCliConn
|
||||
accountId string
|
||||
@ -16,14 +16,14 @@ type Player struct {
|
||||
room *common.Room
|
||||
}
|
||||
|
||||
func (this *Player) GetAccountId() string {
|
||||
func (this *player) GetAccountId() string {
|
||||
return this.accountId
|
||||
}
|
||||
|
||||
func (this *Player) SendMsg(rspMsg proto.Message) {
|
||||
func (this *player) SendMsg(rspMsg proto.Message) {
|
||||
GetWspListener().SendProxyMsg(this.socket.Conn, this.socket.SocketHandle, rspMsg)
|
||||
}
|
||||
|
||||
func (this *Player) GetRoom() *common.Room {
|
||||
func (this *player) GetRoom() *common.Room {
|
||||
return this.room
|
||||
}
|
||||
|
@ -9,17 +9,28 @@ import (
|
||||
"q5"
|
||||
"main/constant"
|
||||
"github.com/golang/protobuf/proto"
|
||||
. "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
|
||||
accountIdHash map[string]*player
|
||||
socketHash map[f5.WspCliConn]*player
|
||||
pendingLoginHash map[string]*pendingLoginRequest
|
||||
currReqId int64
|
||||
}
|
||||
|
||||
func (this *playerMgr) Init() {
|
||||
this.accountIdHash = make(map[string]*Player)
|
||||
this.socketHash = make(map[f5.WspCliConn]*Player)
|
||||
this.accountIdHash = make(map[string]*player)
|
||||
this.socketHash = make(map[f5.WspCliConn]*player)
|
||||
this.pendingLoginHash = make(map[string]*pendingLoginRequest)
|
||||
|
||||
f5.GetTimer().SetInterval(
|
||||
1000,
|
||||
@ -38,13 +49,46 @@ func (this *playerMgr) UnInit() {
|
||||
}
|
||||
|
||||
func (this *playerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) {
|
||||
oldPlayer := this.getPlayerBySocket(hdr.GetSocket())
|
||||
if oldPlayer != nil {
|
||||
rspMsg := cs.SMLogin{}
|
||||
rspMsg.Errcode = proto.Int32(0)
|
||||
rspMsg.Errmsg = proto.String("")
|
||||
oldPlayer.SendMsg(&rspMsg)
|
||||
return;
|
||||
{
|
||||
oldplayer := this.getPlayerBySocket(hdr.GetSocket())
|
||||
if oldplayer != nil {
|
||||
rspMsg := cs.SMLogin{}
|
||||
rspMsg.Errcode = proto.Int32(0)
|
||||
rspMsg.Errmsg = proto.String("")
|
||||
oldplayer.SendMsg(&rspMsg)
|
||||
return;
|
||||
}
|
||||
}
|
||||
{
|
||||
oldplayer := this.getPlayerByAccountId(msg.GetAccountId())
|
||||
if oldplayer != nil {
|
||||
rspMsg := cs.SMLogin{}
|
||||
rspMsg.Errcode = proto.Int32(0)
|
||||
rspMsg.Errmsg = proto.String("")
|
||||
oldplayer.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",
|
||||
@ -58,11 +102,22 @@ func (this *playerMgr) CMLogin(hdr *f5.MsgHdr, msg *cs.CMLogin) {
|
||||
url,
|
||||
params,
|
||||
func(rsp f5.HttpCliResponse) {
|
||||
this.CMLoginResult(hdr, msg, rsp)
|
||||
this.apiAuthCb(hdr, msg, rsp, reqId)
|
||||
})
|
||||
}
|
||||
|
||||
func (this *playerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.HttpCliResponse) {
|
||||
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
|
||||
}
|
||||
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)
|
||||
return
|
||||
}
|
||||
resObj := struct {
|
||||
Errcode int `json:"errcode"`
|
||||
Errmsg string `json:"errmsg"`
|
||||
@ -73,29 +128,36 @@ func (this *playerMgr) CMLoginResult(hdr *f5.MsgHdr, msg *cs.CMLogin, rsp f5.Htt
|
||||
Name string `json:"name"`
|
||||
} `json:"info"`
|
||||
}{}
|
||||
err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj)
|
||||
if err != nil {
|
||||
f5.GetSysLog().Info("Api服务器JSON 解析错误:%s\n", err)
|
||||
return
|
||||
{
|
||||
err := json.Unmarshal([]byte(rsp.GetRawData()), &resObj)
|
||||
if err != nil {
|
||||
rspMsg.Errcode = proto.Int32(1)
|
||||
rspMsg.Errmsg = proto.String("invalid session_id")
|
||||
GetWspListener().SendProxyMsg(pendingReq.hdr.Conn, pendingReq.hdr.SocketHandle, &rspMsg)
|
||||
f5.GetSysLog().Warning("game2006api parse error:%s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if resObj.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().Warning("game2006api login auth errcode:%d", resObj.Errcode)
|
||||
return
|
||||
}
|
||||
|
||||
player := Player{
|
||||
player := player{
|
||||
socket: hdr.GetSocket(),
|
||||
accountId: msg.GetAccountId(),
|
||||
sessionId: msg.GetAccountId(),
|
||||
}
|
||||
// Add to online user
|
||||
this.addPlayer(&player)
|
||||
this.addplayer(&player)
|
||||
this.addSocketHash(hdr.GetSocket(), &player)
|
||||
|
||||
serverInfo := "192.168.100.39:2000"
|
||||
rspMsg := &cs.SMLogin{}
|
||||
rspMsg.ServerInfo = &serverInfo
|
||||
//wspListener.sendProxyMsg(hdr.Conn, hdr.SocketHandle, rspMsg)
|
||||
GetWspListener().SendProxyMsg(hdr.Conn, hdr.SocketHandle, &rspMsg)
|
||||
}
|
||||
|
||||
func (this *playerMgr) reportServerState(masterIp string, masterPort int32) {
|
||||
@ -118,15 +180,15 @@ func (this *playerMgr) reportServerState(masterIp string, masterPort int32) {
|
||||
})
|
||||
}
|
||||
|
||||
func (this *playerMgr) GetPlayers() map[string]*Player {
|
||||
func (this *playerMgr) GetPlayers() map[string]*player {
|
||||
return this.accountIdHash
|
||||
}
|
||||
|
||||
func (this *playerMgr) addPlayer(p *Player) {
|
||||
func (this *playerMgr) addplayer(p *player) {
|
||||
this.accountIdHash[p.accountId] = p
|
||||
}
|
||||
|
||||
func (this *playerMgr) getPlayerByAccountId(accountId string) *Player {
|
||||
func (this *playerMgr) getPlayerByAccountId(accountId string) *player {
|
||||
player, ok := this.accountIdHash[accountId]
|
||||
if ok {
|
||||
return player
|
||||
@ -134,7 +196,7 @@ func (this *playerMgr) getPlayerByAccountId(accountId string) *Player {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *playerMgr) getPlayerBySocket(socket f5.WspCliConn) *Player {
|
||||
func (this *playerMgr) getPlayerBySocket(socket f5.WspCliConn) *player {
|
||||
player, ok := this.socketHash[socket]
|
||||
if ok {
|
||||
return player
|
||||
@ -142,7 +204,7 @@ func (this *playerMgr) getPlayerBySocket(socket f5.WspCliConn) *Player {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *playerMgr) addSocketHash(wsp f5.WspCliConn, p *Player) {
|
||||
func (this *playerMgr) addSocketHash(wsp f5.WspCliConn, p *player) {
|
||||
this.socketHash[wsp] = p
|
||||
}
|
||||
|
||||
@ -157,3 +219,17 @@ func (this* playerMgr) ProcessCMMsg(handler *cs.CsNetMsgHandler, hdr *f5.MsgHdr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user