2017-12-12 02:51:45 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
2018-01-20 18:07:01 +00:00
|
|
|
"sync"
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
"github.com/lucas-clemente/quic-go/internal/protocol"
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/wire"
|
2017-12-12 02:51:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type streamFramer struct {
|
2018-01-20 18:07:01 +00:00
|
|
|
streamGetter streamGetter
|
|
|
|
cryptoStream cryptoStreamI
|
2018-01-03 19:19:49 +00:00
|
|
|
version protocol.VersionNumber
|
2017-12-12 02:51:45 +00:00
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
retransmissionQueue []*wire.StreamFrame
|
2018-01-20 18:07:01 +00:00
|
|
|
|
|
|
|
streamQueueMutex sync.Mutex
|
|
|
|
activeStreams map[protocol.StreamID]struct{}
|
|
|
|
streamQueue []protocol.StreamID
|
|
|
|
hasCryptoStreamData bool
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
func newStreamFramer(
|
2018-01-20 18:07:01 +00:00
|
|
|
cryptoStream cryptoStreamI,
|
|
|
|
streamGetter streamGetter,
|
2018-01-03 19:19:49 +00:00
|
|
|
v protocol.VersionNumber,
|
|
|
|
) *streamFramer {
|
2017-12-12 02:51:45 +00:00
|
|
|
return &streamFramer{
|
2018-01-20 18:07:01 +00:00
|
|
|
streamGetter: streamGetter,
|
|
|
|
cryptoStream: cryptoStream,
|
|
|
|
activeStreams: make(map[protocol.StreamID]struct{}),
|
|
|
|
version: v,
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
func (f *streamFramer) AddFrameForRetransmission(frame *wire.StreamFrame) {
|
2017-12-12 02:51:45 +00:00
|
|
|
f.retransmissionQueue = append(f.retransmissionQueue, frame)
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:07:01 +00:00
|
|
|
func (f *streamFramer) AddActiveStream(id protocol.StreamID) {
|
|
|
|
if id == f.version.CryptoStreamID() { // the crypto stream is handled separately
|
|
|
|
f.streamQueueMutex.Lock()
|
|
|
|
f.hasCryptoStreamData = true
|
|
|
|
f.streamQueueMutex.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.streamQueueMutex.Lock()
|
|
|
|
if _, ok := f.activeStreams[id]; !ok {
|
|
|
|
f.streamQueue = append(f.streamQueue, id)
|
|
|
|
f.activeStreams[id] = struct{}{}
|
|
|
|
}
|
|
|
|
f.streamQueueMutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
func (f *streamFramer) PopStreamFrames(maxLen protocol.ByteCount) []*wire.StreamFrame {
|
2017-12-12 02:51:45 +00:00
|
|
|
fs, currentLen := f.maybePopFramesForRetransmission(maxLen)
|
|
|
|
return append(fs, f.maybePopNormalFrames(maxLen-currentLen)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *streamFramer) HasFramesForRetransmission() bool {
|
|
|
|
return len(f.retransmissionQueue) > 0
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:07:01 +00:00
|
|
|
func (f *streamFramer) HasCryptoStreamData() bool {
|
|
|
|
f.streamQueueMutex.Lock()
|
|
|
|
hasCryptoStreamData := f.hasCryptoStreamData
|
|
|
|
f.streamQueueMutex.Unlock()
|
|
|
|
return hasCryptoStreamData
|
2018-01-03 19:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *streamFramer) PopCryptoStreamFrame(maxLen protocol.ByteCount) *wire.StreamFrame {
|
2018-01-20 18:07:01 +00:00
|
|
|
f.streamQueueMutex.Lock()
|
|
|
|
frame, hasMoreData := f.cryptoStream.popStreamFrame(maxLen)
|
|
|
|
f.hasCryptoStreamData = hasMoreData
|
|
|
|
f.streamQueueMutex.Unlock()
|
2018-01-03 19:19:49 +00:00
|
|
|
return frame
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:07:01 +00:00
|
|
|
func (f *streamFramer) maybePopFramesForRetransmission(maxTotalLen protocol.ByteCount) (res []*wire.StreamFrame, currentLen protocol.ByteCount) {
|
2017-12-12 02:51:45 +00:00
|
|
|
for len(f.retransmissionQueue) > 0 {
|
|
|
|
frame := f.retransmissionQueue[0]
|
|
|
|
frame.DataLenPresent = true
|
|
|
|
|
2018-01-20 18:07:01 +00:00
|
|
|
frameHeaderLen := frame.MinLength(f.version) // can never error
|
|
|
|
maxLen := maxTotalLen - currentLen
|
|
|
|
if frameHeaderLen+frame.DataLen() > maxLen && maxLen < protocol.MinStreamFrameSize {
|
2017-12-12 02:51:45 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:07:01 +00:00
|
|
|
splitFrame := maybeSplitOffFrame(frame, maxLen-frameHeaderLen)
|
2017-12-12 02:51:45 +00:00
|
|
|
if splitFrame != nil { // StreamFrame was split
|
|
|
|
res = append(res, splitFrame)
|
2018-01-20 18:07:01 +00:00
|
|
|
currentLen += frameHeaderLen + splitFrame.DataLen()
|
2017-12-12 02:51:45 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
f.retransmissionQueue = f.retransmissionQueue[1:]
|
|
|
|
res = append(res, frame)
|
2018-01-20 18:07:01 +00:00
|
|
|
currentLen += frameHeaderLen + frame.DataLen()
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-20 18:07:01 +00:00
|
|
|
func (f *streamFramer) maybePopNormalFrames(maxTotalLen protocol.ByteCount) []*wire.StreamFrame {
|
2017-12-12 02:51:45 +00:00
|
|
|
var currentLen protocol.ByteCount
|
2018-01-20 18:07:01 +00:00
|
|
|
var frames []*wire.StreamFrame
|
|
|
|
f.streamQueueMutex.Lock()
|
|
|
|
// pop STREAM frames, until less than MinStreamFrameSize bytes are left in the packet
|
|
|
|
numActiveStreams := len(f.streamQueue)
|
|
|
|
for i := 0; i < numActiveStreams; i++ {
|
|
|
|
if maxTotalLen-currentLen < protocol.MinStreamFrameSize {
|
|
|
|
break
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
2018-01-20 18:07:01 +00:00
|
|
|
id := f.streamQueue[0]
|
|
|
|
f.streamQueue = f.streamQueue[1:]
|
|
|
|
str, err := f.streamGetter.GetOrOpenSendStream(id)
|
|
|
|
if err != nil { // can happen if the stream completed after it said it had data
|
|
|
|
delete(f.activeStreams, id)
|
|
|
|
continue
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
2018-01-20 18:07:01 +00:00
|
|
|
frame, hasMoreData := str.popStreamFrame(maxTotalLen - currentLen)
|
|
|
|
if hasMoreData { // put the stream back in the queue (at the end)
|
|
|
|
f.streamQueue = append(f.streamQueue, id)
|
|
|
|
} else { // no more data to send. Stream is not active any more
|
|
|
|
delete(f.activeStreams, id)
|
2018-01-03 19:19:49 +00:00
|
|
|
}
|
2018-01-20 18:07:01 +00:00
|
|
|
if frame == nil { // can happen if the receiveStream was canceled after it said it had data
|
|
|
|
continue
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
2018-01-20 18:07:01 +00:00
|
|
|
frames = append(frames, frame)
|
|
|
|
currentLen += frame.MinLength(f.version) + frame.DataLen()
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
2018-01-20 18:07:01 +00:00
|
|
|
f.streamQueueMutex.Unlock()
|
|
|
|
return frames
|
2017-12-12 02:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// maybeSplitOffFrame removes the first n bytes and returns them as a separate frame. If n >= len(frame), nil is returned and nothing is modified.
|
2018-01-03 19:19:49 +00:00
|
|
|
func maybeSplitOffFrame(frame *wire.StreamFrame, n protocol.ByteCount) *wire.StreamFrame {
|
2017-12-12 02:51:45 +00:00
|
|
|
if n >= frame.DataLen() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
frame.Data = frame.Data[n:]
|
|
|
|
frame.Offset += n
|
|
|
|
}()
|
|
|
|
|
2018-01-03 19:19:49 +00:00
|
|
|
return &wire.StreamFrame{
|
2017-12-12 02:51:45 +00:00
|
|
|
FinBit: false,
|
|
|
|
StreamID: frame.StreamID,
|
|
|
|
Offset: frame.Offset,
|
|
|
|
Data: frame.Data[:n],
|
|
|
|
DataLenPresent: frame.DataLenPresent,
|
|
|
|
}
|
|
|
|
}
|