forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 9
/
frame_sorter.go
219 lines (192 loc) · 5.45 KB
/
frame_sorter.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
package quic
import (
"errors"
"github.com/sagernet/quic-go/internal/protocol"
"github.com/sagernet/quic-go/internal/utils"
"github.com/sagernet/quic-go/internal/utils/tree"
)
// byteInterval is an interval from one ByteCount to the other
type byteInterval struct {
Start protocol.ByteCount
End protocol.ByteCount
}
type frameSorterEntry struct {
Data []byte
DoneCb func()
}
type frameSorter struct {
queue map[protocol.ByteCount]frameSorterEntry
readPos protocol.ByteCount
gapTree *tree.Btree[utils.ByteInterval]
}
var errDuplicateStreamData = errors.New("duplicate stream data")
func newFrameSorter() *frameSorter {
s := frameSorter{
gapTree: tree.New[utils.ByteInterval](),
queue: make(map[protocol.ByteCount]frameSorterEntry),
}
s.gapTree.Insert(utils.ByteInterval{Start: 0, End: protocol.MaxByteCount})
return &s
}
func (s *frameSorter) Push(data []byte, offset protocol.ByteCount, doneCb func()) error {
err := s.push(data, offset, doneCb)
if err == errDuplicateStreamData {
if doneCb != nil {
doneCb()
}
return nil
}
return err
}
func (s *frameSorter) push(data []byte, offset protocol.ByteCount, doneCb func()) error {
if len(data) == 0 {
return errDuplicateStreamData
}
start := offset
end := offset + protocol.ByteCount(len(data))
covInterval := utils.ByteInterval{Start: start, End: end}
gaps := s.gapTree.Match(covInterval)
if len(gaps) == 0 {
// No overlap with any existing gap
return errDuplicateStreamData
}
startGap := gaps[0]
endGap := gaps[len(gaps)-1]
startGapEqualsEndGap := len(gaps) == 1
if startGapEqualsEndGap && end <= startGap.Start {
return errDuplicateStreamData
}
startsInGap := covInterval.Start >= startGap.Start && covInterval.Start <= startGap.End
endsInGap := covInterval.End >= endGap.Start && covInterval.End < endGap.End
startGapEnd := startGap.End // save it, in case startGap is modified
endGapStart := endGap.Start // save it, in case endGap is modified
endGapEnd := endGap.End // save it, in case endGap is modified
var adjustedStartGapEnd bool
var wasCut bool
pos := start
var hasReplacedAtLeastOne bool
for {
oldEntry, ok := s.queue[pos]
if !ok {
break
}
oldEntryLen := protocol.ByteCount(len(oldEntry.Data))
if end-pos > oldEntryLen || (hasReplacedAtLeastOne && end-pos == oldEntryLen) {
// The existing frame is shorter than the new frame. Replace it.
delete(s.queue, pos)
pos += oldEntryLen
hasReplacedAtLeastOne = true
if oldEntry.DoneCb != nil {
oldEntry.DoneCb()
}
} else {
if !hasReplacedAtLeastOne {
return errDuplicateStreamData
}
// The existing frame is longer than the new frame.
// Cut the new frame such that the end aligns with the start of the existing frame.
data = data[:pos-start]
end = pos
wasCut = true
break
}
}
if !startsInGap && !hasReplacedAtLeastOne {
// cut the frame, such that it starts at the start of the gap
data = data[startGap.Start-start:]
start = startGap.Start
wasCut = true
}
if start <= startGap.Start {
if end >= startGap.End {
// The frame covers the whole startGap. Delete the gap.
s.gapTree.Delete(startGap)
} else {
s.gapTree.Delete(startGap)
startGap.Start = end
// Re-insert the gap, but with the new start.
s.gapTree.Insert(startGap)
}
} else if !hasReplacedAtLeastOne {
s.gapTree.Delete(startGap)
startGap.End = start
// Re-insert the gap, but with the new end.
s.gapTree.Insert(startGap)
adjustedStartGapEnd = true
}
if !startGapEqualsEndGap {
s.deleteConsecutive(startGapEnd)
for _, g := range gaps[1:] {
if g.End >= endGapStart {
break
}
s.deleteConsecutive(g.End)
s.gapTree.Delete(g)
}
}
if !endsInGap && start != endGapEnd && end > endGapEnd {
// cut the frame, such that it ends at the end of the gap
data = data[:endGapEnd-start]
end = endGapEnd
wasCut = true
}
if end == endGapEnd {
if !startGapEqualsEndGap {
// The frame covers the whole endGap. Delete the gap.
s.gapTree.Delete(endGap)
}
} else {
if startGapEqualsEndGap && adjustedStartGapEnd {
// The frame split the existing gap into two.
s.gapTree.Insert(utils.ByteInterval{Start: end, End: startGapEnd})
} else if !startGapEqualsEndGap {
s.gapTree.Delete(endGap)
endGap.Start = end
// Re-insert the gap, but with the new start.
s.gapTree.Insert(endGap)
}
}
if wasCut && len(data) < protocol.MinStreamFrameBufferSize {
newData := make([]byte, len(data))
copy(newData, data)
data = newData
if doneCb != nil {
doneCb()
doneCb = nil
}
}
if s.gapTree.Len() > protocol.MaxStreamFrameSorterGaps {
return errors.New("too many gaps in received data")
}
s.queue[start] = frameSorterEntry{Data: data, DoneCb: doneCb}
return nil
}
// deleteConsecutive deletes consecutive frames from the queue, starting at pos
func (s *frameSorter) deleteConsecutive(pos protocol.ByteCount) {
for {
oldEntry, ok := s.queue[pos]
if !ok {
break
}
oldEntryLen := protocol.ByteCount(len(oldEntry.Data))
delete(s.queue, pos)
if oldEntry.DoneCb != nil {
oldEntry.DoneCb()
}
pos += oldEntryLen
}
}
func (s *frameSorter) Pop() (protocol.ByteCount, []byte, func()) {
entry, ok := s.queue[s.readPos]
if !ok {
return s.readPos, nil, nil
}
delete(s.queue, s.readPos)
offset := s.readPos
s.readPos += protocol.ByteCount(len(entry.Data))
return offset, entry.Data, entry.DoneCb
}
// HasMoreData says if there is any more data queued at *any* offset.
func (s *frameSorter) HasMoreData() bool {
return len(s.queue) > 0
}