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"` // 文件上传配置 File *file `json:"file,omitempty"` // 文件上传配置
Name string `json:"name"` // 项目名称 Name string `json:"name"` // 项目名称
// LoginPort string `json:"port"` // LoginPort string `json:"port"`
GameOnlineID uint16 `json:"port_bl"` //这个是命令行输入的参数 GameOnlineID uint32 `json:"port_bl"` //这个是命令行输入的参数
ServerInfo ServerList ServerInfo ServerList
Address string //rpc端口 Address string //rpc端口
} }
type ServerList struct { 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 //服务器名称Desc
Name string `gorm:"comment:'服务器名称'" json:"name"` Name string `gorm:"comment:'服务器名称'" json:"name"`
IP string `gorm:"type:string;comment:'服务器IP'" json:"ip"` 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"` IsOpen uint8 `gorm:"default:0;not null;comment:'是否开启'" json:"is_open"`
//登录地址 //登录地址
LoginAddr string `gorm:"type:string;comment:'登录地址'" json:"login_addr"` LoginAddr string `gorm:"type:string;comment:'登录地址'" json:"login_addr"`

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -23,11 +23,11 @@ var Maincontroller = &Controller{} //注入service
// Controller 分发cmd逻辑实现 // Controller 分发cmd逻辑实现
type Controller struct { type Controller struct {
Port uint16 Port uint32
RPCClient *struct { RPCClient *struct {
Kick func(uint32) error Kick func(uint32) error
RegisterLogic func(uint16, uint16) error RegisterLogic func(uint32, uint32) error
} }
} }

View File

@@ -97,7 +97,7 @@ func main() {
// go cool.ListenFunc(gctx.New()) // go cool.ListenFunc(gctx.New())
// } // }
// 解析命令行参数 // 解析命令行参数
cool.Config.GameOnlineID = gcmd.GetOpt("id", "1").Uint16() cool.Config.GameOnlineID = gcmd.GetOpt("id", "1").Uint32()
go Start() //注入service go Start() //注入service
// if cool.Config.GameOnlineID == 2 { //只分析1服务器的 // if cool.Config.GameOnlineID == 2 { //只分析1服务器的
// go PprofWeb() // go PprofWeb()

View File

@@ -66,7 +66,7 @@ func Start() {
} }
port, err := determinePort(cool.Config.ServerInfo.CanPort) port, err := determinePort(cool.Config.ServerInfo.CanPort)
cool.Config.ServerInfo.Port = uint16(port) cool.Config.ServerInfo.Port = uint32(port)
if err != nil { if err != nil {
log.Fatalf("Failed to determine port: %v", err) log.Fatalf("Failed to determine port: %v", err)
} }
@@ -75,12 +75,12 @@ func Start() {
socket.WithPort(port), socket.WithPort(port),
) )
rpcClient := rpc.StartClient(serverID, uint16(port), server) //连接rpc rpcClient := rpc.StartClient(serverID, uint32(port), server) //连接rpc
controller.Maincontroller.RPCClient = rpcClient //将RPC赋值Start controller.Maincontroller.RPCClient = rpcClient //将RPC赋值Start
controller.Maincontroller.Port = uint16(port) //赋值服务器ID controller.Maincontroller.Port = uint32(port) //赋值服务器ID
controller.Init(true) controller.Init(true)
xmlres.Initfile() xmlres.Initfile()
server.Boot(serverID, gconv.Uint16(port)) server.Boot(serverID, gconv.Uint32(port))
} }

View File

@@ -34,9 +34,9 @@ type QuitSReq struct {
func (this *ServerController) Quit(ctx context.Context, req *QuitSReq) (res *cool.BaseRes, err error) { func (this *ServerController) Quit(ctx context.Context, req *QuitSReq) (res *cool.BaseRes, err error) {
res = &cool.BaseRes{} res = &cool.BaseRes{}
serv := service.NewServerService().GetServerID(uint16(req.ID)) serv := service.NewServerService().GetServerID(req.ID)
aa, ok := cool.GetClient(serv.Port) aa, ok := cool.GetClient(serv.OnlineID*100000 + serv.Port)
if ok && aa != nil { //如果已经存在且这个端口已经被存过 if ok && aa != nil { //如果已经存在且这个端口已经被存过
aa.QuitSelf(req.Code) aa.QuitSelf(req.Code)
} }

View File

@@ -29,7 +29,7 @@ func NewServerService() *ServerService {
var rr []g.MapStrAny var rr []g.MapStrAny
r, _ := gconv.Map(data)["list"].(gdb.Result) r, _ := gconv.Map(data)["list"].(gdb.Result)
for i := 0; i < len(r); i++ { for i := 0; i < len(r); i++ {
t, ok := cool.GetClient(gconv.Uint16(r[i].Map()["port"])) t, ok := cool.GetClient(10000*gconv.Uint32(r[i].Map()["online_id"])+gconv.Uint32(r[i].Map()["port"]))
// tt.Friends = v.Friends // tt.Friends = v.Friends
subm := r[i].GMap() subm := r[i].GMap()
@@ -107,7 +107,7 @@ func (s *ServerService) StartUPdate(OnlineID uint16, isinstall int) model.Server
return tttt return tttt
} }
func (s *ServerService) SetServerID(OnlineID uint16, Port uint16) error { func (s *ServerService) SetServerID(OnlineID uint32, Port uint32) error {
m := cool.DBM(s.Model).Where("online_id", OnlineID) m := cool.DBM(s.Model).Where("online_id", OnlineID)
@@ -122,7 +122,7 @@ func (s *ServerService) SetServerID(OnlineID uint16, Port uint16) error {
return nil return nil
} }
func (s *ServerService) GetServerID(OnlineID uint16) model.ServerList { func (s *ServerService) GetServerID(OnlineID uint32) model.ServerList {
var tttt model.ServerList var tttt model.ServerList
cool.DBM(s.Model).Where("online_id", OnlineID).Scan(&tttt) cool.DBM(s.Model).Where("online_id", OnlineID).Scan(&tttt)
@@ -131,7 +131,7 @@ func (s *ServerService) GetServerID(OnlineID uint16) model.ServerList {
} }
// 保存版本号 // 保存版本号
func (s *ServerService) SetServerScreen(id uint16, name string) { func (s *ServerService) SetServerScreen(id uint32, name string) {
cool.DBM(s.Model).Where("online_id", id).Data("old_screen", name).Update() cool.DBM(s.Model).Where("online_id", id).Data("old_screen", name).Update()