-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
492 lines (429 loc) · 13.4 KB
/
stream.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package quic
import (
"fmt"
"sync"
qerror "github.com/ami-GS/gQUIC/error"
"github.com/ami-GS/gQUIC/qtype"
"github.com/ami-GS/gQUIC/utils"
)
type Stream interface {
GetState() qtype.StreamState
GetID() qtype.StreamID
Close()
IsTerminated() bool
QueueFrame(f StreamLevelFrame) error
UpdateConnectionByteSent()
UpdateConnectionByteReceived()
UpdateStreamOffsetSent(offset qtype.QuicInt)
UpdateStreamOffsetReceived(offset qtype.QuicInt)
handleMaxStreamDataFrame(f *MaxStreamDataFrame) error
handleStopSendingFrame(f *StopSendingFrame) error
handleRstStreamFrame(f *RstStreamFrame) error
handleStreamFrame(f *StreamFrame) error
handleStreamBlockedFrame(f *StreamBlockedFrame) error
}
type BaseStream struct {
ID qtype.StreamID
State qtype.StreamState
sess *Session
largestOffset qtype.QuicInt
flowcontroller *StreamFlowController
}
func (s BaseStream) GetState() qtype.StreamState {
return s.State
}
func (s BaseStream) GetID() qtype.StreamID {
return s.ID
}
func (s BaseStream) UpdateConnectionByteSent() {
s.flowcontroller.connFC.updateByteSent(s.flowcontroller.largestSent)
}
func (s BaseStream) UpdateConnectionByteReceived() {
s.flowcontroller.connFC.updateByteReceived(s.flowcontroller.largestReceived)
}
func (s BaseStream) UpdateStreamOffsetSent(offset qtype.QuicInt) {
s.flowcontroller.updateLargestSent(offset)
}
func (s BaseStream) UpdateStreamOffsetReceived(offset qtype.QuicInt) {
s.flowcontroller.updateLargestReceived(offset)
}
func (s BaseStream) String() string {
return fmt.Sprintf("%s, State:%s\n%s", s.ID, s.State, s.flowcontroller)
}
type SendStream struct {
*BaseStream
// application data can be buffered at "Ready" state
SendBuffer []byte
blockedFramesOnStream *utils.RingBuffer
blockedFrameMutex *sync.Mutex
stopSendingCh chan struct{}
}
func newSendStream(streamID qtype.StreamID, sess *Session) *SendStream {
return &SendStream{
BaseStream: &BaseStream{
ID: streamID,
State: qtype.StreamReady,
sess: sess,
// TODO: need to check default MAX_STREAM_DATA
flowcontroller: NewStreamFlowController(streamID == 0, sess.flowController),
},
blockedFramesOnStream: utils.NewRingBuffer(100),
blockedFrameMutex: new(sync.Mutex),
stopSendingCh: make(chan struct{}, 1),
// TODO : need to be able to set initial windowsize
//FlowControllBlocked: false,
}
}
func (s *SendStream) Close() {
// implicitely close
s.State = qtype.StreamDataRecvd
}
func (s SendStream) IsTerminated() bool {
return s.State == qtype.StreamDataRecvd || s.State == qtype.StreamResetRecvd
}
// QueueFrame is used for validate the frame can be sent, and then queue the frame
func (s *SendStream) QueueFrame(f StreamLevelFrame) (err error) {
if s.IsTerminated() {
// MUST NOT send any frame in the states above
return nil
}
switch frame := f.(type) {
case *StreamFrame:
if s.State == qtype.StreamResetSent {
// MUST NOT send Stream frame in the states above
return nil
}
dataOffset := frame.Offset
switch s.flowcontroller.SendableByOffset(dataOffset) {
case Sendable:
err = s.sendStreamFrame(frame)
case StreamBlocked:
s.blockedFramesOnStream.Enqueue(frame)
err = s.QueueFrame(NewStreamBlockedFrame(s.GetID(), dataOffset))
return nil
}
case *StreamBlockedFrame:
if s.State == qtype.StreamResetSent {
// MUST NOT send StreamBlocked frame in the states above
return nil
}
err = s.sendStreamBlockedFrame(frame)
case *RstStreamFrame:
err = s.sendRstStreamFrame(frame)
default:
// TODO: error
return nil
}
//qtype.MaxPayloadSizeIPv4*0.8 = 958.4
if float64(f.(Frame).GetWireSize()) >= qtype.HighPriorityWireSizeThreshold {
s.sess.sendFrameHPChan <- f.(Frame)
} else {
s.sess.sendFrameChan <- f.(Frame)
}
return err
}
func (s *SendStream) resendBlockedFrames() error {
// TODO: be careful for multithread
s.blockedFrameMutex.Lock()
defer s.blockedFrameMutex.Unlock()
size := s.blockedFramesOnStream.Size()
for i := 0; i < size; i++ {
f := s.blockedFramesOnStream.Dequeue().(*StreamFrame)
err := s.QueueFrame(f)
if err != nil {
return err
}
}
// send quickly
s.sess.AssembleFrameChan <- struct{}{}
return nil
}
func (s *SendStream) sendStreamFrame(f *StreamFrame) error {
if s.State == qtype.StreamReady {
s.State = qtype.StreamSend
} else if s.State == qtype.StreamSend && f.Finish {
s.State = qtype.StreamDataSent
}
s.UpdateStreamOffsetSent(f.Offset)
// update for connection was already done in stream_manager
return nil
}
func (s *SendStream) sendStreamBlockedFrame(f *StreamBlockedFrame) error {
if s.State == qtype.StreamReady {
s.State = qtype.StreamSend
}
return nil
}
func (s *SendStream) sendRstStreamFrame(f *RstStreamFrame) error {
s.State = qtype.StreamResetSent
s.sess.UpdateConnectionOffsetSent(f.FinalOffset)
//TODO: ResetRecvd after Ack
return nil
}
// SendStream handle MaxStreamDataFrame for flow control
func (s *SendStream) handleMaxStreamDataFrame(f *MaxStreamDataFrame) error {
if s.State == qtype.StreamDataSent {
// ignore after being "Sent" state
return nil
}
var err error
if s.flowcontroller.maybeUpdateMaxDataLimit(f.Data) {
err = s.resendBlockedFrames()
}
return err
}
// SendStream handle StopSending for receiving abondon request
func (s *SendStream) handleStopSendingFrame(f *StopSendingFrame) error {
if s.State == qtype.StreamReady {
return qerror.ProtocolViolation
}
// respond by RstStreamFrame with error code of STOPPING
s.stopSendingCh <- struct{}{}
return s.QueueFrame(NewRstStreamFrame(s.ID, qerror.Stopping, qtype.QuicInt(s.largestOffset)))
}
// AckFrame comes via connection level handling
func (s *SendStream) handleAckFrame(f *AckFrame) error {
if s.State == qtype.StreamDataSent {
// TODO: process all stream data are acked then,
s.State = qtype.StreamDataRecvd
} else if s.State == qtype.StreamResetSent {
s.State = qtype.StreamResetRecvd
}
return nil
}
func (s *SendStream) handleStreamFrame(f *StreamFrame) error {
return qerror.ProtocolViolation
}
func (s *SendStream) handleRstStreamFrame(f *RstStreamFrame) error {
return qerror.ProtocolViolation
}
func (s *SendStream) handleStreamBlockedFrame(f *StreamBlockedFrame) error {
return qerror.ProtocolViolation
}
func (s *SendStream) ackedAllStreamData() {
s.State = qtype.StreamDataRecvd
}
type RecvStream struct {
*BaseStream
DataBuffer *utils.RingBuffer
DataSize qtype.QuicInt // will be known after receiving all data
ReceiveAllDetector qtype.QuicInt
}
func newRecvStream(streamID qtype.StreamID, sess *Session) *RecvStream {
return &RecvStream{
BaseStream: &BaseStream{
ID: streamID,
State: qtype.StreamRecv,
sess: sess,
// TODO: need to check default MAX_DATA
flowcontroller: NewStreamFlowController(streamID == 0, sess.flowController),
},
// TODO: need to adjust buffer size for data transfered
DataBuffer: utils.NewRingBuffer(100),
}
}
func (s *RecvStream) Close() {
// implicitely close
s.State = qtype.StreamDataRead
}
func (s RecvStream) IsTerminated() bool {
return s.State == qtype.StreamDataRead || s.State == qtype.StreamResetRead
}
// returns data, is_reset
func (s *RecvStream) ReadData() ([]byte, bool) {
// TODO: blocked until all data received and reordered?
// should be implemented by two channel (dataCh and RstCh)
if s.State == qtype.StreamResetRecvd {
s.State = qtype.StreamResetRead
return nil, true
}
out := make([]byte, s.DataSize)
size := s.DataBuffer.Size()
for i := 0; i < size; i++ {
item := s.DataBuffer.Dequeue().(*utils.StreamData)
copy(out[item.Offset-uint64(len(item.Data)):], item.Data)
}
s.State = qtype.StreamDataRead
return out, false
}
// QueueFrame is used for validate the frame can be sent, and then queue the frame
func (s *RecvStream) QueueFrame(f StreamLevelFrame) (err error) {
if s.IsTerminated() {
// MUST NOT send any frame in the states above
return nil
}
switch frame := f.(type) {
case *MaxStreamDataFrame:
err = s.sendMaxStreamDataFrame(frame)
case *StopSendingFrame:
err = s.sendStopSendingFrame(frame)
default:
// TODO: error
return nil
}
s.sess.sendFrameChan <- f.(Frame)
return err
}
func (s *RecvStream) sendMaxStreamDataFrame(f *MaxStreamDataFrame) error {
//The receiver only sends MAX_STREAM_DATA in the "Recv" state
if s.State != qtype.StreamRecv {
return nil
}
s.flowcontroller.maybeUpdateMaxDataLimit(f.Data)
return nil
}
func (s *RecvStream) sendStopSendingFrame(f *StopSendingFrame) error {
// A receiver can send STOP_SENDING in any state where it has not received a
// RST_STREAM frame; that is states other than "Reset Recvd" or "Reset Read"
if s.State == qtype.StreamResetRecvd || s.State == qtype.StreamResetRead {
return nil
}
return nil
}
func (s *RecvStream) handleMaxStreamDataFrame(f *MaxStreamDataFrame) error {
return qerror.ProtocolViolation
}
func (s *RecvStream) handleStopSendingFrame(f *StopSendingFrame) error {
return qerror.ProtocolViolation
}
func (s *RecvStream) handleRstStreamFrame(f *RstStreamFrame) error {
if f.FinalOffset < s.largestOffset ||
s.State == qtype.StreamSizeKnown && f.FinalOffset != s.largestOffset {
return qerror.FinalOffsetError
}
if s.State == qtype.StreamDataRecvd {
// Optional
s.State = qtype.StreamResetRecvd
} else if s.State == qtype.StreamRecv || s.State == qtype.StreamSizeKnown {
s.State = qtype.StreamResetRecvd
}
// discard data received
s.DataBuffer.DeleteAll()
s.sess.SetFinishedStream(s)
return nil
}
func (s *RecvStream) handleStreamFrame(f *StreamFrame) error {
if s.State == qtype.StreamDataRecvd {
// ignore after receiving all data
return nil
}
offsetValue := f.Offset
err := s.flowcontroller.ReceivableByOffset(offsetValue)
if err != nil {
return err
}
if s.State == qtype.StreamSizeKnown {
if s.largestOffset < f.Offset {
return qerror.FinalOffsetError
}
}
if f.Finish {
s.State = qtype.StreamSizeKnown
s.DataSize = offsetValue
}
if s.largestOffset < f.Offset {
s.largestOffset = f.Offset
}
// TODO: copy workaround of data corrupting issue
data := make([]byte, len(f.Data))
copy(data, f.Data)
s.DataBuffer.Enqueue(&utils.StreamData{uint64(offsetValue), data})
s.ReceiveAllDetector = s.ReceiveAllDetector ^ offsetValue ^ (offsetValue - f.Length)
if s.State == qtype.StreamSizeKnown && s.ReceiveAllDetector != 0 && s.ReceiveAllDetector == s.DataSize {
s.State = qtype.StreamDataRecvd
s.sess.SetFinishedStream(s)
}
s.UpdateStreamOffsetReceived(f.Offset)
return nil
}
func (s *RecvStream) handleStreamBlockedFrame(f *StreamBlockedFrame) error {
if s.State == qtype.StreamDataRecvd {
// ignore after receiving all data
return nil
}
return s.QueueFrame(NewMaxStreamDataFrame(f.GetStreamID(), f.Offset))
}
type SendRecvStream struct {
*BaseStream
*SendStream
*RecvStream
}
func newSendRecvStream(streamID qtype.StreamID, sess *Session) *SendRecvStream {
return &SendRecvStream{
BaseStream: &BaseStream{
ID: streamID,
State: qtype.StreamIdle,
sess: sess,
// flow controll should be done in each send and recv stream bellows?
flowcontroller: nil,
},
SendStream: newSendStream(streamID, sess),
RecvStream: newRecvStream(streamID, sess),
}
}
func (s *SendRecvStream) handleRstStreamFrame(f *RstStreamFrame) error {
err := s.RecvStream.handleRstStreamFrame(f)
if err != nil {
return err
}
if s.RecvStream.State == qtype.StreamResetRecvd {
if s.SendStream.State == qtype.StreamReady || s.SendStream.State == qtype.StreamSend || s.SendStream.State == qtype.StreamDataSent {
s.State = qtype.StreamHalfClosed
} else {
s.State = qtype.StreamClosed
}
} else {
// would be impossible to reach here
}
return nil
}
func (s *SendRecvStream) handleMaxStreamDataFrame(f *MaxStreamDataFrame) error {
return s.SendStream.handleMaxStreamDataFrame(f)
}
func (s *SendRecvStream) handleStopSendingFrame(f *StopSendingFrame) error {
return s.SendStream.handleStopSendingFrame(f)
}
func (s *SendRecvStream) handleStreamBlockedFrame(f *StreamBlockedFrame) error {
return s.RecvStream.handleStreamBlockedFrame(f)
}
func (s *SendRecvStream) handleStreamFrame(f *StreamFrame) error {
err := s.RecvStream.handleStreamFrame(f)
if err != nil {
return err
}
if s.RecvStream.State == qtype.StreamSizeKnown {
if s.SendStream.State == qtype.StreamReady || s.SendStream.State == qtype.StreamSend || s.SendStream.State == qtype.StreamDataSent {
s.State = qtype.StreamOpen
} else if s.SendStream.State == qtype.StreamDataRecvd || s.SendStream.State == qtype.StreamResetSent || s.SendStream.State == qtype.StreamResetRecvd {
s.State = qtype.StreamHalfClosed
}
} else if s.RecvStream.State == qtype.StreamDataRecvd {
if s.SendStream.State == qtype.StreamDataRecvd || s.SendStream.State == qtype.StreamResetSent || s.SendStream.State == qtype.StreamResetRecvd {
s.State = qtype.StreamClosed
} else if s.SendStream.State == qtype.StreamReady || s.SendStream.State == qtype.StreamSend || s.SendStream.State == qtype.StreamDataSent {
s.State = qtype.StreamHalfClosed
}
}
return nil
}
func (s *SendRecvStream) Close() {
// implicitely close
s.SendStream.Close()
s.RecvStream.Close()
s.State = qtype.StreamClosed
}
func (s *SendRecvStream) IsTerminated() bool {
// TODO: currently s.State doesn't care after sending frame
return s.State == qtype.StreamClosed
}
func (s *SendRecvStream) QueueFrame(f StreamLevelFrame) error {
var err error
switch f.(type) {
case *StreamFrame, *StreamBlockedFrame, *RstStreamFrame:
err = s.SendStream.QueueFrame(f)
case *MaxStreamIDFrame, *StopSendingFrame:
err = s.RecvStream.QueueFrame(f)
}
// TODO: change s.State
return err
}