2025-12-11 10:32:39 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"blazing/common/data/xmlres"
|
|
|
|
|
|
"blazing/common/rpc"
|
|
|
|
|
|
"blazing/common/socket"
|
2026-01-08 03:30:18 +08:00
|
|
|
|
"blazing/cool"
|
2026-03-20 04:58:23 +08:00
|
|
|
|
"context"
|
2025-12-11 10:32:39 +08:00
|
|
|
|
|
|
|
|
|
|
"blazing/logic/controller"
|
|
|
|
|
|
|
2026-01-09 19:58:12 +08:00
|
|
|
|
config "blazing/modules/config/service"
|
2025-12-11 10:32:39 +08:00
|
|
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"net"
|
|
|
|
|
|
|
2026-01-01 22:19:00 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
2025-12-11 10:32:39 +08:00
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
minRandomPort = 10000
|
|
|
|
|
|
maxRandomPort = 60000
|
|
|
|
|
|
maxPortRetryCount = 5
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// determinePort 确定服务器使用的端口
|
2026-01-01 19:57:39 +08:00
|
|
|
|
func determinePort(ports []uint32) (int, error) {
|
2025-12-11 10:32:39 +08:00
|
|
|
|
// 尝试从指定端口列表中找可用端口,最多尝试maxPortRetryCount轮
|
|
|
|
|
|
for i := 0; i < maxPortRetryCount; i++ {
|
|
|
|
|
|
// 遍历指定的端口列表
|
2026-01-01 19:57:39 +08:00
|
|
|
|
for _, port := range ports {
|
2025-12-11 10:32:39 +08:00
|
|
|
|
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 检查端口是否可用
|
2026-01-01 19:57:39 +08:00
|
|
|
|
func isPortAvailable(port uint32) bool {
|
2025-12-11 10:32:39 +08:00
|
|
|
|
address := fmt.Sprintf(":%d", port)
|
|
|
|
|
|
listener, err := net.Listen("tcp", address)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
defer listener.Close()
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-25 12:21:15 +08:00
|
|
|
|
// Start 启动服务器
|
2025-12-11 10:32:39 +08:00
|
|
|
|
// 如果id是0,那就是login server
|
2026-01-08 03:30:18 +08:00
|
|
|
|
func Start() {
|
|
|
|
|
|
serverID := cool.Config.GameOnlineID
|
2026-01-09 19:58:12 +08:00
|
|
|
|
cool.Config.ServerInfo = config.NewServerService().GetServerID(serverID).ServerList
|
2026-02-14 11:51:34 +08:00
|
|
|
|
// if cool.Config.ServerInfo.IsVip != 0 {
|
|
|
|
|
|
// g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(cool.Redis)) //设置数据库
|
|
|
|
|
|
// }
|
2026-03-20 04:58:23 +08:00
|
|
|
|
go rpc.ListenFight(context.Background())
|
2026-01-10 02:01:17 +08:00
|
|
|
|
if cool.Config.ServerInfo.IsDebug == 1 {
|
2026-01-01 22:19:00 +08:00
|
|
|
|
g.DB().SetDebug(true)
|
2026-03-02 23:59:15 +08:00
|
|
|
|
|
2026-01-01 22:19:00 +08:00
|
|
|
|
}
|
2026-03-29 02:11:21 +08:00
|
|
|
|
go PprofWeb()
|
2026-01-08 03:30:18 +08:00
|
|
|
|
port, err := determinePort(cool.Config.ServerInfo.CanPort)
|
2026-03-02 18:34:20 +08:00
|
|
|
|
cool.Config.ServerInfo.Port = uint32(port)
|
2025-12-11 10:32:39 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
log.Fatalf("Failed to determine port: %v", err)
|
|
|
|
|
|
}
|
2025-12-25 12:21:15 +08:00
|
|
|
|
server := socket.NewServer(
|
2025-12-11 10:32:39 +08:00
|
|
|
|
socket.WithCORS(),
|
|
|
|
|
|
socket.WithPort(port),
|
|
|
|
|
|
)
|
2026-01-20 16:59:23 +08:00
|
|
|
|
|
2026-03-02 18:34:20 +08:00
|
|
|
|
rpcClient := rpc.StartClient(serverID, uint32(port), server) //连接rpc
|
2025-12-11 10:32:39 +08:00
|
|
|
|
|
2026-02-07 22:54:44 +08:00
|
|
|
|
controller.Maincontroller.RPCClient = rpcClient //将RPC赋值Start
|
2026-03-02 23:59:15 +08:00
|
|
|
|
|
2026-03-20 04:58:23 +08:00
|
|
|
|
controller.Maincontroller.UID = gconv.Uint32(cool.Config.ServerInfo.GetID()) //赋值服务器ID
|
2025-12-11 10:32:39 +08:00
|
|
|
|
controller.Init(true)
|
|
|
|
|
|
xmlres.Initfile()
|
2026-01-09 19:58:12 +08:00
|
|
|
|
|
2026-03-02 18:34:20 +08:00
|
|
|
|
server.Boot(serverID, gconv.Uint32(port))
|
2026-01-01 19:57:39 +08:00
|
|
|
|
}
|