github.com/danielpfeifer02/quic-go-prio-packs@v0.41.0-28/internal/protocol/stream.go (about)

     1  package protocol
     2  
     3  // StreamType encodes if this is a unidirectional or bidirectional stream
     4  type StreamType uint8
     5  
     6  // PRIO_PACKS_TAG
     7  // Priority encodes the priority of the stream
     8  type Priority int8
     9  
    10  const (
    11  	// StreamTypeUni is a unidirectional stream
    12  	StreamTypeUni StreamType = iota
    13  	// StreamTypeBidi is a bidirectional stream
    14  	StreamTypeBidi
    15  )
    16  
    17  // InvalidPacketNumber is a stream ID that is invalid.
    18  // The first valid stream ID in QUIC is 0.
    19  const InvalidStreamID StreamID = -1
    20  
    21  // StreamNum is the stream number
    22  type StreamNum int64
    23  
    24  const (
    25  	// InvalidStreamNum is an invalid stream number.
    26  	InvalidStreamNum = -1
    27  	// MaxStreamCount is the maximum stream count value that can be sent in MAX_STREAMS frames
    28  	// and as the stream count in the transport parameters
    29  	MaxStreamCount StreamNum = 1 << 60
    30  )
    31  
    32  // StreamID calculates the stream ID.
    33  func (s StreamNum) StreamID(stype StreamType, pers Perspective) StreamID {
    34  	if s == 0 {
    35  		return InvalidStreamID
    36  	}
    37  	var first StreamID
    38  	switch stype {
    39  	case StreamTypeBidi:
    40  		switch pers {
    41  		case PerspectiveClient:
    42  			first = 0
    43  		case PerspectiveServer:
    44  			first = 1
    45  		}
    46  	case StreamTypeUni:
    47  		switch pers {
    48  		case PerspectiveClient:
    49  			first = 2
    50  		case PerspectiveServer:
    51  			first = 3
    52  		}
    53  	}
    54  	return first + 4*StreamID(s-1)
    55  }
    56  
    57  // A StreamID in QUIC
    58  type StreamID int64
    59  
    60  // InitiatedBy says if the stream was initiated by the client or by the server
    61  func (s StreamID) InitiatedBy() Perspective {
    62  	if s%2 == 0 {
    63  		return PerspectiveClient
    64  	}
    65  	return PerspectiveServer
    66  }
    67  
    68  // Type says if this is a unidirectional or bidirectional stream
    69  func (s StreamID) Type() StreamType {
    70  	if s%4 >= 2 {
    71  		return StreamTypeUni
    72  	}
    73  	return StreamTypeBidi
    74  }
    75  
    76  // StreamNum returns how many streams in total are below this
    77  // Example: for stream 9 it returns 3 (i.e. streams 1, 5 and 9)
    78  func (s StreamID) StreamNum() StreamNum {
    79  	return StreamNum(s/4) + 1
    80  }