将硬编码的 ID 组合逻辑(100000*OnlineID + Port)提取为通用函数 ComposeRuntimeID, 使用 16 位位移掩码优化,并新增辅助方法与类型转换。同时修复踢人流程中的资源清理问题。
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package cool
|
||
|
||
import "blazing/cool/coolconfig"
|
||
|
||
// 存值示例
|
||
func AddClient(id uint32, client *ClientHandler) {
|
||
// 普通map:Clientmap[id] = client
|
||
Clientmap.Store(id, client) // sync.Map存值
|
||
}
|
||
|
||
// 清理指定 client(高 16 位 serverID,低 16 位 port)
|
||
func DeleteClientOnly(uid uint32) {
|
||
Clientmap.Delete(uid)
|
||
}
|
||
|
||
// 清理指定 client(由 serverID 和 port 组合)
|
||
func DeleteClient(id, port uint32) {
|
||
Clientmap.Delete(coolconfig.ComposeRuntimeID(id, port))
|
||
}
|
||
|
||
// 取值示例
|
||
func GetClient(id, port uint32) (*ClientHandler, bool) {
|
||
// 普通map:client, ok := Clientmap[id]
|
||
val, ok := Clientmap.Load(coolconfig.ComposeRuntimeID(id, port)) // sync.Map取值
|
||
if !ok {
|
||
return nil, false
|
||
}
|
||
// 类型断言(确保value是*ClientHandler)
|
||
client, ok := val.(*ClientHandler)
|
||
return client, ok
|
||
}
|
||
func GetClientOnly(uid uint32) (*ClientHandler, bool) {
|
||
// 普通map:client, ok := Clientmap[id]
|
||
val, ok := Clientmap.Load(uid) // 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 //全服广播,返回的是在线人数
|
||
}
|