-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtcp_listener.go
224 lines (203 loc) · 5.51 KB
/
tcp_listener.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
package gnet
import (
"context"
"errors"
"net"
"sync"
"sync/atomic"
"syscall"
"time"
)
// tcp Listener
type TcpListener struct {
baseListener
netListener net.Listener
acceptConnectionConfig ConnectionConfig
acceptConnectionCodec Codec
acceptConnectionHandler ConnectionHandler
// accept协程结束标记
// notify chan for accept goroutine end
acceptStopNotifyChan chan struct{}
// manage the accepted connections
connectionMap map[uint32]Connection
connectionMapLock sync.RWMutex
isRunning int32
closeOnce sync.Once
// close callback
onClose func(listener Listener)
acceptConnectionCreator AcceptConnectionCreator
// 外部传进来的WaitGroup
netMgrWg *sync.WaitGroup
}
func NewTcpListener(listenerConfig *ListenerConfig) *TcpListener {
return &TcpListener{
baseListener: baseListener{
listenerId: newListenerId(),
config: listenerConfig,
handler: listenerConfig.ListenerHandler,
},
acceptConnectionConfig: listenerConfig.AcceptConfig,
acceptConnectionCodec: listenerConfig.AcceptConfig.Codec,
acceptConnectionHandler: listenerConfig.AcceptConfig.Handler,
acceptConnectionCreator: listenerConfig.AcceptConnectionCreator,
connectionMap: make(map[uint32]Connection),
acceptStopNotifyChan: make(chan struct{}, 1),
}
}
func (l *TcpListener) GetConnection(connectionId uint32) Connection {
l.connectionMapLock.RLock()
conn := l.connectionMap[connectionId]
l.connectionMapLock.RUnlock()
return conn
}
// 广播消息
//
// broadcast packet to accepted connections
func (l *TcpListener) Broadcast(packet Packet) {
l.connectionMapLock.RLock()
defer l.connectionMapLock.RUnlock()
for _, conn := range l.connectionMap {
if conn.IsConnected() {
conn.SendPacket(packet.Clone())
}
}
}
// range for accepted connections
func (l *TcpListener) RangeConnections(f func(conn Connection) bool) {
l.connectionMapLock.RLock()
defer l.connectionMapLock.RUnlock()
for _, conn := range l.connectionMap {
if conn.IsConnected() {
if !f(conn) {
return
}
}
}
}
// start goroutine
func (l *TcpListener) Start(ctx context.Context, listenAddress string) bool {
var err error
l.netListener, err = net.Listen("tcp", listenAddress)
if err != nil {
logger.Error("Listen Failed %v: %v", l.GetListenerId(), err.Error())
return false
}
logger.Debug("TcpListener Start %v", l.GetListenerId())
// 监听协程
atomic.StoreInt32(&l.isRunning, 1)
l.netMgrWg.Add(1)
go func(ctx context.Context) {
defer l.netMgrWg.Done()
l.acceptLoop(ctx)
l.acceptStopNotifyChan <- struct{}{}
logger.Debug("acceptLoop end %v", l.GetListenerId())
}(ctx)
// 关闭响应协程
l.netMgrWg.Add(1)
go func() {
defer l.netMgrWg.Done()
for l.IsRunning() {
select {
// 关闭通知
case <-ctx.Done():
logger.Debug("recv closeNotify %v", l.GetListenerId())
l.Close()
return
case <-l.acceptStopNotifyChan:
logger.Debug("recv acceptStopNotify %v", l.GetListenerId())
l.Close()
return
}
}
}()
return true
}
// 关闭监听,并关闭管理的连接
//
// close listen, close the accepted connections
func (l *TcpListener) Close() {
l.closeOnce.Do(func() {
atomic.StoreInt32(&l.isRunning, 0)
if l.netListener != nil {
l.netListener.Close()
}
connMap := make(map[uint32]Connection)
l.connectionMapLock.RLock()
for k, v := range l.connectionMap {
connMap[k] = v
}
l.connectionMapLock.RUnlock()
// 关闭管理的连接
for _, conn := range connMap {
conn.Close()
}
if l.onClose != nil {
l.onClose(l)
}
})
}
func (l *TcpListener) IsRunning() bool {
return atomic.LoadInt32(&l.isRunning) > 0
}
// accept goroutine
func (l *TcpListener) acceptLoop(ctx context.Context) {
defer func() {
if err := recover(); err != nil {
logger.Error("acceptLoop fatal %v: %v", l.GetListenerId(), err.(error))
LogStack()
}
}()
for l.IsRunning() {
// 阻塞accept,当netListener关闭时,会返回err
newConn, err := l.netListener.Accept()
if err != nil {
logger.Error("%v accept err:%v", l.GetListenerId(), err.Error())
var netError net.Error
if errors.As(err, &netError) && netError.Temporary() {
logger.Error("accept temporary err:%v", l.GetListenerId())
time.Sleep(100 * time.Millisecond)
continue
}
// 有可能是因为open file数量限制 而导致的accept失败
if err == syscall.EMFILE {
logger.Error("accept failed id:%v syscall.EMFILE", l.GetListenerId())
// 这个错误只是导致新连接暂时无法连接,不应该退出监听,当有连接释放后,新连接又可以连接上
time.Sleep(100 * time.Millisecond)
continue
}
break
}
l.netMgrWg.Add(1)
go func() {
defer func() {
l.netMgrWg.Done()
if err := recover(); err != nil {
logger.Error("acceptLoop fatal %v: %v", l.GetListenerId(), err.(error))
LogStack()
}
}()
newTcpConn := l.acceptConnectionCreator(newConn, &l.acceptConnectionConfig)
l.connectionMapLock.Lock()
l.connectionMap[newTcpConn.GetConnectionId()] = newTcpConn
l.connectionMapLock.Unlock()
newTcpConn.Start(ctx, l.netMgrWg, func(connection Connection) {
if l.handler != nil {
l.handler.OnConnectionDisconnect(l, connection)
}
l.connectionMapLock.Lock()
delete(l.connectionMap, connection.GetConnectionId())
l.connectionMapLock.Unlock()
})
if l.handler != nil {
l.handler.OnConnectionConnected(l, newTcpConn)
}
}()
}
}
// Addr returns the listener's network address.
func (l *TcpListener) Addr() net.Addr {
if l.netListener == nil {
return nil
}
return l.netListener.Addr()
}