Files
bl/logic/server.go
昔念 caa5fc37b9 ```
refactor(common/rpc): 修改服务引用从blazing到config模块

将RPC服务中的blservice引用替换为config服务,
统一使用config.NewServerService()进行服务器信息获取。

feat(blazing): 实现新的会话生成机制

- 添加Gensession方法,基于accountID、UUID生成唯一会话标识
- 会话ID由accountID(4字节) + UUID(16字节)组成,编码为十六进制字符串
- 更新登录控制器使用新的会话生成方式

fix(pet_info): 添加宠物信息空值检查

在切换宠物背包仓库时,当宠物信息查询结果为空时,
返回系统错误避免空指针异常。
2026-01-09 19:58:12 +08:00

85 lines
2.0 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 main
import (
"blazing/common/data/xmlres"
"blazing/common/rpc"
"blazing/common/socket"
"blazing/cool"
"blazing/logic/controller"
config "blazing/modules/config/service"
"fmt"
"log"
"net"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
)
const (
minRandomPort = 10000
maxRandomPort = 60000
maxPortRetryCount = 5
)
// determinePort 确定服务器使用的端口
func determinePort(ports []uint32) (int, error) {
// 尝试从指定端口列表中找可用端口最多尝试maxPortRetryCount轮
for i := 0; i < maxPortRetryCount; i++ {
// 遍历指定的端口列表
for _, port := range ports {
if isPortAvailable(port) {
return int(port), nil
}
log.Printf("Port %d is not available, checking next...", port)
}
log.Printf("All candidate ports are in use, retrying round %d...", i+1)
}
return 0, fmt.Errorf("failed to find available port after %d rounds of checking", maxPortRetryCount)
}
// isPortAvailable 检查端口是否可用
func isPortAvailable(port uint32) bool {
address := fmt.Sprintf(":%d", port)
listener, err := net.Listen("tcp", address)
if err != nil {
return false
}
defer listener.Close()
return true
}
// Start 启动服务器
// 如果id是0,那就是login server
func Start() {
serverID := cool.Config.GameOnlineID
cool.Config.ServerInfo = config.NewServerService().GetServerID(serverID).ServerList
if cool.Config.ServerInfo.IsVip == 1 {
g.DB().SetDebug(true)
}
port, err := determinePort(cool.Config.ServerInfo.CanPort)
cool.Config.ServerInfo.Port = uint16(port)
if err != nil {
log.Fatalf("Failed to determine port: %v", err)
}
server := socket.NewServer(
socket.WithCORS(),
socket.WithPort(port),
)
// go func() {
rpcClient := rpc.StartClient(serverID, uint16(port), server)
controller.Maincontroller.RPCClient = *rpcClient //将RPC赋值Start
controller.Maincontroller.Port = uint16(port) //赋值服务器ID
controller.Init(true)
xmlres.Initfile()
config.NewServerService().SetServerID(serverID, gconv.Uint16(port))
server.Boot()
}