forked from gonet2/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
317 lines (288 loc) · 7.72 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"encoding/binary"
"io"
"net"
"net/http"
_ "net/http/pprof"
"os"
"time"
. "agent/types"
"agent/utils"
log "github.com/Sirupsen/logrus"
"github.com/xtaci/kcp-go"
cli "gopkg.in/urfave/cli.v2"
)
type Config struct {
listen string
readDeadline time.Duration
sockbuf int
udp_sockbuf int
txqueuelen int
dscp int
sndwnd int
rcvwnd int
mtu int
nodelay, interval, resend, nc int
}
func main() {
log.SetLevel(log.DebugLevel)
// to catch all uncaught panic
defer utils.PrintPanicStack()
// open profiling
go http.ListenAndServe("0.0.0.0:6060", nil)
app := &cli.App{
Name: "agent",
Usage: "a gateway for games with stream multiplexing",
Version: "2.0",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen",
Value: ":8888",
Usage: "listening address:port",
},
&cli.StringSliceFlag{
Name: "etcd-hosts",
Value: cli.NewStringSlice("http://127.0.0.1:2379"),
Usage: "etcd hosts",
},
&cli.StringFlag{
Name: "etcd-root",
Value: "/backends",
Usage: "etcd root path",
},
&cli.StringSliceFlag{
Name: "services",
Value: cli.NewStringSlice("snowflake-10000", "game-10000"),
Usage: "auto-discovering services",
},
&cli.DurationFlag{
Name: "read-deadline",
Value: 15 * time.Second,
Usage: "per connection read timeout",
},
&cli.IntFlag{
Name: "txqueuelen",
Value: 128,
Usage: "per connection output message queue, packet will be dropped if exceeds",
},
&cli.IntFlag{
Name: "sockbuf",
Value: 32767,
Usage: "per connection tcp socket buffer",
},
&cli.IntFlag{
Name: "udp-sockbuf",
Value: 4194304,
Usage: "UDP listener socket buffer",
},
&cli.IntFlag{
Name: "udp-sndwnd",
Value: 32,
Usage: "per connection UDP send window",
},
&cli.IntFlag{
Name: "udp-rcvwnd",
Value: 32,
Usage: "per connection UDP recv window",
},
&cli.IntFlag{
Name: "udp-mtu",
Value: 1280,
Usage: "MTU of UDP packets, without IP(20) + UDP(8)",
},
&cli.IntFlag{
Name: "dscp",
Value: 46,
Usage: "set DSCP(6bit)",
},
&cli.IntFlag{
Name: "nodelay",
Value: 1,
Usage: "ikcp_nodelay()",
},
&cli.IntFlag{
Name: "interval",
Value: 20,
Usage: "ikcp_nodelay()",
},
&cli.IntFlag{
Name: "resend",
Value: 1,
Usage: "ikcp_nodelay()",
},
&cli.IntFlag{
Name: "nc",
Value: 1,
Usage: "ikcp_nodelay()",
},
&cli.IntFlag{
Name: "rpm-limit",
Value: 200,
Usage: "per connection rpm limit",
},
},
Action: func(c *cli.Context) error {
log.Println("listen:", c.String("listen"))
log.Println("etcd-hosts:", c.StringSlice("etcd-hosts"))
log.Println("etcd-root:", c.String("etcd-root"))
log.Println("services:", c.StringSlice("services"))
log.Println("read-deadline:", c.Duration("read-deadline"))
log.Println("txqueuelen:", c.Int("txqueuelen"))
log.Println("sockbuf:", c.Int("sockbuf"))
log.Println("udp-sockbuf:", c.Int("udp-sockbuf"))
log.Println("udp-sndwnd:", c.Int("udp-sndwnd"))
log.Println("udp-rcvwnd:", c.Int("udp-rcvwnd"))
log.Println("udp-mtu:", c.Int("udp-mtu"))
log.Println("dscp:", c.Int("dscp"))
log.Println("rpm-limit:", c.Int("rpm-limit"))
log.Println("nodelay parameters:", c.Int("nodelay"), c.Int("interval"), c.Int("resend"), c.Int("nc"))
//setup net param
config := &Config{
listen: c.String("listen"),
readDeadline: c.Duration("read-deadline"),
sockbuf: c.Int("sockbuf"),
udp_sockbuf: c.Int("udp-sockbuf"),
txqueuelen: c.Int("txqueuelen"),
dscp: c.Int("dscp"),
sndwnd: c.Int("udp-sndwnd"),
rcvwnd: c.Int("udp-rcvwnd"),
mtu: c.Int("udp-mtu"),
nodelay: c.Int("nodelay"),
interval: c.Int("interval"),
resend: c.Int("resend"),
nc: c.Int("nc"),
}
// init services
startup(c)
// init timer
initTimer(c.Int("rpm-limit"))
// listeners
go tcpServer(config)
go udpServer(config)
// wait forever
select {}
},
}
app.Run(os.Args)
}
func tcpServer(config *Config) {
// resolve address & start listening
tcpAddr, err := net.ResolveTCPAddr("tcp4", config.listen)
checkError(err)
listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
log.Info("listening on:", listener.Addr())
// loop accepting
for {
conn, err := listener.AcceptTCP()
if err != nil {
log.Warning("accept failed:", err)
continue
}
// set socket read buffer
conn.SetReadBuffer(config.sockbuf)
// set socket write buffer
conn.SetWriteBuffer(config.sockbuf)
// start a goroutine for every incoming connection for reading
go handleClient(conn, config)
}
}
func udpServer(config *Config) {
l, err := kcp.Listen(config.listen)
checkError(err)
log.Info("udp listening on:", l.Addr())
lis := l.(*kcp.Listener)
if err := lis.SetReadBuffer(config.sockbuf); err != nil {
log.Println("SetReadBuffer", err)
}
if err := lis.SetWriteBuffer(config.sockbuf); err != nil {
log.Println("SetWriteBuffer", err)
}
if err := lis.SetDSCP(config.dscp); err != nil {
log.Println("SetDSCP", err)
}
// loop accepting
for {
conn, err := lis.AcceptKCP()
if err != nil {
log.Warning("accept failed:", err)
continue
}
// set kcp parameters
conn.SetWindowSize(config.sndwnd, config.rcvwnd)
conn.SetNoDelay(config.nodelay, config.interval, config.resend, config.nc)
conn.SetStreamMode(true)
conn.SetMtu(config.mtu)
// start a goroutine for every incoming connection for reading
go handleClient(conn, config)
}
}
// PIPELINE #1: handleClient
// the goroutine is used for reading incoming PACKETS
// each packet is defined as :
// | 2B size | DATA |
//
func handleClient(conn net.Conn, config *Config) {
defer utils.PrintPanicStack()
defer conn.Close()
// for reading the 2-Byte header
header := make([]byte, 2)
// the input channel for agent()
in := make(chan []byte)
defer func() {
close(in) // session will close
}()
// create a new session object for the connection
// and record it's IP address
var sess Session
host, port, err := net.SplitHostPort(conn.RemoteAddr().String())
if err != nil {
log.Error("cannot get remote address:", err)
return
}
sess.IP = net.ParseIP(host)
log.Infof("new connection from:%v port:%v", host, port)
// session die signal, will be triggered by agent()
sess.Die = make(chan struct{})
// create a write buffer
out := new_buffer(conn, sess.Die, config.txqueuelen)
go out.start()
// start agent for PACKET processing
wg.Add(1)
go agent(&sess, in, out)
// read loop
for {
// solve dead link problem:
// physical disconnection without any communcation between client and server
// will cause the read to block FOREVER, so a timeout is a rescue.
conn.SetReadDeadline(time.Now().Add(config.readDeadline))
// read 2B header
n, err := io.ReadFull(conn, header)
if err != nil {
log.Warningf("read header failed, ip:%v reason:%v size:%v", sess.IP, err, n)
return
}
size := binary.BigEndian.Uint16(header)
// alloc a byte slice of the size defined in the header for reading data
payload := make([]byte, size)
n, err = io.ReadFull(conn, payload)
if err != nil {
log.Warningf("read payload failed, ip:%v reason:%v size:%v", sess.IP, err, n)
return
}
// deliver the data to the input queue of agent()
select {
case in <- payload: // payload queued
case <-sess.Die:
log.Warningf("connection closed by logic, flag:%v ip:%v", sess.Flag, sess.IP)
return
}
}
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
os.Exit(-1)
}
}