114 lines
2.6 KiB
Go
114 lines
2.6 KiB
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
_ "net/http/pprof"
|
||
"os"
|
||
"os/signal"
|
||
"runtime"
|
||
"runtime/debug"
|
||
"server/MergeConst"
|
||
"server/backend"
|
||
mergeCluster "server/cluster"
|
||
"server/conf"
|
||
"server/game"
|
||
GoUtil "server/game_util"
|
||
"server/gamedata"
|
||
"server/gate"
|
||
"syscall"
|
||
"time"
|
||
|
||
"gitea.bywaystudios.com/pet_home/leaf/cluster"
|
||
lconf "gitea.bywaystudios.com/pet_home/leaf/conf"
|
||
"gitea.bywaystudios.com/pet_home/leaf/console"
|
||
"gitea.bywaystudios.com/pet_home/leaf/log"
|
||
"gitea.bywaystudios.com/pet_home/leaf/module"
|
||
)
|
||
|
||
func main() {
|
||
// 设置使用所有 CPU 核心
|
||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||
|
||
lconf.LogLevel = conf.Server.LogLevel
|
||
lconf.LogPath = conf.Server.LogPath
|
||
lconf.LogFlag = conf.LogFlag
|
||
lconf.ConsolePort = conf.Server.ConsolePort
|
||
lconf.ProfilePath = conf.Server.ProfilePath
|
||
|
||
lconf.ListenAddr = conf.Server.ListenAddr
|
||
lconf.CenterAddr = conf.Server.CenterAddr
|
||
lconf.PendingWriteNum = conf.PendingWriteNum
|
||
// 当内存>256M时开始GC
|
||
debug.SetGCPercent(MergeConst.Go_gc_percent)
|
||
debug.SetMemoryLimit(MergeConst.Go_gc_memory_limit)
|
||
// 启动 pprof(仅绑定本地)
|
||
go func() {
|
||
// 如果需要绑定所有接口改为 ":6060"
|
||
port := 6060 + conf.Server.ServerID
|
||
_ = http.ListenAndServe(fmt.Sprintf("127.0.0.1:%d", port), nil)
|
||
}()
|
||
|
||
run(
|
||
game.Module,
|
||
gate.Module,
|
||
)
|
||
}
|
||
|
||
func run(mods ...module.Module) {
|
||
//logger
|
||
if lconf.LogLevel != "" {
|
||
if _, err := os.Stat(lconf.LogPath); os.IsNotExist(err) {
|
||
os.Mkdir(lconf.LogPath, os.ModePerm)
|
||
}
|
||
zero := GoUtil.ZeroTimestamp()
|
||
now := time.Unix(zero, 0).Local()
|
||
err := log.NewDailyLog(now, log.DEBUG_LEVEL, lconf.LogPath, conf.LogFlag)
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
}
|
||
log.Release("服务器版本: %s", MergeConst.Go_game_version)
|
||
log.Release("%s 启动, 节点类型: %s, 区服id: %d", conf.Server.GameName, conf.Server.ServerType, conf.Server.ServerID)
|
||
go backend.Start()
|
||
// module
|
||
for i := 0; i < len(mods); i++ {
|
||
module.Register(mods[i])
|
||
}
|
||
module.Init()
|
||
|
||
// cluster
|
||
mergeCluster.Init()
|
||
|
||
// console
|
||
console.Init()
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
log.Debug("Leaf console error: %v", r)
|
||
log.Debug("Leaf closing down\n")
|
||
console.Destroy()
|
||
cluster.Destroy()
|
||
module.Destroy()
|
||
game.Destroy()
|
||
os.Exit(1)
|
||
}
|
||
}()
|
||
// close
|
||
c := make(chan os.Signal, 1)
|
||
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
|
||
for {
|
||
sig := <-c
|
||
if sig == syscall.SIGINT {
|
||
gamedata.Reload()
|
||
continue
|
||
}
|
||
log.Debug("Leaf closing down (signal: %v)\n", sig.String())
|
||
console.Destroy()
|
||
cluster.Destroy()
|
||
module.Destroy()
|
||
game.Destroy()
|
||
os.Exit(1)
|
||
}
|
||
|
||
}
|