github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/internal/wire/max_streams_frame.go (about)

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