refactor: 将端口和在线ID类型从uint16改为uint32
Some checks failed
ci/woodpecker/push/my-first-workflow Pipeline failed

This commit is contained in:
xinian
2026-03-02 18:34:20 +08:00
committed by cnb
parent ae06d18aa2
commit 47bc680889
12 changed files with 35 additions and 35 deletions

View File

@@ -13,18 +13,18 @@ type sConfig struct {
File *file `json:"file,omitempty"` // 文件上传配置
Name string `json:"name"` // 项目名称
// LoginPort string `json:"port"`
GameOnlineID uint16 `json:"port_bl"` //这个是命令行输入的参数
GameOnlineID uint32 `json:"port_bl"` //这个是命令行输入的参数
ServerInfo ServerList
Address string //rpc端口
}
type ServerList struct {
OnlineID uint16 `gorm:"column:online_id;comment:'在线ID';uniqueIndex" json:"online_id"`
OnlineID uint32 `gorm:"column:online_id;comment:'在线ID';uniqueIndex" json:"online_id"`
//服务器名称Desc
Name string `gorm:"comment:'服务器名称'" json:"name"`
IP string `gorm:"type:string;comment:'服务器IP'" json:"ip"`
Port uint16 `gorm:"comment:'端口号,通常是小整数'" json:"port"`
Port uint32 `gorm:"comment:'端口号,通常是小整数'" json:"port"`
IsOpen uint8 `gorm:"default:0;not null;comment:'是否开启'" json:"is_open"`
//登录地址
LoginAddr string `gorm:"type:string;comment:'登录地址'" json:"login_addr"`

View File

@@ -1,13 +1,13 @@
package cool
// 存值示例
func AddClient(id uint16, client *ClientHandler) {
func AddClient(id uint32, client *ClientHandler) {
// 普通mapClientmap[id] = client
Clientmap.Store(id, client) // sync.Map存值
}
// 取值示例
func GetClient(id uint16) (*ClientHandler, bool) {
func GetClient(id uint32) (*ClientHandler, bool) {
// 普通mapclient, ok := Clientmap[id]
val, ok := Clientmap.Load(id) // sync.Map取值
if !ok {

View File

@@ -20,8 +20,8 @@ func newSessionStore() *cacheStore[uint32] {
}
// newUserOnlineStore 创建用户在线状态缓存实例
func newUserOnlineStore() *cacheStore[uint16] {
return &cacheStore[uint16]{
func newUserOnlineStore() *cacheStore[uint32] {
return &cacheStore[uint32]{
manager: cool.CacheManager,
prefix: "blazing:useronline:",
}
@@ -38,7 +38,7 @@ func newEmailCodeStore() *cacheStore[int] {
// sessionManager 会话管理器
type sessionManager struct {
sessionStore *cacheStore[uint32] // 会话缓存
userOnlineStore *cacheStore[uint16] // 用户在线状态缓存
userOnlineStore *cacheStore[uint32] // 用户在线状态缓存
emailCodeStore *cacheStore[int] // 邮件注册码缓存
}
@@ -52,12 +52,12 @@ func newSessionManager() *sessionManager {
}
// SetUserOnline 设置用户在线状态
func (m *sessionManager) SetUserOnline(userID uint32, serverID uint16) error {
func (m *sessionManager) SetUserOnline(userID uint32, serverID uint32) error {
return m.userOnlineStore.Set(gctx.New(), gconv.String(userID), serverID, 0)
}
// GetUserOnline 获取用户在线状态
func (m *sessionManager) GetUserOnline(userID uint32) (uint16, error) {
func (m *sessionManager) GetUserOnline(userID uint32) (uint32, error) {
return m.userOnlineStore.Get(context.Background(), gconv.String(userID))
}

View File

@@ -33,7 +33,7 @@ func GetServerInfoList(isdebug int32) []ServerInfo {
}
tt.Name = v.Name
tt.Port = v.Port
tt.Port =uint16( v.Port)
ret1 = append(ret1, *tt)
}
@@ -89,7 +89,7 @@ type ServerInfo struct {
// 服务器IP, 16字节UTF-8, 不足16补齐到16
IP string `struc:"[16]byte"` // 定长模式16字节
// 端口
Port uint16
Port uint16
// 好友在线的个数
Friends uint32
}

View File

@@ -34,7 +34,7 @@ func (*ServerHandler) Kick(_ context.Context, userid uint32) error {
}
// 注册logic服务器
func (*ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) error {
func (*ServerHandler) RegisterLogic(ctx context.Context, id, port uint32) error {
fmt.Println("注册logic服务器", id, port)
//TODO 待修复滚动更新可能导致的玩家可以同时在旧服务器和新服务器同时在线的bug
@@ -44,11 +44,11 @@ func (*ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) error
}
t := config.NewServerService().GetServerID((id))
aa, ok := cool.GetClient(t.Port)
aa, ok := cool.GetClient(t.OnlineID*100000 + t.Port)
if ok && aa != nil { //如果已经存在且这个端口已经被存过
aa.QuitSelf(0)
}
cool.AddClient(port, &revClient)
cool.AddClient(100000*id+port, &revClient)
//Refurh()
return nil
@@ -67,10 +67,10 @@ func CServer() *jsonrpc.RPCServer {
var closer jsonrpc.ClientCloser
func StartClient(id, port uint16, callback any) *struct {
func StartClient(id, port uint32, callback any) *struct {
Kick func(uint32) error
RegisterLogic func(uint16, uint16) error
RegisterLogic func(uint32, uint32) error
} {
// cool.Config.File.Domain = "127.0.0.1"
var rpcaddr = "ws://" + cool.Config.File.Domain + gconv.String(cool.Config.Address) + "/rpc"
@@ -99,7 +99,7 @@ func StartClient(id, port uint16, callback any) *struct {
var RPCClient struct {
Kick func(uint32) error //踢人
RegisterLogic func(uint16, uint16) error
RegisterLogic func(uint32, uint32) error
// UserLogin func(int32, int32) error //用户登录事件
// UserLogout func(int32, int32) error //用户登出事件

View File

@@ -20,7 +20,7 @@ import (
"github.com/panjf2000/gnet/v2"
)
func (s *Server) Boot(serverid, port uint16) error {
func (s *Server) Boot(serverid, port uint32) error {
// go s.bootws()
s.serverid = serverid
s.port = port

View File

@@ -25,8 +25,8 @@ type Server struct {
discorse bool
quit bool
// batchRead int
serverid uint16
port uint16
serverid uint32
port uint32
}
type Option func(*Server)