1
+ //go:build windows
1
2
// +build windows
2
3
3
4
package gev
@@ -28,7 +29,7 @@ type Handler interface {
28
29
type Server struct {
29
30
listener net.Listener
30
31
callback Handler
31
- connections [] * Connection
32
+ connections stdsync. Map
32
33
33
34
timingWheel * timingwheel.TimingWheel
34
35
opts * Options
@@ -71,33 +72,39 @@ func (s *Server) Start() {
71
72
sw := sync.WaitGroupWrapper {}
72
73
s .timingWheel .Start ()
73
74
74
- sw .AddAndRun (func () {
75
- for {
76
- select {
77
- case <- s .dying :
78
- return
75
+ if s .opts .NumLoops <= 0 {
76
+ s .opts .NumLoops = 1
77
+ }
78
+ for i := 0 ; i < s .opts .NumLoops ; i ++ {
79
+ sw .AddAndRun (func () {
80
+ for {
81
+ select {
82
+ case <- s .dying :
83
+ return
79
84
80
- default :
81
- conn , err := s .listener .Accept ()
82
- if err != nil {
83
- log .Errorf ("accept error: %v" , err )
84
- continue
85
+ default :
86
+ conn , err := s .listener .Accept ()
87
+ if err != nil {
88
+ log .Errorf ("accept error: %v" , err )
89
+ continue
90
+ }
91
+
92
+ connection := NewConnection (conn , s .opts .Protocol , s .timingWheel , s .opts .IdleTime , s .callback )
93
+ s .connections .Store (connection , struct {}{})
94
+
95
+ sw .AddAndRun (func () {
96
+ connection .readLoop ()
97
+ })
98
+ sw .AddAndRun (func () {
99
+ connection .writeLoop ()
100
+ })
101
+ sw .AddAndRun (func () {
102
+ s .callback .OnConnect (connection )
103
+ })
85
104
}
86
-
87
- connection := NewConnection (conn , s .opts .Protocol , s .timingWheel , s .opts .IdleTime , s .callback )
88
- s .connections = append (s .connections , connection )
89
-
90
- s .callback .OnConnect (connection )
91
-
92
- sw .AddAndRun (func () {
93
- connection .readLoop ()
94
- })
95
- sw .AddAndRun (func () {
96
- connection .writeLoop ()
97
- })
98
105
}
99
- }
100
- })
106
+ })
107
+ }
101
108
102
109
s .running .Set (true )
103
110
@@ -116,9 +123,13 @@ func (s *Server) Stop() {
116
123
log .Error (err )
117
124
}
118
125
119
- for _ , c := range s .connections {
126
+ s .connections .Range (func (key , value interface {}) bool {
127
+ c := key .(* Connection )
120
128
c .Close ()
121
- }
129
+
130
+ return true
131
+ })
132
+
122
133
}
123
134
}
124
135
@@ -253,8 +264,6 @@ func (c *Connection) Send(data interface{}, opts ...ConnectionOption) error {
253
264
// Close 关闭连接
254
265
func (c * Connection ) Close () error {
255
266
if c .connected .Get () {
256
- log .Info ("Close " , c .PeerAddr ())
257
-
258
267
close (c .dying )
259
268
c .connected .Set (false )
260
269
c .callBack .OnClose (c )
@@ -272,9 +281,6 @@ func (c *Connection) Close() error {
272
281
273
282
// ShutdownWrite 关闭可写端,等待读取完接收缓冲区所有数据
274
283
func (c * Connection ) ShutdownWrite () error {
275
- log .Info ("ShutdownWrite " , c .PeerAddr ())
276
-
277
- //return nil
278
284
return c .Close ()
279
285
}
280
286
@@ -302,7 +308,6 @@ func (c *Connection) readLoop() {
302
308
return
303
309
304
310
default :
305
- //c.conn.SetReadDeadline(time.Now().Add(time.Second))
306
311
n , err := c .conn .Read (buf )
307
312
if err != nil {
308
313
if err != io .EOF {
@@ -345,8 +350,6 @@ func (c *Connection) writeLoop() {
345
350
continue
346
351
}
347
352
348
- c .conn .SetWriteDeadline (time .Now ().Add (time .Second ))
349
-
350
353
first , end := c .outBuffer .PeekAll ()
351
354
n , err := c .conn .Write (first )
352
355
if err != nil {
@@ -429,14 +432,10 @@ func (c *Connection) closeTimeoutConn() func() {
429
432
return func () {
430
433
now := time .Now ()
431
434
intervals := now .Sub (time .Unix (c .activeTime .Get (), 0 ))
432
- log .Info ("closeTimeoutConn " , intervals )
433
435
434
436
if intervals >= c .idleTime {
435
- log .Info ("closeTimeoutConn " , c .conn .RemoteAddr ())
436
437
_ = c .Close ()
437
438
} else {
438
- log .Info ("timingWheel.AfterFunc " , c .idleTime - intervals )
439
-
440
439
timer := c .timingWheel .AfterFunc (c .idleTime - intervals , c .closeTimeoutConn ())
441
440
c .timer .Store (timer )
442
441
}
0 commit comments