Files
bl/logic/server.go
昔念 dd28d48ca4 ```
feat(build): 更新构建脚本添加资源打包和proto编译

更新build.bat脚本,添加proto文件编译和资源打包功能,调整资源打包顺序。

BREAKING CHANGE: 构建流程发生变化,需要重新生成proto文件和打包资源。

---

refactor(xmlres): 使用gres替换gfile读取资源文件

将xmlres模块中文件读取方式从gfile.GetBytes改为gres.GetContent,
使
2026-01-01 19:57:39 +08:00

76 lines
1.8 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/logic/controller"
blservice "blazing/modules/blazing/service"
"fmt"
"log"
"net"
"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 uint16) {
// 确定端口
r := blservice.NewLoginServiceService().GetServerID(serverID)
port, err := determinePort(r.CanPort)
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()
blservice.NewLoginServiceService().SetServerID(serverID, gconv.Uint16(port))
server.Boot()
}