github.com/quic-go/quic-go@v0.44.0/internal/wire/max_streams_frame.go (about)

     1  package wire
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/quic-go/quic-go/internal/protocol"
     7  	"github.com/quic-go/quic-go/quicvarint"
     8  )
     9  
    10  // A MaxStreamsFrame is a MAX_STREAMS frame
    11  type MaxStreamsFrame struct {
    12  	Type         protocol.StreamType
    13  	MaxStreamNum protocol.StreamNum
    14  }
    15  
    16  func parseMaxStreamsFrame(b []byte, typ uint64, _ protocol.Version) (*MaxStreamsFrame, int, error) {
    17  	f := &MaxStreamsFrame{}
    18  	switch typ {
    19  	case bidiMaxStreamsFrameType:
    20  		f.Type = protocol.StreamTypeBidi
    21  	case uniMaxStreamsFrameType:
    22  		f.Type = protocol.StreamTypeUni
    23  	}
    24  	streamID, l, err := quicvarint.Parse(b)
    25  	if err != nil {
    26  		return nil, 0, replaceUnexpectedEOF(err)
    27  	}
    28  	f.MaxStreamNum = protocol.StreamNum(streamID)
    29  	if f.MaxStreamNum > protocol.MaxStreamCount {
    30  		return nil, 0, fmt.Errorf("%d exceeds the maximum stream count", f.MaxStreamNum)
    31  	}
    32  	return f, l, nil
    33  }
    34  
    35  func (f *MaxStreamsFrame) Append(b []byte, _ protocol.Version) ([]byte, error) {
    36  	switch f.Type {
    37  	case protocol.StreamTypeBidi:
    38  		b = append(b, bidiMaxStreamsFrameType)
    39  	case protocol.StreamTypeUni:
    40  		b = append(b, uniMaxStreamsFrameType)
    41  	}
    42  	b = quicvarint.Append(b, uint64(f.MaxStreamNum))
    43  	return b, nil
    44  }
    45  
    46  // Length of a written frame
    47  func (f *MaxStreamsFrame) Length(protocol.Version) protocol.ByteCount {
    48  	return 1 + protocol.ByteCount(quicvarint.Len(uint64(f.MaxStreamNum)))
    49  }