-
Notifications
You must be signed in to change notification settings - Fork 194
/
server_std.go
443 lines (359 loc) · 8.72 KB
/
server_std.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//go:build windows
// +build windows
package gev
import (
"errors"
"io"
"net"
stdsync "sync"
at "sync/atomic"
"time"
"github.com/Allenxuxu/gev/log"
"github.com/Allenxuxu/gev/poller"
"github.com/Allenxuxu/ringbuffer"
"github.com/Allenxuxu/toolkit/sync"
"github.com/Allenxuxu/toolkit/sync/atomic"
"github.com/RussellLuo/timingwheel"
)
// Handler Server 注册接口
type Handler interface {
CallBack
OnConnect(c *Connection)
}
// Server gev Server
type Server struct {
listener net.Listener
callback Handler
connections stdsync.Map
timingWheel *timingwheel.TimingWheel
opts *Options
running atomic.Bool
dying chan struct{}
}
// NewServer 创建 Server
func NewServer(handler Handler, opts ...Option) (server *Server, err error) {
if handler == nil {
return nil, errors.New("handler is nil")
}
options := newOptions(opts...)
server = new(Server)
server.dying = make(chan struct{})
server.callback = handler
server.opts = options
server.timingWheel = timingwheel.NewTimingWheel(server.opts.tick, server.opts.wheelSize)
server.listener, err = net.Listen(server.opts.Network, server.opts.Address)
if err != nil {
return nil, err
}
return
}
// RunAfter 延时任务
func (s *Server) RunAfter(d time.Duration, f func()) *timingwheel.Timer {
return s.timingWheel.AfterFunc(d, f)
}
// RunEvery 定时任务
func (s *Server) RunEvery(d time.Duration, f func()) *timingwheel.Timer {
return s.timingWheel.ScheduleFunc(&everyScheduler{Interval: d}, f)
}
// Start 启动 Server
func (s *Server) Start() {
sw := sync.WaitGroupWrapper{}
s.timingWheel.Start()
if s.opts.NumLoops <= 0 {
s.opts.NumLoops = 1
}
for i := 0; i < s.opts.NumLoops; i++ {
sw.AddAndRun(func() {
for {
select {
case <-s.dying:
return
default:
conn, err := s.listener.Accept()
if err != nil {
log.Errorf("accept error: %v", err)
continue
}
connection := NewConnection(conn, s.opts.Protocol, s.timingWheel, s.opts.IdleTime, s.callback)
s.connections.Store(connection, struct{}{})
sw.AddAndRun(func() {
connection.readLoop()
})
sw.AddAndRun(func() {
connection.writeLoop()
})
sw.AddAndRun(func() {
s.callback.OnConnect(connection)
})
}
}
})
}
s.running.Set(true)
log.Infof("server run in windows")
sw.Wait()
}
// Stop 关闭 Server
func (s *Server) Stop() {
if s.running.Get() {
close(s.dying)
s.running.Set(false)
s.timingWheel.Stop()
if err := s.listener.Close(); err != nil {
log.Error(err)
}
s.connections.Range(func(key, value interface{}) bool {
c := key.(*Connection)
c.Close()
return true
})
}
}
// Options 返回 options
func (s *Server) Options() Options {
return *s.opts
}
// connection
type CallBack interface {
OnMessage(c *Connection, ctx interface{}, data []byte) interface{}
OnClose(c *Connection)
}
// Connection TCP 连接
type Connection struct {
conn net.Conn
connected atomic.Bool
dying chan struct{}
userBuffer *[]byte
buffer *ringbuffer.RingBuffer
outBuffer *ringbuffer.RingBuffer // write buffer
inBuffer *ringbuffer.RingBuffer // read buffer
outBufferLen atomic.Int64
inBufferLen atomic.Int64
callBack CallBack
ctx interface{}
KeyValueContext
mu stdsync.Mutex
taskQueueW []func()
taskQueueR []func()
idleTime time.Duration
activeTime atomic.Int64
timingWheel *timingwheel.TimingWheel
timer at.Value
protocol Protocol
}
var ErrConnectionClosed = errors.New("connection closed")
// NewConnection 创建 Connection
func NewConnection(
conn net.Conn,
protocol Protocol,
tw *timingwheel.TimingWheel,
idleTime time.Duration,
callBack CallBack) *Connection {
userBuffer := make([]byte, 4096)
connection := &Connection{
conn: conn,
dying: make(chan struct{}),
outBuffer: ringbuffer.GetFromPool(),
inBuffer: ringbuffer.GetFromPool(),
callBack: callBack,
idleTime: idleTime,
timingWheel: tw,
protocol: protocol,
buffer: ringbuffer.New(0),
taskQueueW: make([]func(), 0, 1024),
taskQueueR: make([]func(), 0, 1024),
userBuffer: &userBuffer,
}
connection.connected.Set(true)
if connection.idleTime > 0 {
_ = connection.activeTime.Swap(time.Now().Unix())
timer := connection.timingWheel.AfterFunc(connection.idleTime, connection.closeTimeoutConn())
connection.timer.Store(timer)
}
return connection
}
func (c *Connection) UserBuffer() *[]byte {
return c.userBuffer
}
// Context 获取 Context
func (c *Connection) Context() interface{} {
return c.ctx
}
// SetContext 设置 Context
func (c *Connection) SetContext(ctx interface{}) {
c.ctx = ctx
}
// PeerAddr 获取客户端地址信息
func (c *Connection) PeerAddr() string {
return c.conn.RemoteAddr().String()
}
// Connected 是否已连接
func (c *Connection) Connected() bool {
return c.connected.Get()
}
// Send 用来在非 loop 协程发送
func (c *Connection) Send(data interface{}, opts ...ConnectionOption) error {
if !c.connected.Get() {
return ErrConnectionClosed
}
opt := ConnectionOptions{}
for _, o := range opts {
o(&opt)
}
f := func() {
if c.connected.Get() {
c.sendInLoop(c.protocol.Packet(c, data))
if opt.sendInLoopFinish != nil {
opt.sendInLoopFinish(data)
}
}
}
c.mu.Lock()
c.taskQueueW = append(c.taskQueueW, f)
c.mu.Unlock()
return nil
}
// Close 关闭连接
func (c *Connection) Close() error {
if c.connected.Get() {
close(c.dying)
c.connected.Set(false)
c.callBack.OnClose(c)
if v := c.timer.Load(); v != nil {
timer := v.(*timingwheel.Timer)
timer.Stop()
}
return c.conn.Close()
}
return nil
}
// ShutdownWrite 关闭可写端,等待读取完接收缓冲区所有数据
func (c *Connection) ShutdownWrite() error {
return c.Close()
}
// ReadBufferLength read buffer 当前积压的数据长度
func (c *Connection) ReadBufferLength() int64 {
return c.inBufferLen.Get()
}
// WriteBufferLength write buffer 当前积压的数据长度
func (c *Connection) WriteBufferLength() int64 {
return c.outBufferLen.Get()
}
// HandleEvent 内部使用,event loop 回调
func (c *Connection) HandleEvent(fd int, events poller.Event) {
}
func (c *Connection) readLoop() {
buf := make([]byte, 0, 66635)
for {
select {
case <-c.dying:
return
default:
n, err := c.conn.Read(buf)
if err != nil {
if err != io.EOF {
log.Info("read error: ", err)
}
c.Close()
return
}
_, _ = c.inBuffer.Write(buf[:n])
buf = buf[:0]
c.handlerProtocol(&buf, c.inBuffer)
if len(buf) != 0 {
tmp := make([]byte, len(buf))
copy(tmp, buf)
_ = c.Send(tmp)
}
buf = buf[:cap(buf)]
if c.idleTime > 0 {
_ = c.activeTime.Swap(time.Now().Unix())
}
c.inBufferLen.Swap(int64(c.inBuffer.Length()))
}
}
}
func (c *Connection) writeLoop() {
for {
select {
case <-c.dying:
return
default:
c.doPendingFunc()
if c.outBuffer.IsEmpty() {
continue
}
first, end := c.outBuffer.PeekAll()
n, err := c.conn.Write(first)
if err != nil {
log.Error("Write error: ", err)
c.Close()
return
}
c.outBuffer.Retrieve(n)
if n == len(first) && len(end) > 0 {
n, err = c.conn.Write(end)
if err != nil {
log.Error("Write error: ", err)
c.Close()
return
}
c.outBuffer.Retrieve(n)
}
if c.idleTime > 0 {
_ = c.activeTime.Swap(time.Now().Unix())
}
c.outBufferLen.Swap(int64(c.outBuffer.Length()))
}
}
}
func (c *Connection) doPendingFunc() {
c.mu.Lock()
c.taskQueueW, c.taskQueueR = c.taskQueueR, c.taskQueueW
c.mu.Unlock()
length := len(c.taskQueueR)
for i := 0; i < length; i++ {
c.taskQueueR[i]()
}
c.taskQueueR = c.taskQueueR[:0]
}
func (c *Connection) sendInLoop(data []byte) (closed bool) {
if !c.outBuffer.IsEmpty() {
_, _ = c.outBuffer.Write(data)
} else {
n, err := c.conn.Write(data)
if err != nil {
log.Error("Write error: ", err)
c.Close()
return true
}
if n <= 0 {
_, _ = c.outBuffer.Write(data)
} else if n < len(data) {
_, _ = c.outBuffer.Write(data[n:])
}
}
return false
}
func (c *Connection) handlerProtocol(tmpBuffer *[]byte, buffer *ringbuffer.RingBuffer) {
ctx, receivedData := c.protocol.UnPacket(c, buffer)
for ctx != nil || len(receivedData) != 0 {
sendData := c.callBack.OnMessage(c, ctx, receivedData)
if sendData != nil {
*tmpBuffer = append(*tmpBuffer, c.protocol.Packet(c, sendData)...)
}
ctx, receivedData = c.protocol.UnPacket(c, buffer)
}
}
func (c *Connection) closeTimeoutConn() func() {
return func() {
now := time.Now()
intervals := now.Sub(time.Unix(c.activeTime.Get(), 0))
if intervals >= c.idleTime {
_ = c.Close()
} else {
timer := c.timingWheel.AfterFunc(c.idleTime-intervals, c.closeTimeoutConn())
c.timer.Store(timer)
}
}
}