refactor(entity): 重构 ClientData 结构体并添加同步锁

- 在 ClientData 中添加 sync.Mutex 以确保并发安全
- 实现 SetPlayer 和 GetPlayer 等方法来封装 player 字段的操作
- 更新相关代码以使用新的 ClientData 结构和方法
This commit is contained in:
2025-07-06 22:58:39 +08:00
parent 53da82df82
commit 666557416d
9 changed files with 142 additions and 49 deletions

View File

@@ -17,6 +17,7 @@ var usermap = make(map[int]int) //用户->客户端的map
// Define the client handler interface
type ClientHandler struct {
KickPerson func(int) error //踢人,这里是返回具体的logic
QuitSelf func(int) error //关闭服务器进程
}
// Define the server handler
@@ -43,18 +44,30 @@ func (h *ServerHandler) Kick(ctx context.Context, userid int) error {
}
// 退出指定服务器
func (h *ServerHandler) Quit(ctx context.Context, userid int) error {
// func (h *ServerHandler) Quit(ctx context.Context, portid int) error {
return nil
// a, ok := clientmap[portid]
}
// if ok && a != nil {
// a.QuitSelf(0)
// }
// //clientmap[portid].QuitSelf(0)
// return nil
// }
// 注册logic服务器
func (h *ServerHandler) RegisterLogic(ctx context.Context, port int) error {
//TODO 待修复滚动更新可能导致的玩家可以同时在旧服务器和新服务器同时在线的bug
revClient, ok := jsonrpc.ExtractReverseClient[ClientHandler](ctx)
if !ok {
return fmt.Errorf("no reverse client")
}
aa, ok := clientmap[port]
if ok && aa != nil {
aa.QuitSelf(0)
}
clientmap[port] = &revClient
return nil
@@ -92,8 +105,8 @@ func StartServer() {
var closer jsonrpc.ClientCloser
func StartClient(port uint16, callback any) *struct {
Kick func(int32) error
Quit func(int32) error
Kick func(int32) error
RegisterLogic func(int32) error
UserLogin func(int32, int32) error
UserLogout func(int32, int32) error
@@ -124,8 +137,8 @@ func CloseClient() {
// Setup RPCClient with reverse call handler
var RPCClient struct {
Kick func(int32) error //踢人
Quit func(int32) error //退出指定服务器
Kick func(int32) error //踢人
RegisterLogic func(int32) error //注册服务器消息
UserLogin func(int32, int32) error //用户登录事件