Files
bl/common/cool/rpc.go
xinian 7d49aaa212 refactor: 重构运行时ID组合逻辑
将硬编码的 ID 组合逻辑(100000*OnlineID + Port)提取为通用函数 ComposeRuntimeID,
使用 16 位位移掩码优化,并新增辅助方法与类型转换。同时修复踢人流程中的资源清理问题。
2026-04-28 04:03:13 +08:00

48 lines
1.3 KiB
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
import "blazing/cool/coolconfig"
// 存值示例
func AddClient(id uint32, client *ClientHandler) {
// 普通mapClientmap[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) {
// 普通mapclient, 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) {
// 普通mapclient, 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 //全服广播,返回的是在线人数
}