-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
229 lines (203 loc) · 3.78 KB
/
buffer.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
package utp
import (
"errors"
"math"
"time"
)
type packetBuffer struct {
root *packetBufferNode
size int
begin int
}
type packetBufferNode struct {
p *packet
next *packetBufferNode
pushed time.Time
}
func newPacketBuffer(size, begin int) *packetBuffer {
return &packetBuffer{
size: size,
begin: begin,
}
}
func (b *packetBuffer) push(p packet) error {
if int(p.header.seq) > b.begin+b.size-1 {
return errors.New("out of bounds")
} else if int(p.header.seq) < b.begin {
if int(p.header.seq)+math.MaxUint16 > b.begin+b.size-1 {
return errors.New("out of bounds")
}
}
if b.root == nil {
b.root = &packetBufferNode{}
}
n := b.root
i := b.begin
for {
if i == int(p.header.seq) {
n.p = &p
n.pushed = time.Now()
return nil
} else if n.next == nil {
n.next = &packetBufferNode{}
}
n = n.next
i = (i + 1) % (math.MaxUint16 + 1)
}
return nil
}
func (b *packetBuffer) fetch(id uint16) (packet, error) {
for p := b.root; p != nil; p = p.next {
if p.p != nil {
if p.p.header.seq < id {
p.p = nil
} else if p.p.header.seq == id {
r := *p.p
p.p = nil
return r, nil
}
}
}
return packet{}, errors.New("not found")
}
func (b *packetBuffer) compact() {
for b.root != nil && b.root.p == nil {
b.root = b.root.next
b.begin = (b.begin + 1) % (math.MaxUint16 + 1)
}
}
func (b *packetBuffer) all() []packet {
var a []packet
for p := b.root; p != nil; p = p.next {
if p.p != nil {
a = append(a, *p.p)
}
}
return a
}
func (b *packetBuffer) first() (packet, error) {
if b.root == nil || b.root.p == nil {
return packet{}, errors.New("no first packet")
}
return *b.root.p, nil
}
func (b *packetBuffer) frontPushedTime() (time.Time, error) {
if b.root == nil || b.root.p == nil {
return time.Time{}, errors.New("no first packet")
}
return b.root.pushed, nil
}
func (b *packetBuffer) fetchSequence() []packet {
var a []packet
for ; b.root != nil && b.root.p != nil; b.root = b.root.next {
a = append(a, *b.root.p)
b.begin = (b.begin + 1) % (math.MaxUint16 + 1)
}
return a
}
func (b *packetBuffer) sequence() []packet {
var a []packet
n := b.root
for ; n != nil && n.p != nil; n = n.next {
a = append(a, *n.p)
}
return a
}
func (b *packetBuffer) space() int {
s := b.size
for p := b.root; p != nil; p = p.next {
s--
}
return s
}
func (b *packetBuffer) empty() bool {
return b.root == nil
}
func (b *packetBuffer) generateSelectiveACK() []byte {
if b.empty() {
return nil
}
var ack []byte
var bit uint
var octet byte
for p := b.root.next; p != nil; p = p.next {
if p.p != nil {
octet |= (1 << bit)
}
bit++
if bit == 8 {
ack = append(ack, octet)
bit = 0
octet = 0
}
}
if bit != 0 {
ack = append(ack, octet)
}
for len(ack) > 0 && ack[len(ack)-1] == 0 {
ack = ack[:len(ack)-1]
}
return ack
}
func (b *packetBuffer) processSelectiveACK(ack []byte) {
if b.empty() {
return
}
p := b.root.next
if p == nil {
return
}
for _, a := range ack {
for i := 0; i < 8; i++ {
acked := (a & 1) != 0
a >>= 1
if acked {
p.p = nil
}
p = p.next
if p == nil {
return
}
}
}
}
type timedBuffer struct {
d time.Duration
root *timedBufferNode
}
type timedBufferNode struct {
val float64
next *timedBufferNode
pushed time.Time
}
func (b *timedBuffer) push(val float64) {
var before *timedBufferNode
for n := b.root; n != nil; n = n.next {
if time.Now().Sub(n.pushed) >= b.d {
if before != nil {
before.next = nil
} else {
b.root = nil
}
break
}
before = n
}
b.root = &timedBufferNode{
val: val,
next: b.root,
pushed: time.Now(),
}
}
func (b *timedBuffer) min() float64 {
if b.root == nil {
return 0
}
min := b.root.val
for n := b.root; n != nil; n = n.next {
if min > n.val {
min = n.val
}
}
return min
}