refactor: 重构运行时ID组合逻辑
将硬编码的 ID 组合逻辑(100000*OnlineID + Port)提取为通用函数 ComposeRuntimeID, 使用 16 位位移掩码优化,并新增辅助方法与类型转换。同时修复踢人流程中的资源清理问题。
This commit is contained in:
@@ -1,25 +1,27 @@
|
||||
package cool
|
||||
|
||||
import "blazing/cool/coolconfig"
|
||||
|
||||
// 存值示例
|
||||
func AddClient(id uint32, client *ClientHandler) {
|
||||
// 普通map:Clientmap[id] = client
|
||||
Clientmap.Store(id, client) // sync.Map存值
|
||||
}
|
||||
|
||||
// 清理指定client(uid=100000*onlineID+port)
|
||||
// 清理指定 client(高 16 位 serverID,低 16 位 port)
|
||||
func DeleteClientOnly(uid uint32) {
|
||||
Clientmap.Delete(uid)
|
||||
}
|
||||
|
||||
// 清理指定client(onlineID+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) {
|
||||
// 普通map:client, 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user