refactor(common): 使用 sync.Map 优化全局客户端映射 将 common/cool/global.go 中的 Clientmap 从普通 map 替换为 sync.Map, 以提高并发安全性。同时迁移相关操作函数至 cool 包中统一管理。 更新了 rpc 和 service 层代码,确保正确调用新的客户端管理方法。 在 InfoService 中新增 Kick 方法用于处理用户踢出逻辑。 ```
26 lines
698 B
Go
26 lines
698 B
Go
package cool
|
||
|
||
// 存值示例
|
||
func AddClient(id uint16, client *ClientHandler) {
|
||
// 普通map:Clientmap[id] = client
|
||
Clientmap.Store(id, client) // sync.Map存值
|
||
}
|
||
|
||
// 取值示例
|
||
func GetClient(id uint16) (*ClientHandler, bool) {
|
||
// 普通map:client, 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 //全服广播,返回的是在线人数
|
||
}
|