Files
bl/common/cool/rpc.go
昔念 2598dee881 ```
refactor(common): 使用 sync.Map 优化全局客户端映射

将 common/cool/global.go 中的 Clientmap 从普通 map 替换为 sync.Map,
以提高并发安全性。同时迁移相关操作函数至 cool 包中统一管理。

更新了 rpc 和 service 层代码,确保正确调用新的客户端管理方法。
在 InfoService 中新增 Kick 方法用于处理用户踢出逻辑。
```
2025-12-09 02:48:41 +08:00

26 lines
698 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cool
// 存值示例
func AddClient(id uint16, client *ClientHandler) {
// 普通mapClientmap[id] = client
Clientmap.Store(id, client) // sync.Map存值
}
// 取值示例
func GetClient(id uint16) (*ClientHandler, bool) {
// 普通mapclient, ok := Clientmap[id]
val, ok := Clientmap.Load(id) // sync.Map取值
if !ok {
return nil, false
}
// 类型断言确保value是*ClientHandler
client, ok := val.(*ClientHandler)
return client, ok
}
type ClientHandler struct {
KickPerson func(uint32) error //踢人,这里是返回具体的logic
QuitSelf func(int) error //关闭服务器进程
Broadcast func(string) int //全服广播,返回的是在线人数
}