feat(config): 更新服务器配置字段注释并修复VIP标识逻辑 - 修改config.go中IsVip字段注释,明确其表示测试服状态 - 添加isdebug字段注释说明本地服标识 - 从.gitignore添加login-login-linux-amd64到忽略列表 - 移除已废弃的coolconfig.SetTest函数 fix(item_buy): 注释掉金币购买功能代码 - 将BuyGoldItem方法注释掉,暂时禁用金币购买商品功能 - 移除未使用的gconv导入包 fix(server): 修正调试模式判断条件 - 将server.go中的IsVip判断改为IsDebug,确保调试模式正确启用 refactor(item_service): 优化模型调用并添加VIP标识 - 修复ItemService.UPDATE方法中模型调用的一致性问题 - 添加is_vip字段到数据记录中用于区分服务器类型 feat(pet_service): 为宠物数据添加VIP标识 - 在宠物服务中为新捕捉的宠物添加IsVip字段设置 ```
85 lines
2.0 KiB
Go
85 lines
2.0 KiB
Go
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.IsDebug == 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()
|
||
}
|