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