/
config.go
172 lines (155 loc) · 3.98 KB
/
config.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// config
package netlib
import (
"time"
"github.com/breezedup/goserver.v3/core"
"github.com/breezedup/goserver.v3/core/logger"
)
var Config = Configuration{}
type Configuration struct {
SrvInfo ServerInfo
IoServices []SessionConfig
}
type ServerInfo struct {
Name string
Type int
Id int
AreaID int
Data string
Banner []string
}
type SessionConfig struct {
Id int
Type int
AreaId int
Protocol string //"tcp" "ws" "wss"
Name string
Ip string
Path string //path of "ws"
CertFile string
KeyFile string
OuterIp string //对外的IP
Port int
MaxDone int
MaxPend int
MaxPacket int
MaxConn int
ExtraConn int
RcvBuff int
SndBuff int
SoLinger int
MTU int
WriteTimeout time.Duration
ReadTimeout time.Duration
IdleTimeout time.Duration
KeepAlive bool
KeepAlivePeriod time.Duration
KeepAliveIdle time.Duration
KeepAliveCount int
NoDelay bool
IsClient bool
IsAutoReconn bool
IsInnerLink bool
AuthKey string //Authentication Key
EncoderName string //ProtocolEncoder name
DecoderName string //ProtocolDecoder name
ErrorPacketHandlerName string
FilterChain []string
HandlerChain []string
SupportFragment bool
AllowMultiConn bool
encoder ProtocolEncoder
decoder ProtocolDecoder
sfc *SessionFilterChain
shc *SessionHandlerChain
eph ErrorPacketHandler
}
func (c *Configuration) Name() string {
return "netlib"
}
func (c *Configuration) Init() error {
for _, str := range c.SrvInfo.Banner {
logger.Logger.Info(str)
}
for i := 0; i < len(c.IoServices); i++ {
c.IoServices[i].Init()
}
return nil
}
func (c *Configuration) Close() error {
return nil
}
func (sc *SessionConfig) Init() {
if sc.EncoderName == "" {
sc.encoder = GetProtocolEncoder(DefaultProtocolEncoderName)
} else {
sc.encoder = GetProtocolEncoder(sc.EncoderName)
}
if sc.DecoderName == "" {
sc.decoder = GetProtocolDecoder(DefaultProtocoDecoderName)
} else {
sc.decoder = GetProtocolDecoder(sc.DecoderName)
}
for i := 0; i < len(sc.FilterChain); i++ {
creator := GetSessionFilterCreator(sc.FilterChain[i])
if creator != nil {
if sc.sfc == nil {
sc.sfc = NewSessionFilterChain()
}
if sc.sfc != nil {
sc.sfc.AddLast(creator())
}
}
}
for i := 0; i < len(sc.HandlerChain); i++ {
creator := GetSessionHandlerCreator(sc.HandlerChain[i])
if creator != nil {
if sc.shc == nil {
sc.shc = NewSessionHandlerChain()
}
if sc.shc != nil {
sc.shc.AddLast(creator())
}
}
}
if sc.ErrorPacketHandlerName != "" {
creator := GetErrorPacketHandlerCreator(sc.ErrorPacketHandlerName)
if creator != nil {
sc.eph = creator()
} else {
logger.Logger.Warnf("[%v] ErrorPacketHandler not registe", sc.ErrorPacketHandlerName)
}
}
if sc.IdleTimeout <= 0 {
sc.IdleTimeout = 5 * time.Second
} else {
sc.IdleTimeout = sc.IdleTimeout * time.Second
}
if sc.WriteTimeout <= 0 {
sc.WriteTimeout = 30 * time.Second
} else {
sc.WriteTimeout = sc.WriteTimeout * time.Second
}
if sc.ReadTimeout <= 0 {
sc.ReadTimeout = 30 * time.Second
} else {
sc.ReadTimeout = sc.ReadTimeout * time.Second
}
sc.KeepAlivePeriod *= time.Second
sc.KeepAliveIdle *= time.Second
}
func (sc *SessionConfig) GetFilter(name string) SessionFilter {
if sc.sfc != nil {
return sc.sfc.GetFilter(name)
}
return nil
}
func (sc *SessionConfig) GetHandler(name string) SessionHandler {
if sc.shc != nil {
return sc.shc.GetHandler(name)
}
return nil
}
func init() {
core.RegistePackage(&Config)
}