将硬编码的 ID 组合逻辑(100000*OnlineID + Port)提取为通用函数 ComposeRuntimeID, 使用 16 位位移掩码优化,并新增辅助方法与类型转换。同时修复踢人流程中的资源清理问题。
23 lines
794 B
Go
23 lines
794 B
Go
package coolconfig
|
||
|
||
import "github.com/gogf/gf/v2/util/gconv"
|
||
|
||
const runtimeIDPortBits = 16
|
||
const runtimeIDPortMask uint32 = 0xFFFF
|
||
|
||
// ComposeRuntimeID 将 serverID 和 port 组合成运行时复合 ID。
|
||
// 高 16 位保存 serverID,低 16 位保存 port。
|
||
func ComposeRuntimeID(serverID, port uint32) uint32 {
|
||
return ((serverID & runtimeIDPortMask) << runtimeIDPortBits) | (port & runtimeIDPortMask)
|
||
}
|
||
|
||
// SplitRuntimeID 将运行时复合 ID 拆分为 serverID 和 port。
|
||
func SplitRuntimeID(runtimeID uint32) (serverID, port uint32) {
|
||
return runtimeID >> runtimeIDPortBits, runtimeID & runtimeIDPortMask
|
||
}
|
||
|
||
// RuntimeIDString 返回运行时复合 ID 的字符串形式。
|
||
func RuntimeIDString(serverID, port uint32) string {
|
||
return gconv.String(ComposeRuntimeID(serverID, port))
|
||
}
|