-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
331 lines (277 loc) · 7.28 KB
/
conn.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
// File Tcpcon
// @Author: yandaren1220@126.com
// @Date: 2016-08-23
package gonetio
import (
"bytes"
"errors"
"net"
"runtime/debug"
"sync"
"sync/atomic"
"time"
)
// error type
var (
ErrConnShutdown = errors.New("Connection has shutdown")
ErrConnClosed = errors.New("Connection has been closed")
ErrWriteBlocking = errors.New("Write packet was blocking")
ErrConnException = errors.New("Connection exception")
)
// connection state
type ConState int32
const (
ConStateOpened ConState = iota
ConStateClosed
)
type Tcpcon struct {
condID uint32 // connection id
rawConn *net.TCPConn // the raw connection
keepAliveMinTime int // in seconds, the min time between two package read from remote, valid only when the value is positive
customData interface{} // save the user custom data
remoteAddr string // the remote addr
fullBuffer *bytes.Buffer // full recv buffer
recvBuffer []byte // recv buffer
packetSendChan chan *bytes.Buffer // packet send channel
conState ConState // the connection state
closeOnce sync.Once // make sure the connection call close just once
closeChan chan struct{} // close signal to the read/write loop
shutdownFlag int32 // shutdown flag
ioFilterChain *IoFilterChain // filter chain
waitGroup *sync.WaitGroup // wait group
globalExitChan chan struct{} // global exit chan
}
// new a connection instance from tcp acceptor
func newConn(conn *net.TCPConn, aptor *TcpAcceptor) *Tcpcon {
con := NewConnFull(conn, aptor.config.connSendChanSizeLimit, aptor.waitGroup, aptor.config.keepAliveMinTime)
con.setGlobalExitChan(aptor.exitChan)
return con
}
// new a connection instance
func NewConn(conn *net.TCPConn, sendQueueSize int, wg *sync.WaitGroup, keepAliveMinTimeDuration int) *Tcpcon {
return NewConnFull(conn, sendQueueSize, wg, keepAliveMinTimeDuration)
}
// new a connection instance
func NewConnFull(conn *net.TCPConn, sendQueueSize int, wg *sync.WaitGroup, keepAliveMinTimeDuration int) *Tcpcon {
var addr string = ""
if conn != nil {
addr = conn.RemoteAddr().String()
}
return &Tcpcon{
condID: 0,
rawConn: conn,
keepAliveMinTime: keepAliveMinTimeDuration,
remoteAddr: addr,
fullBuffer: bytes.NewBuffer([]byte{}),
recvBuffer: make([]byte, 65535),
packetSendChan: make(chan *bytes.Buffer, sendQueueSize),
conState: ConStateClosed,
shutdownFlag: 0,
closeChan: make(chan struct{}),
ioFilterChain: nil,
waitGroup: wg,
globalExitChan: make(chan struct{}),
}
}
// set conid
func (this *Tcpcon) SetConID(id uint32) {
this.condID = id
}
// get conid
func (this *Tcpcon) GetConID() uint32 {
return this.condID
}
// set iofilter chain
func (this *Tcpcon) SetIoFilterChain(chain *IoFilterChain) {
this.ioFilterChain = chain
if this.ioFilterChain != nil {
this.ioFilterChain.setCon(this)
}
}
// get iofilter chain
func (this *Tcpcon) GetIoFilterChain() *IoFilterChain {
return this.ioFilterChain
}
// set global exit chan
func (this *Tcpcon) setGlobalExitChan(gexitchan chan struct{}) {
this.globalExitChan = gexitchan
}
// set custom data
func (this *Tcpcon) SetCustomData(data interface{}) {
this.customData = data
}
// get custom data
func (this *Tcpcon) GetCustomData() interface{} {
return this.customData
}
// set remote addr
func (this *Tcpcon) SetRemoteAddr(addr string) {
this.remoteAddr = addr
}
// get remote addr
func (this *Tcpcon) RemoteAddr() string {
return this.remoteAddr
}
// is connection opened
func (this *Tcpcon) IsConnected() bool {
return this.conState == ConStateOpened
}
// is closed
func (this *Tcpcon) IsShutdown() bool {
return atomic.LoadInt32(&this.shutdownFlag) == 1
}
// shutdown the connection
func (this *Tcpcon) ShutDown() {
if atomic.SwapInt32(&this.shutdownFlag, 1) == 0 {
LogError("Tcpcon ShutDown, url[%s].", this.remoteAddr)
this.Close()
}
}
// close the connection
func (this *Tcpcon) Close() {
this.closeOnce.Do(func() {
this.conState = ConStateClosed
close(this.closeChan)
close(this.packetSendChan)
this.rawConn.Close()
if !this.IsShutdown() {
this.ioFilterChain.FireConnClosed()
}
})
}
// add to the send queue
func (this *Tcpcon) Flush(buffer *bytes.Buffer, timeout time.Duration) (err error) {
if this.IsShutdown() {
return ErrConnShutdown
}
defer func() {
if e := recover(); e != nil {
err = ErrConnException
}
}()
if timeout == 0 {
select {
case this.packetSendChan <- buffer:
return nil
default:
return ErrWriteBlocking
}
} else {
select {
case this.packetSendChan <- buffer:
return nil
case <-this.closeChan:
return ErrConnClosed
case <-time.After(timeout):
return ErrWriteBlocking
}
}
}
// connection start
func (this *Tcpcon) Start() {
if this.IsShutdown() {
return
}
if this.rawConn != nil {
this.SetRemoteAddr(this.rawConn.RemoteAddr().String())
}
this.conState = ConStateOpened
this.ioFilterChain.FireConnOpened()
// start the read/write/handle loop
asyncDo(this.readLoop, this.waitGroup)
asyncDo(this.writeLoop, this.waitGroup)
}
// try set read dead line
func (this *Tcpcon) setReadDeadline() {
if this.keepAliveMinTime > 0 {
this.rawConn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(this.keepAliveMinTime)))
}
}
// read
func (this *Tcpcon) read(b []byte) (int, error) {
return this.rawConn.Read(b)
}
// write
func (this *Tcpcon) Write(obj BaseObject) {
if this.ioFilterChain != nil {
this.ioFilterChain.FireWrite(obj)
} else {
LogError("Tcpcon Write, but io filter chain is nil, write failed.")
}
}
// async do
func asyncDo(fn func(), wg *sync.WaitGroup) {
wg.Add(1)
go func() {
fn()
wg.Done()
}()
}
// read loop
func (this *Tcpcon) readLoop() {
defer func() {
if p := recover(); p != nil {
LogError("panic recover, p: %v", p)
LogError("stack: %s", debug.Stack())
}
this.Close()
LogInfo("connection[%s] readloop exit.", this.remoteAddr)
}()
LogInfo("connection[%s] readloop start.", this.remoteAddr)
for {
select {
case <-this.globalExitChan:
return
case <-this.closeChan:
return
default:
}
this.setReadDeadline()
readLen, err := this.read(this.recvBuffer)
if err != nil {
LogError("connection[%s] read data error, error:%s.", this.remoteAddr, err.Error())
return
}
if readLen == 0 {
LogError("connection[%s] read data error, read data len is 0, connection may closed.", this.remoteAddr)
return
}
if this.IsShutdown() {
return
}
this.fullBuffer.Write(this.recvBuffer[:readLen])
if !this.IsShutdown() {
this.ioFilterChain.FireMessageReceived(this.fullBuffer)
}
}
}
// write loop
func (this *Tcpcon) writeLoop() {
defer func() {
if p := recover(); p != nil {
LogError("panic recover, p: %v", p)
LogError("stack: %s", debug.Stack())
}
this.Close()
LogError("connection[%s] write loop exit.", this.remoteAddr)
}()
LogInfo("connection[%s] write loop start.", this.remoteAddr)
for {
select {
case <-this.globalExitChan:
return
case <-this.closeChan:
return
case p := <-this.packetSendChan:
if p == nil {
return
}
if this.IsShutdown() {
return
}
if _, err := this.rawConn.Write(p.Bytes()); err != nil {
return
}
}
}
}