route/vendor/github.com/lucas-clemente/quic-go/stream_framer.go

167 lines
4.7 KiB
Go
Raw Normal View History

2017-12-12 02:51:45 +00:00
package quic
import (
2018-01-03 19:19:49 +00:00
"github.com/lucas-clemente/quic-go/internal/flowcontrol"
"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-03 19:19:49 +00:00
streamsMap *streamsMap
cryptoStream streamI
version protocol.VersionNumber
2017-12-12 02:51:45 +00:00
2018-01-03 19:19:49 +00:00
connFlowController flowcontrol.ConnectionFlowController
2017-12-12 02:51:45 +00:00
2018-01-03 19:19:49 +00:00
retransmissionQueue []*wire.StreamFrame
blockedFrameQueue []wire.Frame
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
func newStreamFramer(
cryptoStream streamI,
streamsMap *streamsMap,
cfc flowcontrol.ConnectionFlowController,
v protocol.VersionNumber,
) *streamFramer {
2017-12-12 02:51:45 +00:00
return &streamFramer{
streamsMap: streamsMap,
2018-01-03 19:19:49 +00:00
cryptoStream: cryptoStream,
connFlowController: cfc,
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-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)...)
}
2018-01-03 19:19:49 +00:00
func (f *streamFramer) PopBlockedFrame() wire.Frame {
2017-12-12 02:51:45 +00:00
if len(f.blockedFrameQueue) == 0 {
return nil
}
frame := f.blockedFrameQueue[0]
f.blockedFrameQueue = f.blockedFrameQueue[1:]
return frame
}
func (f *streamFramer) HasFramesForRetransmission() bool {
return len(f.retransmissionQueue) > 0
}
2018-01-03 19:19:49 +00:00
func (f *streamFramer) HasCryptoStreamFrame() bool {
return f.cryptoStream.HasDataForWriting()
}
// TODO(lclemente): This is somewhat duplicate with the normal path for generating frames.
func (f *streamFramer) PopCryptoStreamFrame(maxLen protocol.ByteCount) *wire.StreamFrame {
if !f.HasCryptoStreamFrame() {
return nil
}
frame := &wire.StreamFrame{
StreamID: f.cryptoStream.StreamID(),
Offset: f.cryptoStream.GetWriteOffset(),
}
frameHeaderBytes, _ := frame.MinLength(f.version) // can never error
frame.Data, frame.FinBit = f.cryptoStream.GetDataForWriting(maxLen - frameHeaderBytes)
return frame
}
func (f *streamFramer) maybePopFramesForRetransmission(maxLen 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-03 19:19:49 +00:00
frameHeaderLen, _ := frame.MinLength(f.version) // can never error
2017-12-12 02:51:45 +00:00
if currentLen+frameHeaderLen >= maxLen {
break
}
currentLen += frameHeaderLen
splitFrame := maybeSplitOffFrame(frame, maxLen-currentLen)
if splitFrame != nil { // StreamFrame was split
res = append(res, splitFrame)
currentLen += splitFrame.DataLen()
break
}
f.retransmissionQueue = f.retransmissionQueue[1:]
res = append(res, frame)
currentLen += frame.DataLen()
}
return
}
2018-01-03 19:19:49 +00:00
func (f *streamFramer) maybePopNormalFrames(maxBytes protocol.ByteCount) (res []*wire.StreamFrame) {
frame := &wire.StreamFrame{DataLenPresent: true}
2017-12-12 02:51:45 +00:00
var currentLen protocol.ByteCount
2018-01-03 19:19:49 +00:00
fn := func(s streamI) (bool, error) {
2017-12-12 02:51:45 +00:00
if s == nil {
return true, nil
}
2018-01-03 19:19:49 +00:00
frame.StreamID = s.StreamID()
frame.Offset = s.GetWriteOffset()
2017-12-12 02:51:45 +00:00
// not perfect, but thread-safe since writeOffset is only written when getting data
2018-01-03 19:19:49 +00:00
frameHeaderBytes, _ := frame.MinLength(f.version) // can never error
2017-12-12 02:51:45 +00:00
if currentLen+frameHeaderBytes > maxBytes {
return false, nil // theoretically, we could find another stream that fits, but this is quite unlikely, so we stop here
}
maxLen := maxBytes - currentLen - frameHeaderBytes
2018-01-03 19:19:49 +00:00
if s.HasDataForWriting() {
frame.Data, frame.FinBit = s.GetDataForWriting(maxLen)
2017-12-12 02:51:45 +00:00
}
2018-01-03 19:19:49 +00:00
if len(frame.Data) == 0 && !frame.FinBit {
2017-12-12 02:51:45 +00:00
return true, nil
}
// Finally, check if we are now FC blocked and should queue a BLOCKED frame
2018-01-03 19:19:49 +00:00
if !frame.FinBit && s.IsFlowControlBlocked() {
f.blockedFrameQueue = append(f.blockedFrameQueue, &wire.StreamBlockedFrame{StreamID: s.StreamID()})
}
if f.connFlowController.IsBlocked() {
f.blockedFrameQueue = append(f.blockedFrameQueue, &wire.BlockedFrame{})
2017-12-12 02:51:45 +00:00
}
res = append(res, frame)
currentLen += frameHeaderBytes + frame.DataLen()
if currentLen == maxBytes {
return false, nil
}
2018-01-03 19:19:49 +00:00
frame = &wire.StreamFrame{DataLenPresent: true}
2017-12-12 02:51:45 +00:00
return true, nil
}
f.streamsMap.RoundRobinIterate(fn)
return
}
// 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,
}
}