refactor: 重构运行时ID组合逻辑

将硬编码的 ID 组合逻辑(100000*OnlineID + Port)提取为通用函数 ComposeRuntimeID,
使用 16 位位移掩码优化,并新增辅助方法与类型转换。同时修复踢人流程中的资源清理问题。
This commit is contained in:
xinian
2026-04-28 04:03:13 +08:00
parent deae6d371e
commit 7d49aaa212
10 changed files with 59 additions and 15 deletions

View File

@@ -1,25 +1,27 @@
package cool
import "blazing/cool/coolconfig"
// 存值示例
func AddClient(id uint32, client *ClientHandler) {
// 普通mapClientmap[id] = client
Clientmap.Store(id, client) // sync.Map存值
}
// 清理指定clientuid=100000*onlineID+port
// 清理指定 client高 16 位 serverID低 16 位 port
func DeleteClientOnly(uid uint32) {
Clientmap.Delete(uid)
}
// 清理指定clientonlineID+port
// 清理指定 client由 serverID 和 port 组合
func DeleteClient(id, port uint32) {
Clientmap.Delete(100000*id + port)
Clientmap.Delete(coolconfig.ComposeRuntimeID(id, port))
}
// 取值示例
func GetClient(id, port uint32) (*ClientHandler, bool) {
// 普通mapclient, ok := Clientmap[id]
val, ok := Clientmap.Load(100000*id + port) // sync.Map取值
val, ok := Clientmap.Load(coolconfig.ComposeRuntimeID(id, port)) // sync.Map取值
if !ok {
return nil, false
}