Files
bl/logic/server.go
昔念 79d4343cdc
All checks were successful
ci/woodpecker/push/my-first-workflow Pipeline was successful
```
feat(common/cool): 更新GetClient函数支持端口参数

更新GetClient函数签名以接收端口参数,并修改客户端映射键的计算方式,
添加GetClientOnly函数用于仅通过uid获取客户端。

fix(common/rpc): 修复RPC调用中的客户端获取方法

将GetClient调用替换为GetClientOnly,确保正确的客户端获取逻辑。

refactor(logic/controller): 重命名Port字段为UID并优化道具列表处理

将Controller结构体中的Port字段重命名为UID以更好地反映其用途,
优化GetUserItemList函数中道具列表的初始化和填充逻辑。

perf(logic): 调整性能分析web服务启动位置

将PprofWeb服务从全局启动移至调试模式下启动,优化服务配置。

refactor(logic/server): 更新服务器UID生成逻辑

修改Maincontroller的UID字段设置方式,使用服务器ID和端口组合生成唯一标识。

refactor(logic/service/player): 移除未使用的导入并优化怪物生成

移除未使用的service导入,优化怪物生成逻辑中的地图数据访问。

feat(logic/service/space): 添加PitS缓存映射并重构空间初始化

添加新的PitS字段
2026-03-02 23:59:15 +08:00

90 lines
2.2 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 != 0 {
// g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(cool.Redis)) //设置数据库
// }
if cool.Config.ServerInfo.IsDebug == 1 {
g.DB().SetDebug(true)
go PprofWeb()
}
port, err := determinePort(cool.Config.ServerInfo.CanPort)
cool.Config.ServerInfo.Port = uint32(port)
if err != nil {
log.Fatalf("Failed to determine port: %v", err)
}
server := socket.NewServer(
socket.WithCORS(),
socket.WithPort(port),
)
rpcClient := rpc.StartClient(serverID, uint32(port), server) //连接rpc
controller.Maincontroller.RPCClient = rpcClient //将RPC赋值Start
controller.Maincontroller.UID = uint32(100000*serverID + uint32(port)) //赋值服务器ID
controller.Init(true)
xmlres.Initfile()
server.Boot(serverID, gconv.Uint32(port))
}