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

     1  package wire
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/apernet/quic-go/internal/protocol"
     7  	"github.com/apernet/quic-go/internal/qerr"
     8  	"github.com/apernet/quic-go/quicvarint"
     9  )
    10  
    11  // A ResetStreamFrame is a RESET_STREAM frame in QUIC
    12  type ResetStreamFrame struct {
    13  	StreamID  protocol.StreamID
    14  	ErrorCode qerr.StreamErrorCode
    15  	FinalSize protocol.ByteCount
    16  }
    17  
    18  func parseResetStreamFrame(r *bytes.Reader, _ protocol.Version) (*ResetStreamFrame, error) {
    19  	var streamID protocol.StreamID
    20  	var byteOffset protocol.ByteCount
    21  	sid, err := quicvarint.Read(r)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  	streamID = protocol.StreamID(sid)
    26  	errorCode, err := quicvarint.Read(r)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	bo, err := quicvarint.Read(r)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	byteOffset = protocol.ByteCount(bo)
    35  
    36  	return &ResetStreamFrame{
    37  		StreamID:  streamID,
    38  		ErrorCode: qerr.StreamErrorCode(errorCode),
    39  		FinalSize: byteOffset,
    40  	}, nil
    41  }
    42  
    43  func (f *ResetStreamFrame) Append(b []byte, _ protocol.Version) ([]byte, error) {
    44  	b = append(b, resetStreamFrameType)
    45  	b = quicvarint.Append(b, uint64(f.StreamID))
    46  	b = quicvarint.Append(b, uint64(f.ErrorCode))
    47  	b = quicvarint.Append(b, uint64(f.FinalSize))
    48  	return b, nil
    49  }
    50  
    51  // Length of a written frame
    52  func (f *ResetStreamFrame) Length(protocol.Version) protocol.ByteCount {
    53  	return 1 + protocol.ByteCount(quicvarint.Len(uint64(f.StreamID))+quicvarint.Len(uint64(f.ErrorCode))+quicvarint.Len(uint64(f.FinalSize)))
    54  }