-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpacket.go
204 lines (165 loc) · 4.49 KB
/
packet.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
package reliable
import (
"github.com/lithdew/bytesutil"
"github.com/valyala/bytebufferpool"
"io"
"math/bits"
"time"
)
const ACKBitsetSize = 32
type (
Buffer = bytebufferpool.ByteBuffer
Pool = bytebufferpool.Pool
)
type writtenPacket struct {
buf *Buffer // pooled contents of this packet
acked bool // whether or not this packet was acked
written time.Time // last time the packet was written
resent byte // total number of times this packet was resent
}
func (p writtenPacket) shouldResend(now time.Time, resendTimeout time.Duration) bool {
return !p.acked && p.resent < 10 && now.Sub(p.written) >= resendTimeout
}
type PacketHeaderFlag uint8
const (
FlagFragment PacketHeaderFlag = 1 << iota
FlagA
FlagB
FlagC
FlagD
FlagACKEncoded
FlagEmpty
FlagUnordered
)
func (p PacketHeaderFlag) Toggle(flag PacketHeaderFlag) PacketHeaderFlag {
return p | flag
}
func (p PacketHeaderFlag) Toggled(flag PacketHeaderFlag) bool {
return p&flag != 0
}
func (p PacketHeaderFlag) AppendTo(dst []byte) []byte {
return append(dst, byte(p))
}
type PacketHeader struct {
Sequence uint16
ACK uint16
ACKBits uint32
Unordered bool
Empty bool
}
func (p PacketHeader) AppendTo(dst []byte) []byte {
// Mark a flag byte to RLE-encode the ACK bitset.
flag := PacketHeaderFlag(0)
if p.ACKBits&0x000000FF != 0x000000FF {
flag = flag.Toggle(FlagA)
}
if p.ACKBits&0x0000FF00 != 0x0000FF00 {
flag = flag.Toggle(FlagB)
}
if p.ACKBits&0x00FF0000 != 0x00FF0000 {
flag = flag.Toggle(FlagC)
}
if p.ACKBits&0xFF000000 != 0xFF000000 {
flag = flag.Toggle(FlagD)
}
if p.Empty {
flag = flag.Toggle(FlagEmpty)
}
if p.Unordered {
flag = flag.Toggle(FlagUnordered)
}
diff := int(p.Sequence) - int(p.ACK)
if diff < 0 {
diff += 65536
}
if diff <= 255 {
flag = flag.Toggle(FlagACKEncoded)
}
// If the difference between the sequence number and the latest ACK'd sequence number can be represented by a
// single byte, then represent it as a single byte and set the 5th bit of flag.
// Marshal the flag and sequence number and latest ACK'd sequence number.
dst = flag.AppendTo(dst)
if p.Unordered {
dst = bytesutil.AppendUint16BE(dst, p.ACK)
} else {
dst = bytesutil.AppendUint16BE(dst, p.Sequence)
if diff <= 255 {
dst = append(dst, uint8(diff))
} else {
dst = bytesutil.AppendUint16BE(dst, p.ACK)
}
}
// Marshal ACK bitset.
if p.ACKBits&0x000000FF != 0x000000FF {
dst = append(dst, uint8(p.ACKBits&0x000000FF))
}
if p.ACKBits&0x0000FF00 != 0x0000FF00 {
dst = append(dst, uint8((p.ACKBits&0x0000FF00)>>8))
}
if p.ACKBits&0x00FF0000 != 0x00FF0000 {
dst = append(dst, uint8((p.ACKBits&0x00FF0000)>>16))
}
if p.ACKBits&0xFF000000 != 0xFF000000 {
dst = append(dst, uint8((p.ACKBits&0xFF000000)>>24))
}
return dst
}
func UnmarshalPacketHeader(buf []byte) (header PacketHeader, leftover []byte, err error) {
flag := PacketHeaderFlag(0)
// Read first 3 bytes (header, flag).
if len(buf) < 3 {
return header, buf, io.ErrUnexpectedEOF
}
flag, buf = PacketHeaderFlag(buf[0]), buf[1:]
if flag.Toggled(FlagFragment) {
return header, buf, io.ErrUnexpectedEOF
}
header.Empty = flag.Toggled(FlagEmpty)
header.Unordered = flag.Toggled(FlagUnordered)
if header.Unordered {
if len(buf) < 2 {
return header, buf, io.ErrUnexpectedEOF
}
header.ACK, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
} else {
header.Sequence, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
// Read and decode the latest ACK'ed sequence number (either 1 or 2 bytes) using the RLE flag marker.
if flag.Toggled(FlagACKEncoded) {
if len(buf) < 1 {
return header, buf, io.ErrUnexpectedEOF
}
header.ACK, buf = header.Sequence-uint16(buf[0]), buf[1:]
} else {
if len(buf) < 2 {
return header, buf, io.ErrUnexpectedEOF
}
header.ACK, buf = bytesutil.Uint16BE(buf[:2]), buf[2:]
}
}
if len(buf) < bits.OnesCount8(uint8(flag&(FlagA|FlagB|FlagC|FlagD))) {
return header, buf, io.ErrUnexpectedEOF
}
// Read and decode ACK bitset using the RLE flag marker.
header.ACKBits = 0xFFFFFFFF
if flag.Toggled(FlagA) {
header.ACKBits &= 0xFFFFFF00
header.ACKBits |= uint32(buf[0])
buf = buf[1:]
}
if flag.Toggled(FlagB) {
header.ACKBits &= 0xFFFF00FF
header.ACKBits |= uint32(buf[0]) << 8
buf = buf[1:]
}
if flag.Toggled(FlagC) {
header.ACKBits &= 0xFF00FFFF
header.ACKBits |= uint32(buf[0]) << 16
buf = buf[1:]
}
if flag.Toggled(FlagD) {
header.ACKBits &= 0x00FFFFFF
header.ACKBits |= uint32(buf[0]) << 24
buf = buf[1:]
}
return header, buf, nil
}