/
router_connection.go
367 lines (313 loc) · 7.54 KB
/
router_connection.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
package router
import (
"bytes"
"compress/gzip"
"hash/crc32"
"net"
"sync"
"time"
"github.com/fletaio/common"
"github.com/fletaio/common/util"
)
type routerPhysical interface {
acceptConn(pc *RouterConn) error
localAddress() string
chainCoord() *common.Coordinate
removeRouterConn(conn net.Conn)
unsafeRemoveRouterConn(conn net.Conn)
port() int
}
//MAGICWORD Start of packet
const (
MAGICWORD = 'R'
HEARTBIT = 'H'
)
//compression types
const (
UNCOMPRESSED = uint8(0)
COMPRESSED = uint8(1)
)
// IEEETable is common table of CRC-32 polynomial.
var IEEETable = crc32.MakeTable(crc32.IEEE)
type RouterConn struct {
writeLock sync.Mutex
pConn net.Conn
pingTime time.Duration
isClose bool
r routerPhysical
localhost string
connChan chan *readConn
connBuff bytes.Buffer
heartBitTime time.Time
readBuf bytes.Buffer
c *dataCase
Address string
}
type readConn struct {
n int
err error
bs []byte
}
func newRouterConn(addr string, conn net.Conn, r routerPhysical) *RouterConn {
pc := &RouterConn{
pConn: conn,
isClose: false,
r: r,
heartBitTime: time.Now(),
}
pc.checkHeartBit()
return pc
}
func (pc *RouterConn) checkHeartBit() {
go func() {
for {
time.Sleep(3 * time.Second)
passed := time.Now().Sub(pc.heartBitTime)
if passed > 15*time.Second {
// log.Println("no heartbit while", passed, "/", 15*time.Second, ":", pc.ID(), pc.LocalAddr().String(), pc.RemoteAddr().String())
pc.Close()
return
}
}
}()
}
func (pc *RouterConn) ID() string {
return pc.Address
}
func (pc *RouterConn) Write(body []byte) (int, error) {
return pc.write(body, UNCOMPRESSED)
}
func (pc *RouterConn) write(body []byte, compression uint8) (int, error) {
var wrote int
var buffer bytes.Buffer
pc.writeLock.Lock()
defer pc.writeLock.Unlock()
if pc.pConn == nil {
return wrote, ErrNotConnected
}
if n, err := util.WriteUint8(&buffer, MAGICWORD); err == nil {
wrote += int(n)
} else {
return wrote, err
}
if n, err := pc.r.chainCoord().WriteTo(&buffer); err == nil {
wrote += int(n)
} else {
return wrote, err
}
if n, err := util.WriteUint8(&buffer, compression); err == nil {
wrote += int(n)
} else {
return wrote, err
}
if compression == COMPRESSED {
buf := &bytes.Buffer{}
gw := gzip.NewWriter(buf)
if _, err := gw.Write(body); err != nil {
return wrote, err
}
if err := gw.Flush(); err != nil {
return wrote, err
}
if err := gw.Close(); err != nil {
return wrote, err
}
body = buf.Bytes()
}
size := len(body)
if n, err := util.WriteUint32(&buffer, uint32(size)); err == nil {
wrote += int(n)
} else {
return wrote, err
}
if n, err := buffer.Write(body); err == nil {
wrote += int(n)
} else {
return wrote, err
}
checksum := crc32.Checksum(body, IEEETable)
if n, err := util.WriteUint32(&buffer, checksum); err == nil {
wrote += int(n)
} else {
return wrote, err
}
errCh := make(chan error)
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done()
_, err := pc.pConn.Write(buffer.Bytes())
if err != nil {
pc.Close()
}
errCh <- err
}()
wg.Wait()
deadTimer := time.NewTimer(5 * time.Second)
select {
case <-deadTimer.C:
pc.Close()
return wrote, <-errCh
case err := <-errCh:
deadTimer.Stop()
return wrote, err
}
}
type dataCase struct {
data []byte
size int
readed int
}
func (pc *RouterConn) Read(b []byte) (int, error) {
if pc.readBuf.Len() == 0 {
data, err := pc.ReadConn()
if err != nil {
return 0, err
}
pc.c = &dataCase{
data: data,
size: len(data),
}
if len(b) >= len(pc.c.data) {
copy(b[:], pc.c.data[:len(pc.c.data)])
pc.c.readed += len(pc.c.data)
return len(pc.c.data), nil
}
copy(b[:], pc.c.data[:len(b)])
pc.readBuf.Write(pc.c.data[len(b):])
pc.c.readed += len(b)
return len(b), nil
}
n, err := pc.readBuf.Read(b)
pc.c.readed += n
return n, err
}
func (pc *RouterConn) ReadConn() (body []byte, returnErr error) {
ChainCoord := &common.Coordinate{}
var bs []byte
var err error
for {
bs, err = pc.readBytes(1) // header 1
if err != nil {
returnErr = err
return
}
if bs[0] == HEARTBIT {
pc.heartBitTime = time.Now()
} else {
break
}
}
header, err := pc.readBytes(11) // 6 + 1 + 4
if err != nil {
returnErr = err
return
}
bs = append(bs, header...)
bf := bytes.NewBuffer(bs[1:7])
ChainCoord.ReadFrom(bf)
compression := uint8(bs[7])
bodySize := util.BytesToUint32(bs[8:])
body, err = pc.readBytes(bodySize)
if err != nil {
returnErr = err
return
}
if MAGICWORD != uint8(bs[0]) {
returnErr = ErrPacketNotStartedMagicword
return
}
checksum := crc32.Checksum(body, IEEETable)
if compression == COMPRESSED {
var buf bytes.Buffer
gr, err := gzip.NewReader(bytes.NewBuffer(body))
defer gr.Close()
_, err = buf.ReadFrom(gr)
if err != nil {
returnErr = err
return
}
body = buf.Bytes()
} else if compression != UNCOMPRESSED {
returnErr = ErrNotMatchCompressionType
return
}
checksumBs, err := pc.readBytes(4)
if err != nil {
returnErr = err
return
}
readedChecksum := util.BytesToUint32(checksumBs)
if readedChecksum != checksum {
returnErr = ErrInvalidIntegrity
return
}
return
}
func (pc *RouterConn) readBytes(n uint32) (read []byte, returnErr error) {
pc.SetDeadline(time.Now().Add(time.Second * 15))
bs := make([]byte, n)
_, err := util.FillBytes(pc.pConn, bs)
if err != nil { //has error
return nil, err
}
return bs, nil
}
func (pc *RouterConn) SendHeartBit() {
_, err := pc.pConn.Write([]byte{HEARTBIT})
if err != nil {
pc.pConn.Close()
}
}
func (pc *RouterConn) RemoteAddr() net.Addr {
return pc.pConn.RemoteAddr()
}
func (pc *RouterConn) LocalAddr() net.Addr {
return pc.pConn.LocalAddr()
}
//Close is used to sever all physical connections and logical connections related to physical connections
func (pc *RouterConn) Close() (err error) {
pc.isClose = true
err = pc.pConn.Close()
pc.r.removeRouterConn(pc.pConn)
return
}
//LockFreeClose is used to sever all physical connections and logical connections related to physical connections without lock
func (pc *RouterConn) LockFreeClose() (err error) {
pc.isClose = true
err = pc.pConn.Close()
pc.r.unsafeRemoveRouterConn(pc.pConn)
return err
}
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future and pending
// I/O, not just the immediately following call to Read or
// Write. After a deadline has been exceeded, the connection
// can be refreshed by setting a deadline in the future.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
func (pc *RouterConn) SetDeadline(t time.Time) error {
return pc.pConn.SetDeadline(t)
}
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
func (pc *RouterConn) SetReadDeadline(t time.Time) error {
return pc.pConn.SetReadDeadline(t)
}
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
func (pc *RouterConn) SetWriteDeadline(t time.Time) error {
return pc.pConn.SetWriteDeadline(t)
}