```
feat(config): 添加RPC端口和游戏端口配置项 在配置结构体中新增了RPC端口和游戏端口字段,并更新了配置文件以支持多端口监听。同时, 调整了服务器端口选择逻辑,优先使用默认端口,不再生成随机端口。 refactor(server): 移除随机端口生成逻辑 删除了随机端口生成及相关依赖,端口选择改为从预定义列表中查找可用端口,提高端口分配的 可控性和稳定性。 fix(fight): 注释掉未实现的panic语句 为避免程序运行时因未实现逻辑导致崩溃,注释掉了Turn_Start和Fight_Start中的panic调用, 并添加了相关注释说明后续需实现的内容。 chore(config): 更新默认配置参数 更新了服务器地址、端口、数据库连接信息及Redis配置,确保开发环境配置正确。 ```
This commit is contained in:
@@ -12,9 +12,7 @@ import (
|
||||
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
@@ -26,28 +24,28 @@ const (
|
||||
)
|
||||
|
||||
var defaultPort = gconv.Int(cool.Config.Port) //读入默认的端口
|
||||
var candidatePorts = []int{defaultPort}
|
||||
|
||||
// determinePort 确定服务器使用的端口
|
||||
func determinePort(serverid uint16) (int, error) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
// 服务器ID为0时使用默认端口
|
||||
if serverid == 0 {
|
||||
return defaultPort, nil
|
||||
}
|
||||
|
||||
// 尝试从指定端口列表中找可用端口,最多尝试maxPortRetryCount轮
|
||||
for i := 0; i < maxPortRetryCount; i++ {
|
||||
port := generateRandomPort()
|
||||
if isPortAvailable(port) {
|
||||
return port, nil
|
||||
// 遍历指定的端口列表
|
||||
for _, port := range candidatePorts {
|
||||
if isPortAvailable(port) {
|
||||
return port, nil
|
||||
}
|
||||
log.Printf("Port %d is not available, checking next...", port)
|
||||
}
|
||||
log.Printf("Port %d is not available, retrying...", 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 attempts", maxPortRetryCount)
|
||||
}
|
||||
|
||||
// generateRandomPort 生成指定范围内的随机端口
|
||||
func generateRandomPort() int {
|
||||
return minRandomPort + rand.Intn(maxRandomPort-minRandomPort)
|
||||
return 0, fmt.Errorf("failed to find available port after %d rounds of checking", maxPortRetryCount)
|
||||
}
|
||||
|
||||
// isPortAvailable 检查端口是否可用
|
||||
|
||||
Reference in New Issue
Block a user