-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathconn.go
271 lines (240 loc) · 5.41 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
// Copyright 2015 Tony Bai.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package cmpp
import (
"encoding/binary"
"errors"
"io"
"net"
"sync"
"time"
)
type State uint8
// Errors for conn operations
var (
ErrConnIsClosed = errors.New("connection is closed")
ErrReadCmdIDTimeout = errors.New("read commandId timeout")
ErrReadPktBodyTimeout = errors.New("read packet body timeout")
)
var noDeadline = time.Time{}
// Conn States
const (
CONN_CLOSED State = iota
CONN_CONNECTED
CONN_AUTHOK
)
type Conn struct {
net.Conn
State State
Typ Type
// for SeqId generator goroutine
SeqId <-chan uint32
done chan<- struct{}
}
func newSeqIdGenerator() (<-chan uint32, chan<- struct{}) {
out := make(chan uint32)
done := make(chan struct{})
go func() {
var i uint32
for {
select {
case out <- i:
i++
case <-done:
close(out)
return
}
}
}()
return out, done
}
// New returns an abstract structure for successfully
// established underlying net.Conn.
func NewConn(conn net.Conn, typ Type) *Conn {
seqId, done := newSeqIdGenerator()
c := &Conn{
Conn: conn,
Typ: typ,
SeqId: seqId,
done: done,
}
tc := c.Conn.(*net.TCPConn) // Always tcpconn
tc.SetKeepAlive(true) //Keepalive as default
return c
}
func (c *Conn) Close() {
if c != nil {
if c.State == CONN_CLOSED {
return
}
close(c.done) // let the SeqId goroutine exit.
c.Conn.Close() // close the underlying net.Conn
c.State = CONN_CLOSED
}
}
func (c *Conn) SetState(state State) {
c.State = state
}
// SendPkt pack the cmpp packet structure and send it to the other peer.
func (c *Conn) SendPkt(packet Packer, seqId uint32) error {
if c.State == CONN_CLOSED {
return ErrConnIsClosed
}
data, err := packet.Pack(seqId)
if err != nil {
return err
}
_, err = c.Conn.Write(data) //block write
if err != nil {
return err
}
return nil
}
const (
defaultReadBufferSize = 4096
)
// readBuffer is used to optimize the performance of
// RecvAndUnpackPkt.
type readBuffer struct {
totalLen uint32
commandId CommandId
leftData [defaultReadBufferSize]byte
}
var readBufferPool = sync.Pool{
New: func() interface{} {
return &readBuffer{}
},
}
// RecvAndUnpackPkt receives cmpp byte stream, and unpack it to some cmpp packet structure.
func (c *Conn) RecvAndUnpackPkt(timeout time.Duration) (interface{}, error) {
if c.State == CONN_CLOSED {
return nil, ErrConnIsClosed
}
defer c.SetReadDeadline(noDeadline)
rb := readBufferPool.Get().(*readBuffer)
defer readBufferPool.Put(rb)
// Total_Length in packet
if timeout != 0 {
c.SetReadDeadline(time.Now().Add(timeout))
}
err := binary.Read(c.Conn, binary.BigEndian, &rb.totalLen)
if err != nil {
return nil, err
}
if c.Typ == V30 {
if rb.totalLen < CMPP3_PACKET_MIN || rb.totalLen > CMPP3_PACKET_MAX {
return nil, ErrTotalLengthInvalid
}
}
if c.Typ == V21 || c.Typ == V20 {
if rb.totalLen < CMPP2_PACKET_MIN || rb.totalLen > CMPP2_PACKET_MAX {
return nil, ErrTotalLengthInvalid
}
}
// Command_Id
if timeout != 0 {
c.SetReadDeadline(time.Now().Add(timeout))
}
err = binary.Read(c.Conn, binary.BigEndian, &rb.commandId)
if err != nil {
netErr, ok := err.(net.Error)
if ok {
if netErr.Timeout() {
return nil, ErrReadCmdIDTimeout
}
}
return nil, err
}
if !((rb.commandId > CMPP_REQUEST_MIN && rb.commandId < CMPP_REQUEST_MAX) ||
(rb.commandId > CMPP_RESPONSE_MIN && rb.commandId < CMPP_RESPONSE_MAX)) {
return nil, ErrCommandIdInvalid
}
// The left packet data (start from seqId in header).
if timeout != 0 {
c.SetReadDeadline(time.Now().Add(timeout))
}
var leftData = rb.leftData[0:(rb.totalLen - 8)]
_, err = io.ReadFull(c.Conn, leftData)
if err != nil {
netErr, ok := err.(net.Error)
if ok {
if netErr.Timeout() {
return nil, ErrReadPktBodyTimeout
}
}
return nil, err
}
var p Packer
switch rb.commandId {
case CMPP_CONNECT:
p = &CmppConnReqPkt{}
case CMPP_CONNECT_RESP:
if c.Typ == V30 {
p = &Cmpp3ConnRspPkt{}
} else {
p = &Cmpp2ConnRspPkt{}
}
case CMPP_TERMINATE:
p = &CmppTerminateReqPkt{}
case CMPP_TERMINATE_RESP:
p = &CmppTerminateRspPkt{}
case CMPP_SUBMIT:
if c.Typ == V30 {
p = &Cmpp3SubmitReqPkt{}
} else {
p = &Cmpp2SubmitReqPkt{}
}
case CMPP_SUBMIT_RESP:
if c.Typ == V30 {
p = &Cmpp3SubmitRspPkt{}
} else {
p = &Cmpp2SubmitRspPkt{}
}
case CMPP_DELIVER:
if c.Typ == V30 {
p = &Cmpp3DeliverReqPkt{}
} else {
p = &Cmpp2DeliverReqPkt{}
}
case CMPP_DELIVER_RESP:
if c.Typ == V30 {
p = &Cmpp3DeliverRspPkt{}
} else {
p = &Cmpp2DeliverRspPkt{}
}
case CMPP_FWD:
if c.Typ == V30 {
p = &Cmpp3FwdReqPkt{}
} else {
p = &Cmpp2FwdReqPkt{}
}
case CMPP_FWD_RESP:
if c.Typ == V30 {
p = &Cmpp3FwdRspPkt{}
} else {
p = &Cmpp2FwdRspPkt{}
}
case CMPP_ACTIVE_TEST:
p = &CmppActiveTestReqPkt{}
case CMPP_ACTIVE_TEST_RESP:
p = &CmppActiveTestRspPkt{}
default:
p = nil
return nil, ErrCommandIdNotSupported
}
err = p.Unpack(leftData)
if err != nil {
return nil, err
}
return p, nil
}