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

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