-
Notifications
You must be signed in to change notification settings - Fork 16
/
tracking.go
184 lines (159 loc) · 3.84 KB
/
tracking.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
// Internal structure indicating packets we have
// received
package minq
import (
"fmt"
"github.com/bifurcation/mint"
"time"
)
type packetData struct {
protected bool
nonAcks bool
pn uint64
t time.Time
acked2 bool
}
type recvdPackets struct {
log loggingFunction
initted bool
minReceived uint64
maxReceived uint64
minNotAcked2 uint64
packets map[uint64]*packetData
unacked bool // Are there packets we haven't generated an ACK for
}
func newRecvdPackets(log loggingFunction) *recvdPackets {
return &recvdPackets{
log, // loggingFunction
false, // initted
0, // minReceived
0, // maxReceived
0, // minNotAcked2
make(map[uint64]*packetData, 0), // packets
false, // unacked
}
}
func (p *recvdPackets) initialized() bool {
return p.initted
}
func (p *recvdPackets) init(pn uint64) {
p.log(logTypeAck, "Initializing received packet start=%x", pn)
p.initted = true
p.minReceived = pn
p.maxReceived = pn
p.minNotAcked2 = pn
}
func (p *recvdPackets) packetNotReceived(pn uint64) bool {
if pn < p.minReceived {
return false
}
_, found := p.packets[pn]
return !found
}
func (p *recvdPackets) packetSetReceived(pn uint64, protected bool, nonAcks bool) {
p.log(logTypeAck, "Setting packet received=%x", pn)
if pn > p.maxReceived {
p.maxReceived = pn
}
if pn < p.minNotAcked2 {
p.minNotAcked2 = pn
}
p.log(logTypeAck, "Setting packet received=%x", pn)
p.packets[pn] = &packetData{
protected,
nonAcks,
pn,
time.Now(),
false,
}
p.unacked = true
}
func (p *recvdPackets) packetSetAcked2(pn uint64) {
p.log(logTypeAck, "Setting packet acked2=%v", pn)
if pn >= p.minNotAcked2 {
pk, ok := p.packets[pn]
if ok {
pk.acked2 = true
}
}
}
func (r *ackRange) String() string {
return fmt.Sprintf("%x(%d)", r.lastPacket, r.count)
}
func (r *ackRanges) String() string {
rsp := ""
for _, s := range *r {
if rsp != "" {
rsp += ", "
}
rsp += s.String()
}
return rsp
}
func (p *recvdPackets) needToAck() bool {
return p.unacked
}
// Prepare a list of the ACK ranges, starting at the highest
func (p *recvdPackets) prepareAckRange(epoch mint.Epoch, allowAckOnly bool) ackRanges {
p.log(logTypeAck, "Prepare ACK range epoch=%d", epoch)
// Don't ACK if there's nothing new to ACK
if !p.unacked {
p.log(logTypeAck, "Nothing new to ACK")
return nil
}
var last uint64
var pn uint64
inrange := false
nonAcks := false
ranges := make(ackRanges, 0)
newMinNotAcked2 := p.maxReceived
// TODO([email protected]): This is kind of a gross hack in case
// someone sends us a 0 initial packet number.
for pn = p.maxReceived; pn >= p.minNotAcked2 && pn > 0; pn-- {
p.log(logTypeTrace, "Examining packet %x", pn)
pk, ok := p.packets[pn]
needs_ack := false
// If we don't know about the packet, or if the ack has been
// acked, we don't need to ack it.
if ok && !pk.acked2 {
needs_ack = true
newMinNotAcked2 = pn
}
if ok && pk.acked2 {
delete(p.packets, pn)
}
if needs_ack {
p.log(logTypeTrace, "Acking packet %x", pn)
}
if needs_ack && pk.nonAcks {
// Note if this is an ack of anything other than
// acks.
p.log(logTypeTrace, "Packet %x contains non-acks", pn)
nonAcks = true
}
if inrange != needs_ack {
if inrange {
// This is the end of a range.
ranges = append(ranges, ackRange{last, last - pn})
} else {
last = pn
}
inrange = needs_ack
}
}
if inrange {
p.log(logTypeTrace, "Appending final range %x-%x", last, pn+1)
ranges = append(ranges, ackRange{last, last - pn})
}
p.minNotAcked2 = newMinNotAcked2
p.log(logTypeAck, "%v ACK ranges to send", len(ranges))
for i, r := range ranges {
p.log(logTypeAck, " %d = %v", i, r.String())
}
if !allowAckOnly && !nonAcks {
p.log(logTypeAck, "No non-ack packets and this ack is not ack-only capable")
return nil
}
p.unacked = false
return ranges
}