-
Notifications
You must be signed in to change notification settings - Fork 5
/
PacketConn.go
308 lines (257 loc) · 6.46 KB
/
PacketConn.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
package pktconn
import (
"context"
"fmt"
"github.com/xiaonanln/tickchan"
"hash/crc32"
"io"
"net"
"sync/atomic"
"time"
)
const (
MaxPayloadLength = 32 * 1024 * 1024
DefaultRecvChanSize = 100
defaultFlushDelay = time.Millisecond * 1
defaultMaxFlushDelay = time.Millisecond * 10
sendChanSize = 100
payloadLengthSize = 4 // payloadLengthSize is the packet size field (uint32) size
prePayloadSize = payloadLengthSize
)
var (
uniformTicker = tickchan.NewChanTicker(time.Millisecond)
)
type Config struct {
FlushDelay time.Duration `json:"flush_delay"`
MaxFlushDelay time.Duration `json:"max_flush_delay"`
CrcChecksum bool `json:"crc_checksum"`
Tag interface{}
}
func DefaultConfig() *Config {
return &Config{
FlushDelay: defaultFlushDelay,
MaxFlushDelay: defaultMaxFlushDelay,
CrcChecksum: false,
Tag: nil,
}
}
// PacketConn is a connection that send and receive data packets upon a network stream connection
type PacketConn struct {
Tag interface{}
Config Config
ctx context.Context
conn net.Conn
sendChan chan *Packet
waitPendingPacketsCnt int32
gotPacketFlag bool
cancel context.CancelFunc
err error
once uint32
}
// NewPacketConn creates a packet connection based on network connection
func NewPacketConn(ctx context.Context, conn net.Conn) *PacketConn {
return NewPacketConnWithConfig(ctx, conn, DefaultConfig())
}
func NewPacketConnWithConfig(ctx context.Context, conn net.Conn, cfg *Config) *PacketConn {
if conn == nil {
panic(fmt.Errorf("conn is nil"))
}
if cfg == nil {
cfg = DefaultConfig()
}
validateConfig(cfg)
pcCtx, pcCancel := context.WithCancel(ctx)
pc := &PacketConn{
conn: conn,
Config: *cfg,
ctx: pcCtx,
cancel: pcCancel,
Tag: cfg.Tag,
sendChan: make(chan *Packet, sendChanSize),
}
go pc.flushRoutine()
return pc
}
func validateConfig(cfg *Config) {
if cfg.FlushDelay < 0 {
panic(fmt.Errorf("negative flush interval"))
}
if cfg.MaxFlushDelay < cfg.FlushDelay {
panic(fmt.Errorf("please set max_flush_delay > flush_delay"))
}
}
func (pc *PacketConn) flushRoutine() {
defer pc.Close()
tickerChan := make(chan time.Time, 1)
defer uniformTicker.Remove(tickerChan)
ctxDone := pc.ctx.Done()
var firstPacketArriveTime, lastPacketArriveTime time.Time
var packets []*Packet
loop:
for {
select {
case packet := <-pc.sendChan:
now := uniformTicker.LastTickTime()
packets = append(packets, packet)
lastPacketArriveTime = now
if len(packets) == 1 {
firstPacketArriveTime = now
uniformTicker.Add(tickerChan)
}
case now := <-tickerChan:
if len(packets) > 0 {
if now.Sub(lastPacketArriveTime) >= pc.Config.FlushDelay+time.Millisecond || now.Sub(firstPacketArriveTime) >= pc.Config.MaxFlushDelay {
// time to send the packet
uniformTicker.Remove(tickerChan)
err := pc.flush(packets)
packets = nil
if err != nil {
pc.closeWithError(err)
break loop
}
}
}
case <-ctxDone:
pc.closeWithError(pc.ctx.Err())
break loop
}
}
}
func (pc *PacketConn) Recv() <-chan *Packet {
return pc.RecvChanSize(DefaultRecvChanSize)
}
func (pc *PacketConn) RecvChanSize(chanSize uint) <-chan *Packet {
recvChan := make(chan *Packet, chanSize)
go func() {
defer close(recvChan)
_ = pc.RecvChan(recvChan)
}()
return recvChan
}
func (pc *PacketConn) RecvChan(recvChan chan *Packet) (err error) {
for {
packet, err := pc.recv()
if err != nil {
_ = pc.closeWithError(err)
break
}
recvChan <- packet
}
return
}
// Send send packets to remote
func (pc *PacketConn) Send(packet *Packet) {
if atomic.LoadInt64(&packet.refcount) <= 0 {
panic(fmt.Errorf("sending packet with refcount=%d", packet.refcount))
}
packet.addRefCount(1)
pc.sendChan <- packet
}
// Flush connection writes
func (pc *PacketConn) flush(packets []*Packet) (err error) {
if len(packets) == 1 {
// only 1 packet to send, just send it directly, no need to use send buffer
packet := packets[0]
err = pc.writePacket(packet)
packet.Release()
if err == nil {
err = tryFlush(pc.conn)
}
return
}
for _, packet := range packets {
err = pc.writePacket(packet)
packet.Release()
if err != nil {
break
}
}
// now we send all data in the send buffer
if err == nil {
err = tryFlush(pc.conn)
}
return
}
func (pc *PacketConn) writePacket(packet *Packet) error {
pdata := packet.data()
err := writeFull(pc.conn, pdata)
if err != nil {
return err
}
if pc.Config.CrcChecksum {
var crc32Buffer [4]byte
payloadCrc := crc32.ChecksumIEEE(pdata)
packetEndian.PutUint32(crc32Buffer[:], payloadCrc)
return writeFull(pc.conn, crc32Buffer[:])
} else {
return nil
}
}
// recv receives the next packet
func (pc *PacketConn) recv() (*Packet, error) {
var uint32Buffer [4]byte
//var crcChecksumBuffer [4]byte
var err error
// receive payload length (uint32)
err = readFull(pc.conn, uint32Buffer[:])
if err != nil {
return nil, err
}
payloadSize := packetEndian.Uint32(uint32Buffer[:])
if payloadSize > MaxPayloadLength {
return nil, errPayloadTooLarge
}
// allocate a packet to receive payload
packet := NewPacket()
packet.Src = pc
payload := packet.extendPayload(int(payloadSize))
err = readFull(pc.conn, payload)
if err != nil {
return nil, err
}
packet.SetPayloadLen(payloadSize)
// receive checksum (uint32)
if pc.Config.CrcChecksum {
err = readFull(pc.conn, uint32Buffer[:])
if err != nil {
return nil, err
}
payloadCrc := crc32.ChecksumIEEE(packet.data())
if payloadCrc != packetEndian.Uint32(uint32Buffer[:]) {
return nil, errChecksumError
}
}
return packet, nil
}
// Close the connection
func (pc *PacketConn) Close() error {
return pc.closeWithError(io.EOF)
}
func (pc *PacketConn) closeWithError(err error) error {
if atomic.CompareAndSwapUint32(&pc.once, 0, 1) {
// close exactly once
pc.err = err
err := pc.conn.Close()
pc.cancel()
return err
} else {
return nil
}
}
func (pc *PacketConn) Done() <-chan struct{} {
return pc.ctx.Done()
}
func (pc *PacketConn) Err() error {
return pc.err
}
// RemoteAddr return the remote address
func (pc *PacketConn) RemoteAddr() net.Addr {
return pc.conn.RemoteAddr()
}
// LocalAddr returns the local address
func (pc *PacketConn) LocalAddr() net.Addr {
return pc.conn.LocalAddr()
}
func (pc *PacketConn) String() string {
return fmt.Sprintf("PacketConn<%s-%s>", pc.LocalAddr(), pc.RemoteAddr())
}