-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.go
120 lines (100 loc) · 2.78 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
//
// date : 2014-05-23 17:35
// author: xjdrew
//
package main
import (
"flag"
"fmt"
"net"
"os"
"sync"
"github.com/spf13/viper"
"github.com/xjdrew/glog"
)
// log rule:
// verbose:
// 0, default, 确定数量的日志
// 1, connection related, 跟连接相关的日志, 一般每条连接在生命周期内产生x条
// 2, packet releated, 跟包相关的日志,一般每个packet都会产生数条日志
// <2019-10-18> 1.0.0: 重写goscon的配置方式
// <2019-12-24> 1.1.0: 优化内部设计,提升kcp可靠性等
var _Version = "1.1.0"
func testConfigFile(filename string) error {
viper.SetConfigFile(filename)
return viper.ReadInConfig()
}
func main() {
// set default log directory
flag.Set("log_dir", "./")
showVersion := flag.Bool("version", false, "show version and exit")
testConfig := flag.Bool("t", false, "test configuration and exit")
dumpConfig := flag.Bool("T", false, "test configuration, dump it and exit")
configFile := flag.String("config", "./config.yaml", "set configuration file")
flag.Parse()
if *showVersion {
fmt.Printf("goscon version: goscon/%s\n", _Version)
os.Exit(0)
}
if *testConfig || *dumpConfig {
if err := testConfigFile(*configFile); err != nil {
fmt.Printf("read configuration file %s faield: %s\n", *configFile, err.Error())
os.Exit(1)
}
fmt.Printf("the configuration file %s syntax is ok\n", *configFile)
if *dumpConfig {
fmt.Println(marshalConfigFile())
}
os.Exit(0)
}
viper.SetConfigFile(*configFile)
if err := reloadConfig(); err != nil {
os.Exit(1)
}
if err := startManager(viper.GetString("manager")); err != nil {
glog.Errorf("start manager failed: err=%s", err.Error())
os.Exit(1)
}
var wg sync.WaitGroup
// listen
tcpListen := viper.GetString("tcp")
if tcpListen != "" {
l, err := NewTCPListener(tcpListen)
if err != nil {
glog.Errorf("tcp listen failed: addr=%s, err=%s", tcpListen, err.Error())
os.Exit(1)
}
glog.Infof("tcp listen start: addr=%s", tcpListen)
wg.Add(1)
go func(l net.Listener) {
defer l.Close()
defer wg.Done()
err := defaultServer.Serve(l)
glog.Errorf("tcp listen stop: addr=%s, err=%s", tcpListen, err.Error())
}(l)
}
kcpListen := viper.GetString("kcp")
if kcpListen != "" {
reuseport := viper.GetInt("kcp_option.reuseport")
if reuseport <= 0 {
reuseport = 1
}
for i := 0; i < reuseport; i++ {
l, err := NewKCPListener(kcpListen)
if err != nil {
glog.Errorf("kcp listen failed: addr=%s, err=%s", kcpListen, err.Error())
os.Exit(1)
}
glog.Infof("kcp listen start: addr=%s", kcpListen)
wg.Add(1)
go func(l net.Listener) {
defer l.Close()
defer wg.Done()
err := defaultServer.Serve(l)
glog.Errorf("kcp listen stop: addr=%s, err=%s", tcpListen, err.Error())
}(l)
}
}
wg.Wait()
glog.Flush()
}