forked from quic-go/quic-go
/
framer.go
289 lines (240 loc) · 8.32 KB
/
framer.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
package quic
import (
"errors"
"sort"
"sync"
"github.com/TugasAkhir-QUIC/quic-go/internal/ackhandler"
"github.com/TugasAkhir-QUIC/quic-go/internal/protocol"
"github.com/TugasAkhir-QUIC/quic-go/internal/wire"
"github.com/TugasAkhir-QUIC/quic-go/quicvarint"
)
type framer interface {
HasData() bool
QueueControlFrame(wire.Frame)
AppendControlFrames([]ackhandler.Frame, protocol.ByteCount, protocol.Version) ([]ackhandler.Frame, protocol.ByteCount)
AddActiveStream(protocol.StreamID)
AppendStreamFrames([]ackhandler.StreamFrame, protocol.ByteCount, protocol.Version) ([]ackhandler.StreamFrame, protocol.ByteCount)
Handle0RTTRejection() error
}
const maxPathResponses = 256
type framerI struct {
mutex sync.Mutex
streamGetter streamGetter
activeStreams map[protocol.StreamID]struct{}
//streamQueue ringbuffer.RingBuffer[protocol.StreamID]
streamQueue []protocol.StreamID
controlFrameMutex sync.Mutex
controlFrames []wire.Frame
pathResponses []*wire.PathResponseFrame
}
var _ framer = &framerI{}
func newFramer(streamGetter streamGetter) framer {
return &framerI{
streamGetter: streamGetter,
activeStreams: make(map[protocol.StreamID]struct{}),
}
}
func (f *framerI) HasData() bool {
f.mutex.Lock()
hasData := len(f.streamQueue) > 0
f.mutex.Unlock()
if hasData {
return true
}
f.controlFrameMutex.Lock()
defer f.controlFrameMutex.Unlock()
return len(f.controlFrames) > 0 || len(f.pathResponses) > 0
}
func (f *framerI) QueueControlFrame(frame wire.Frame) {
f.controlFrameMutex.Lock()
defer f.controlFrameMutex.Unlock()
if pr, ok := frame.(*wire.PathResponseFrame); ok {
// Only queue up to maxPathResponses PATH_RESPONSE frames.
// This limit should be high enough to never be hit in practice,
// unless the peer is doing something malicious.
if len(f.pathResponses) >= maxPathResponses {
return
}
f.pathResponses = append(f.pathResponses, pr)
return
}
f.controlFrames = append(f.controlFrames, frame)
}
func (f *framerI) AppendControlFrames(frames []ackhandler.Frame, maxLen protocol.ByteCount, v protocol.Version) ([]ackhandler.Frame, protocol.ByteCount) {
f.controlFrameMutex.Lock()
defer f.controlFrameMutex.Unlock()
var length protocol.ByteCount
// add a PATH_RESPONSE first, but only pack a single PATH_RESPONSE per packet
if len(f.pathResponses) > 0 {
frame := f.pathResponses[0]
frameLen := frame.Length(v)
if frameLen <= maxLen {
frames = append(frames, ackhandler.Frame{Frame: frame})
length += frameLen
f.pathResponses = f.pathResponses[1:]
}
}
for len(f.controlFrames) > 0 {
frame := f.controlFrames[len(f.controlFrames)-1]
frameLen := frame.Length(v)
if length+frameLen > maxLen {
break
}
frames = append(frames, ackhandler.Frame{Frame: frame})
length += frameLen
f.controlFrames = f.controlFrames[:len(f.controlFrames)-1]
}
return frames, length
}
func (f *framerI) AddActiveStream(id protocol.StreamID) {
f.mutex.Lock()
if _, ok := f.activeStreams[id]; !ok {
f.streamQueue = append(f.streamQueue, id)
f.activeStreams[id] = struct{}{}
f.sortQueue()
}
f.mutex.Unlock()
}
func (f *framerI) sortQueue() {
// Sort the queue by descending priority order
sort.SliceStable(f.streamQueue, func(i int, j int) bool {
str1, err := f.streamGetter.GetOrOpenSendStream(f.streamQueue[i])
if str1 == nil || err != nil {
return false // Push to the front so we can pop it
}
str2, err := f.streamGetter.GetOrOpenSendStream(f.streamQueue[j])
if str2 == nil || err != nil {
return true // Push to the front so we can pop it
}
return str1.getPriority() > str2.getPriority()
})
}
func (f *framerI) AppendStreamFrames(frames []ackhandler.StreamFrame, maxLen protocol.ByteCount, v protocol.Version) ([]ackhandler.StreamFrame, protocol.ByteCount) {
startLen := len(frames)
var length protocol.ByteCount
f.mutex.Lock()
// TODO perform this sort when SetPriority() is called
f.sortQueue()
// Record information about streams with the same priority
priorityCurrent := 0 // The current priority value
prioritySent := 0 // The number of sent streams with this priority
priorityUnsent := 0 // The number of unsent streams with this priority
i := 0
// pop STREAM frames, until less than MinStreamFrameSize bytes are left in the packet
for i < len(f.streamQueue) {
id := f.streamQueue[i]
// This should never return an error. Better check it anyway.
// The stream will only be in the streamQueue, if it enqueued itself there.
str, err := f.streamGetter.GetOrOpenSendStream(id)
// The stream can be nil if it completed after it said it had data.
if str == nil || err != nil {
delete(f.activeStreams, id)
// Shift the remaining elements in the queue forward
copy(f.streamQueue[i:], f.streamQueue[i+1:])
f.streamQueue = f.streamQueue[:len(f.streamQueue)-1]
// Don't increment i since we just removed an element
continue
}
// Get the priority for the current stream
priority := str.getPriority()
full := protocol.MinStreamFrameSize+length > maxLen
if full {
// If we're full, see if the previous streams had the same priority
if priority != priorityCurrent {
// We can stop interating since we've found all streams with the same priority
break
}
// Keep looping until this is no longer the case.
priorityUnsent += 1
i += 1
continue
}
// See if the previous streams had the same priority
if i == 0 || priority != priorityCurrent {
// We just sent a new priority level; reset our counters
priorityCurrent = priority
priorityUnsent = 0
prioritySent = 0
}
remainingLen := maxLen - length
// For the last STREAM frame, we'll remove the DataLen field later.
// Therefore, we can pretend to have more bytes available when popping
// the STREAM frame (which will always have the DataLen set).
remainingLen += quicvarint.Len(uint64(remainingLen))
frame, ok, hasMoreData := str.popStreamFrame(remainingLen, v)
// The frame can be "nil"
// * if the receiveStream was canceled after it said it had data
// * the remaining size doesn't allow us to add another STREAM frame
if ok {
frames = append(frames, frame)
length += frame.Frame.Length(v)
}
if !hasMoreData {
// no more data to send. Stream is not active any more
delete(f.activeStreams, id)
// Shift the remaining elements in the queue forward
copy(f.streamQueue[i:], f.streamQueue[i+1:])
f.streamQueue = f.streamQueue[:len(f.streamQueue)-1]
// Don't increment i since we just removed an element
continue
}
i += 1
prioritySent += 1
}
if priorityUnsent > 0 && prioritySent > 0 {
// There were some streams sent and some streams unsent within the same priority.
// We want to swap the last `priorityUnsent` values with the prior `prioritySent` values.
// This way we will round-robin streams with the same priority.
swap := make([]protocol.StreamID, prioritySent)
end := i
middle := end - priorityUnsent
start := middle - prioritySent
copy(swap, f.streamQueue[start:middle+1])
copy(f.streamQueue[start:], f.streamQueue[middle:end])
copy(f.streamQueue[end-len(swap):], swap)
// Example:
// i = 7
// streamQueue (priority): [ 7, 7, 5, 5, 5, 5, 5, 2, 2 ]
// priorityUnset = 3
// prioritySent = 2
// We want to move index 2,3 to index 5,6 and index 4,5,6 to index 2,3,4
// end = 7
// middle = 4
// start = 2
// copy(swap, queue[2:5])
// copy(queue[2:], queue[4:7])
// copy(queue[5:], swap)
}
f.mutex.Unlock()
if len(frames) > startLen {
l := frames[len(frames)-1].Frame.Length(v)
// account for the smaller size of the last STREAM frame
frames[len(frames)-1].Frame.DataLenPresent = false
length += frames[len(frames)-1].Frame.Length(v) - l
}
return frames, length
}
func (f *framerI) Handle0RTTRejection() error {
f.mutex.Lock()
defer f.mutex.Unlock()
f.controlFrameMutex.Lock()
f.streamQueue = f.streamQueue[:0]
for id := range f.activeStreams {
delete(f.activeStreams, id)
}
var j int
for i, frame := range f.controlFrames {
switch frame.(type) {
case *wire.MaxDataFrame, *wire.MaxStreamDataFrame, *wire.MaxStreamsFrame:
return errors.New("didn't expect MAX_DATA / MAX_STREAM_DATA / MAX_STREAMS frame to be sent in 0-RTT")
case *wire.DataBlockedFrame, *wire.StreamDataBlockedFrame, *wire.StreamsBlockedFrame:
continue
default:
f.controlFrames[j] = f.controlFrames[i]
j++
}
}
f.controlFrames = f.controlFrames[:j]
f.controlFrameMutex.Unlock()
return nil
}