```
fix(controller): 优化数据发送逻辑,避免并发写冲突 将原先直接操作连接上下文并加锁的写法, 改为通过 player 实例封装的 SendPack 方法发送数据, 提高代码可维护性和安全性。 同时调整了 service 层踢出玩家时锁的获取顺序, 确保在关闭连接前
This commit is contained in:
@@ -185,21 +185,17 @@ func Recv(c gnet.Conn, data player.TomeeHeader) {
|
|||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
t := player.GetPlayer(c, data.UserID)
|
||||||
if ok && aa != 0 { //这里实现回复错误包
|
if ok && aa != 0 { //这里实现回复错误包
|
||||||
|
|
||||||
cool.Loger.Error(context.Background(), aa.Code())
|
cool.Loger.Error(context.Background(), aa.Code())
|
||||||
c.Context().(*player.ClientData).Mu.Lock()
|
t.SendPack(data.Pack(nil))
|
||||||
c.Write(data.Pack(nil))
|
|
||||||
c.Context().(*player.ClientData).Mu.Unlock()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data.Version = "7"
|
data.Version = "7"
|
||||||
//glog.Debug(context.Background(), data.CMD, "回复数据")
|
t.SendPack(data.Pack(ret[0].Interface()))
|
||||||
c.Context().(*player.ClientData).Mu.Lock()
|
|
||||||
c.Write(data.Pack(ret[0].Interface()))
|
|
||||||
c.Context().(*player.ClientData).Mu.Unlock()
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,9 +39,9 @@ func KickPlayer(userid uint32) { //踢出玩家
|
|||||||
head := NewTomeeHeader(1001, userid)
|
head := NewTomeeHeader(1001, userid)
|
||||||
head.Result = uint32(errorcode.ErrorCodes.ErrAccountLoggedInElsewhere)
|
head.Result = uint32(errorcode.ErrorCodes.ErrAccountLoggedInElsewhere)
|
||||||
//实际上这里有个问题,会造成重复保存问题
|
//实际上这里有个问题,会造成重复保存问题
|
||||||
player1.MainConn.Context().(*ClientData).Mu.Lock()
|
|
||||||
player1.SendPack(head.Pack(nil))
|
|
||||||
|
|
||||||
|
player1.SendPack(head.Pack(nil))
|
||||||
|
player1.MainConn.Context().(*ClientData).Mu.Lock()
|
||||||
player1.MainConn.Close()
|
player1.MainConn.Close()
|
||||||
player1.MainConn.Context().(*ClientData).Mu.Unlock()
|
player1.MainConn.Context().(*ClientData).Mu.Unlock()
|
||||||
// clientdata.Player = player
|
// clientdata.Player = player
|
||||||
|
|||||||
Reference in New Issue
Block a user