forked from Golangltd/lockstepserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
140 lines (111 loc) · 2.84 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
//-----------------------------------
//File:main.go
//Date:2018年8月23日
//Desc:帧同步战斗服务器
//Auth:Bruce
//-----------------------------------
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bailu1901/lockstepserver/kcp_server"
"github.com/bailu1901/lockstepserver/protocol"
"github.com/bailu1901/lockstepserver/room"
_ "github.com/bailu1901/lockstepserver/web"
"github.com/bailu1901/lockstepserver/config"
"github.com/bailu1901/lockstepserver/util"
l4g "github.com/alecthomas/log4go"
)
var (
nodeId = flag.Uint64("id", 0, "id")
configFile = flag.String("config", "config.xml", "config file")
gWeb = flag.String("web", ":10002", "web listen address")
outAddress = flag.String("out", ":10086", "out listen address(':10086' means use $localip:10086)")
)
//LoadConfig 加载配置
func LoadConfig() bool {
if err := config.LoadConfig(*configFile); err != nil {
panic(fmt.Sprintf("[main] load config %v fail: %v", *configFile, err))
}
config.Cfg.OutAddress = *outAddress
temp := strings.Split(config.Cfg.OutAddress, ":")
if 0 == len(temp[0]) {
config.Cfg.OutAddress = util.GetOutboundIP().String() + config.Cfg.OutAddress
}
return true
}
//Init 初始化
func Init() bool {
if len(*gWeb) > 0 {
go func() {
e := http.ListenAndServe(*gWeb, nil)
if nil != e {
panic(e)
}
}()
l4g.Info("[main] http.ListenAndServe port=[%s]", *gWeb)
}
return true
}
//Run 运行
func Run() {
defer func() {
//clear
time.Sleep(time.Millisecond * 100)
l4g.Warn("[main] pvp %d quit", *nodeId)
l4g.Global.Close()
}()
defer room.Stop()
//udp server
networkServer, err := kcp_server.ListenAndServe(config.Cfg.OutAddress, &room.Router{}, &protocol.MsgProtocol{})
if nil != err {
panic(err)
}
l4g.Info("[main] kcp.Listen addr=[%s]", config.Cfg.OutAddress)
defer networkServer.Stop()
l4g.Info("[main] cluster start! etcd=[%s] key=[%s]", config.Cfg.EtcdEndPionts, config.Cfg.EtcdKey)
//主循环定时器
ticker := time.NewTimer(time.Second)
defer ticker.Stop()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, os.Interrupt)
l4g.Warn("[main] %d running...", *nodeId)
//主循环
QUIT:
for {
select {
case sig := <-sigs:
l4g.Info("Signal: %s", sig.String())
if sig == syscall.SIGHUP {
} else {
break QUIT
}
case <-ticker.C:
//break QUIT
}
}
l4g.Info("[main] pvp %d quiting...", *nodeId)
}
func main() {
showIP := false
flag.BoolVar(&showIP, "ip", false, "show ip info")
flag.Parse()
if showIP {
fmt.Println("GetOutboundIP", util.GetOutboundIP())
fmt.Println("GetLocalIP", util.GetLocalIP())
fmt.Println("GetExternalIP", util.GetExternalIP())
os.Exit(0)
}
if LoadConfig() && Init() {
Run()
} else {
fmt.Printf("[main] launch fail")
}
}