-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_manager.go
472 lines (438 loc) · 11.7 KB
/
stream_manager.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package quic
import (
"sync"
qerror "github.com/ami-GS/gQUIC/error"
"github.com/ami-GS/gQUIC/qtype"
"github.com/ami-GS/gQUIC/utils"
)
type signedChannel struct {
ch chan struct{}
closed bool
}
type StreamManager struct {
streamMap map[qtype.StreamID]Stream
// would be RcvStream or SendRcvStream
finishedStreams *utils.RingBuffer
maxStreamIDUni qtype.StreamID
maxStreamIDMutex *sync.Mutex
nxtSendStreamIDUni qtype.StreamID
// TODO: should be replaced by atomic add
nxtSendStreamIDUniMutex *sync.Mutex
maxStreamIDBidi qtype.StreamID
nxtStreamIDBidi qtype.StreamID
sess *Session
// TODO: name should be considered
blockedIDs map[qtype.StreamID]*signedChannel
blockedIDsMutex *sync.Mutex
newUniStreamMutex *sync.Mutex
newBidiStreamMutex *sync.Mutex
resendMutex *sync.Mutex
waitReadingChs *utils.RingBuffer
}
func NewStreamManager(sess *Session) *StreamManager {
var nxtUniID, nxtBidiID qtype.StreamID
if sess.isClient {
nxtUniID = 2
nxtBidiID = 0
} else {
nxtUniID = 3
nxtBidiID = 1
}
return &StreamManager{
streamMap: make(map[qtype.StreamID]Stream),
sess: sess,
finishedStreams: utils.NewRingBuffer(20),
// may be set after handshake, or MAX_STREAM_ID frame
maxStreamIDUni: 1,
maxStreamIDMutex: new(sync.Mutex),
nxtSendStreamIDUni: nxtUniID,
nxtSendStreamIDUniMutex: new(sync.Mutex),
maxStreamIDBidi: 100,
nxtStreamIDBidi: nxtBidiID,
blockedIDs: make(map[qtype.StreamID]*signedChannel),
blockedIDsMutex: new(sync.Mutex),
newUniStreamMutex: new(sync.Mutex),
newBidiStreamMutex: new(sync.Mutex),
resendMutex: new(sync.Mutex),
// TODO: should be big enough and be able to configurable
waitReadingChs: utils.NewRingBuffer(30),
}
}
func (s *StreamManager) IsValidID(streamID qtype.StreamID) error {
s.maxStreamIDMutex.Lock()
defer s.maxStreamIDMutex.Unlock()
if streamID&qtype.UnidirectionalStream == qtype.UnidirectionalStream {
// unidirectional
if streamID > s.maxStreamIDUni {
return qerror.StreamIDError
}
} else {
// bidirectional
if streamID > s.maxStreamIDBidi {
return qerror.StreamIDError
}
}
return nil
}
func (s *StreamManager) requestMaxStreamID(targetID qtype.StreamID) {
blockedChan := &signedChannel{make(chan struct{}), false}
s.blockedIDsMutex.Lock()
s.blockedIDs[targetID] = blockedChan
s.blockedIDsMutex.Unlock()
s.sess.sendFrameChan <- NewStreamIDBlockedFrame(targetID)
<-blockedChan.ch
s.blockedIDsMutex.Lock()
delete(s.blockedIDs, targetID)
s.blockedIDsMutex.Unlock()
close(blockedChan.ch)
}
func (s *StreamManager) StartNewSendStream() (Stream, error) {
s.nxtSendStreamIDUniMutex.Lock()
targetID := s.nxtSendStreamIDUni
// TODO: atmic increment?
// looks not working well
s.nxtSendStreamIDUni.Increment()
s.nxtSendStreamIDUniMutex.Unlock()
if targetID > s.maxStreamIDUni {
s.requestMaxStreamID(targetID)
}
stream, _, err := s.GetOrNewStream(targetID, true)
if err != nil {
return nil, err
}
return stream, nil
}
func (s *StreamManager) GetOrNewStream(streamID qtype.StreamID, send bool) (st Stream, isNew bool, err error) {
if streamID == 0 {
return s.getOrNewBidiStream(streamID, s.sess)
}
if streamID&qtype.UnidirectionalStream == qtype.UnidirectionalStream {
st, isNew, err = s.getOrNewUniStream(streamID, s.sess, send)
} else {
st, isNew, err = s.getOrNewBidiStream(streamID, s.sess)
}
if err != nil {
delete(s.streamMap, streamID)
return nil, false, err
}
if isNew {
// check whether ID is larger than MAX_STREAM_ID
err = s.IsValidID(streamID)
if err != nil {
delete(s.streamMap, streamID)
return nil, false, err
}
}
return st, isNew, err
}
func (s *StreamManager) getOrNewUniStream(streamID qtype.StreamID, sess *Session, send bool) (Stream, bool, error) {
s.newUniStreamMutex.Lock()
defer s.newUniStreamMutex.Unlock()
stream, ok := s.streamMap[streamID]
// get
if ok {
if send {
st, ok := stream.(*SendStream)
if !ok {
// TODO: what error?
return nil, false, nil
}
return st, false, nil
}
st, ok := stream.(*RecvStream)
if !ok {
// TODO: what error?
return nil, false, nil
}
return st, false, nil
}
// new
if send {
st := newSendStream(streamID, sess)
s.streamMap[streamID] = st
return st, true, nil
}
st := newRecvStream(streamID, sess)
s.streamMap[streamID] = st
return st, true, nil
}
func (s *StreamManager) getOrNewBidiStream(streamID qtype.StreamID, sess *Session) (*SendRecvStream, bool, error) {
s.newBidiStreamMutex.Lock()
defer s.newBidiStreamMutex.Unlock()
stream, ok := s.streamMap[streamID]
if ok {
st, ok := stream.(*SendRecvStream)
if !ok {
// TODO: what error?
return nil, false, nil
}
return st, false, nil
}
st := newSendRecvStream(streamID, sess)
s.streamMap[streamID] = st
return st, true, nil
}
// called from session.QueueFrame(f)
func (s *StreamManager) QueueFrame(stream Stream, f StreamLevelFrame) error {
sid := f.GetStreamID()
var err error
//var isNew bool
switch frame := f.(type) {
case *StreamFrame:
if sid != 0 && frame.Finish {
if s.sess.flowController.SendableByOffset(frame.Offset) == ConnectionBlocked {
s.sess.streamManager.resendMutex.Lock()
s.sess.blockedFramesOnConnection.Enqueue(frame)
s.sess.streamManager.resendMutex.Unlock()
err = s.sess.QueueFrame(NewBlockedFrame(frame.Offset + s.sess.flowController.bytesSent))
return err
}
s.sess.UpdateConnectionOffsetSent(frame.Offset)
}
if stream == nil {
stream, _, err = s.GetOrNewStream(sid, true)
}
case *RstStreamFrame, *StreamBlockedFrame:
if stream == nil {
stream, _, err = s.GetOrNewStream(sid, true)
}
case *MaxStreamDataFrame, *StopSendingFrame:
if stream == nil {
stream, _, err = s.GetOrNewStream(sid, false)
}
case *MaxStreamIDFrame:
// this is special, affect only stream_manager
//s.sess.sendFrameChan <- f
return nil
case *StreamIDBlockedFrame:
// ??
//s.sess.sendFrameChan <- f
return nil
default:
// error, but impossible to reach here
return nil
}
if err != nil {
return err
}
err = stream.QueueFrame(f)
if err != nil {
return err
}
if stream.IsTerminated() {
stream.UpdateConnectionByteSent()
}
return nil
}
func (s *StreamManager) handleFrame(f StreamLevelFrame) error {
sid := f.GetStreamID()
protocolViolationFunc := func() error {
// An endpoint that receives a MAX_STREAM_DATA frame for a send-only
// stream it has not opened MUST terminate the connection with error
// PROTOCOL_VIOLATION.
delete(s.streamMap, sid)
return qerror.ProtocolViolation
}
var stream Stream
var err error
var isNew bool
switch frame := f.(type) {
case *MaxStreamIDFrame:
return s.handleMaxStreamIDFrame(frame)
case *StreamIDBlockedFrame:
return s.handleStreamIDBlockedFrame(frame)
case *StreamFrame:
stream, isNew, err = s.GetOrNewStream(sid, false)
if err != nil {
return err
}
if sid != 0 && frame.Finish {
err := s.sess.flowController.ReceivableByOffset(frame.Offset)
if err != nil {
return err
}
}
err = stream.handleStreamFrame(frame)
case *RstStreamFrame:
stream, _, err = s.GetOrNewStream(sid, false)
if err != nil {
return err
}
err = stream.handleRstStreamFrame(frame)
case *StreamBlockedFrame:
stream, isNew, err = s.GetOrNewStream(sid, false)
if err != nil {
return err
}
err = stream.handleStreamBlockedFrame(frame)
case *MaxStreamDataFrame:
stream, isNew, err = s.GetOrNewStream(sid, true)
if err != nil {
return err
}
if isNew {
return protocolViolationFunc()
}
err = stream.handleMaxStreamDataFrame(frame)
return err
case *StopSendingFrame:
stream, isNew, err = s.GetOrNewStream(sid, true)
if err != nil {
return err
}
if isNew {
return protocolViolationFunc()
}
err = stream.handleStopSendingFrame(frame)
default:
// error, but impossible to reach here
return nil
}
if err != nil {
return err
}
if stream.IsTerminated() {
stream.UpdateConnectionByteReceived()
}
return err
}
func (s *StreamManager) handleStreamIDBlockedFrame(frame *StreamIDBlockedFrame) error {
// should be from sender stream which needs new ID, but could not open due to limit
s.maxStreamIDMutex.Lock()
// TODO: these setter should be sender of MaxStreamID?
if frame.StreamID&qtype.UnidirectionalStream == qtype.UnidirectionalStream {
// This if is not in spec, but should be needed for unordered StreamIDBlocked Frame
if s.maxStreamIDUni < frame.StreamID {
s.maxStreamIDUni = frame.StreamID
}
} else {
if s.maxStreamIDBidi < frame.StreamID {
s.maxStreamIDBidi = frame.StreamID
}
}
s.maxStreamIDMutex.Unlock()
s.sess.sendFrameChan <- NewMaxStreamIDFrame(frame.StreamID)
return nil
}
func (s *StreamManager) handleMaxStreamIDFrame(frame *MaxStreamIDFrame) error {
s.maxStreamIDMutex.Lock()
defer s.maxStreamIDMutex.Unlock()
sid := frame.StreamID
if sid&qtype.UnidirectionalStream == qtype.UnidirectionalStream {
// unidirectional
if sid < s.maxStreamIDUni {
// ignored
return nil
}
s.maxStreamIDUni = frame.StreamID
} else {
//bidirectional
if sid < s.maxStreamIDBidi {
// ignored
return nil
}
s.maxStreamIDBidi = frame.StreamID
}
s.blockedIDsMutex.Lock()
defer s.blockedIDsMutex.Unlock()
for blockedID, val := range s.blockedIDs {
if !val.closed && sid >= blockedID {
val.closed = true
val.ch <- struct{}{}
}
}
return nil
}
func (s *StreamManager) resendBlockedFrames(blockedFrames *utils.RingBuffer) error {
var stream Stream
var isNew bool
var err error
sID := qtype.StreamID(0)
s.resendMutex.Lock()
size := blockedFrames.Size()
s.resendMutex.Unlock()
for i := 0; i < size; i++ {
s.resendMutex.Lock()
frame := blockedFrames.Dequeue().(*StreamFrame)
s.resendMutex.Unlock()
sID = frame.GetStreamID()
stream, isNew, err = s.GetOrNewStream(sID, true)
if err != nil {
return err
}
if isNew {
// error
delete(s.streamMap, sID)
panic("New stream creation for resending frame")
}
err := s.QueueFrame(stream, frame)
if err != nil {
return err
}
}
return nil
}
func (s *StreamManager) waitData() {
waiting := make(chan struct{})
s.waitReadingChs.Enqueue(&waiting)
<-waiting
close(waiting)
}
func (s *StreamManager) Read() ([]byte, error) {
if s.finishedStreams.Empty() {
s.waitData()
}
stream, ok := s.finishedStreams.Dequeue().(*RecvStream)
if !ok || stream == nil {
return nil, nil
}
data, isReset := stream.ReadData()
// TODO: use isReset to notify the stream was reset
if isReset {
}
return data, nil
}
func (s *StreamManager) Write(data []byte) (n int, err error) {
streamI, err := s.StartNewSendStream()
stream := streamI.(*SendStream)
if err != nil {
return 0, err
}
//return stream.(*SendStream).Write(data)
// 2. loop to make packet which should have bellow or equal to MTUIPv4
READ_LOOP:
for uint64(stream.largestOffset) < uint64(len(data)) {
select {
case <-stream.stopSendingCh:
break READ_LOOP
default:
remainLen := qtype.QuicInt(len(data[stream.largestOffset:]))
if remainLen > qtype.MaxPayloadSizeIPv4 {
stream.largestOffset += qtype.MaxPayloadSizeIPv4
err = s.QueueFrame(stream, NewStreamFrame(stream.ID, qtype.QuicInt(stream.largestOffset),
true, true, false, data[stream.largestOffset-qtype.MaxPayloadSizeIPv4:stream.largestOffset]))
} else {
stream.largestOffset += remainLen
err = s.QueueFrame(stream, NewStreamFrame(stream.ID, qtype.QuicInt(stream.largestOffset),
true, true, true, data[stream.largestOffset-remainLen:]))
}
if err != nil {
return 0, err
}
}
}
return len(data), err
}
func (s *StreamManager) CloseAllStream() error {
// implicitly close all stream
wg := &sync.WaitGroup{}
for _, stream := range s.streamMap {
wg.Add(1)
go func(st Stream) {
st.Close()
wg.Done()
}(stream)
}
wg.Wait()
return nil
}