| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
// Stream ID and stream index handling. |
| 8 |
|
| 9 |
use std::fmt::{self, Display, Formatter}; |
| 10 |
|
| 11 |
use neqo_common::Role; |
| 12 |
|
| 13 |
/// The type of stream, either Bi-Directional or Uni-Directional. |
| 14 |
#[derive(PartialEq, Debug, Copy, Clone, PartialOrd, Eq, Ord, Hash)] |
| 15 |
pub enum StreamType { |
| 16 |
BiDi, |
| 17 |
UniDi, |
| 18 |
} |
| 19 |
|
| 20 |
#[derive(Debug, Eq, PartialEq, Clone, Copy, Ord, PartialOrd, Hash, Default)] |
| 21 |
pub struct StreamId(u64); |
| 22 |
|
| 23 |
impl StreamId { |
| 24 |
#[must_use] |
| 25 |
pub const fn new(id: u64) -> Self { |
| 26 |
Self(id) |
| 27 |
} |
| 28 |
|
| 29 |
#[must_use] |
| 30 |
pub const fn init(stream_type: StreamType, role: Role) -> Self { |
| 31 |
let type_val = match stream_type { |
| 32 |
StreamType::BiDi => 0, |
| 33 |
StreamType::UniDi => 2, |
| 34 |
}; |
| 35 |
Self(type_val + Self::role_bit(role)) |
| 36 |
} |
| 37 |
|
| 38 |
#[must_use] |
| 39 |
pub const fn as_u64(self) -> u64 { |
| 40 |
self.0 |
| 41 |
} |
| 42 |
|
| 43 |
#[must_use] |
| 44 |
pub const fn is_bidi(self) -> bool { |
| 45 |
self.as_u64() & 0x02 == 0 |
| 46 |
} |
| 47 |
|
| 48 |
#[must_use] |
| 49 |
pub const fn is_uni(self) -> bool { |
| 50 |
!self.is_bidi() |
| 51 |
} |
| 52 |
|
| 53 |
#[must_use] |
| 54 |
pub const fn stream_type(self) -> StreamType { |
| 55 |
if self.is_bidi() { |
| 56 |
StreamType::BiDi |
| 57 |
} else { |
| 58 |
StreamType::UniDi |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
#[must_use] |
| 63 |
pub const fn is_client_initiated(self) -> bool { |
| 64 |
self.as_u64() & 0x01 == 0 |
| 65 |
} |
| 66 |
|
| 67 |
#[must_use] |
| 68 |
pub const fn is_server_initiated(self) -> bool { |
| 69 |
!self.is_client_initiated() |
| 70 |
} |
| 71 |
|
| 72 |
#[must_use] |
| 73 |
pub const fn role(self) -> Role { |
| 74 |
if self.is_client_initiated() { |
| 75 |
Role::Client |
| 76 |
} else { |
| 77 |
Role::Server |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
#[must_use] |
| 82 |
pub const fn is_self_initiated(self, my_role: Role) -> bool { |
| 83 |
match my_role { |
| 84 |
Role::Client if self.is_client_initiated() => true, |
| 85 |
Role::Server if self.is_server_initiated() => true, |
| 86 |
_ => false, |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
#[must_use] |
| 91 |
pub const fn is_remote_initiated(self, my_role: Role) -> bool { |
| 92 |
!self.is_self_initiated(my_role) |
| 93 |
} |
| 94 |
|
| 95 |
#[must_use] |
| 96 |
pub const fn is_send_only(self, my_role: Role) -> bool { |
| 97 |
self.is_uni() && self.is_self_initiated(my_role) |
| 98 |
} |
| 99 |
|
| 100 |
#[must_use] |
| 101 |
pub const fn is_recv_only(self, my_role: Role) -> bool { |
| 102 |
self.is_uni() && self.is_remote_initiated(my_role) |
| 103 |
} |
| 104 |
|
| 105 |
pub fn next(&mut self) { |
| 106 |
self.0 += 4; |
| 107 |
} |
| 108 |
|
| 109 |
/// Return the stream index for this stream ID. |
| 110 |
#[must_use] |
| 111 |
pub const fn index(&self) -> u64 { |
| 112 |
self.0 >> 2 |
| 113 |
} |
| 114 |
|
| 115 |
/// This returns a bit that is shared by all streams created by this role. |
| 116 |
#[must_use] |
| 117 |
pub const fn role_bit(role: Role) -> u64 { |
| 118 |
match role { |
| 119 |
Role::Server => 1, |
| 120 |
Role::Client => 0, |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
impl From<u64> for StreamId { |
| 126 |
fn from(val: u64) -> Self { |
| 127 |
Self::new(val) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
impl From<&u64> for StreamId { |
| 132 |
fn from(val: &u64) -> Self { |
| 133 |
Self::new(*val) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl PartialEq<u64> for StreamId { |
| 138 |
fn eq(&self, other: &u64) -> bool { |
| 139 |
self.as_u64() == *other |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
impl AsRef<u64> for StreamId { |
| 144 |
fn as_ref(&self) -> &u64 { |
| 145 |
&self.0 |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
impl Display for StreamId { |
| 150 |
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 151 |
write!(f, "{}", self.as_u64()) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
#[cfg(test)] |
| 156 |
mod test { |
| 157 |
use neqo_common::Role; |
| 158 |
|
| 159 |
use super::StreamId; |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn bidi_stream_properties() { |
| 163 |
let id1 = StreamId::from(16); |
| 164 |
assert!(id1.is_bidi()); |
| 165 |
assert!(!id1.is_uni()); |
| 166 |
assert!(id1.is_client_initiated()); |
| 167 |
assert!(!id1.is_server_initiated()); |
| 168 |
assert_eq!(id1.role(), Role::Client); |
| 169 |
assert!(id1.is_self_initiated(Role::Client)); |
| 170 |
assert!(!id1.is_self_initiated(Role::Server)); |
| 171 |
assert!(!id1.is_remote_initiated(Role::Client)); |
| 172 |
assert!(id1.is_remote_initiated(Role::Server)); |
| 173 |
assert!(!id1.is_send_only(Role::Server)); |
| 174 |
assert!(!id1.is_send_only(Role::Client)); |
| 175 |
assert!(!id1.is_recv_only(Role::Server)); |
| 176 |
assert!(!id1.is_recv_only(Role::Client)); |
| 177 |
assert_eq!(id1.as_u64(), 16); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn uni_stream_properties() { |
| 182 |
let id2 = StreamId::from(35); |
| 183 |
assert!(!id2.is_bidi()); |
| 184 |
assert!(id2.is_uni()); |
| 185 |
assert!(!id2.is_client_initiated()); |
| 186 |
assert!(id2.is_server_initiated()); |
| 187 |
assert_eq!(id2.role(), Role::Server); |
| 188 |
assert!(!id2.is_self_initiated(Role::Client)); |
| 189 |
assert!(id2.is_self_initiated(Role::Server)); |
| 190 |
assert!(id2.is_remote_initiated(Role::Client)); |
| 191 |
assert!(!id2.is_remote_initiated(Role::Server)); |
| 192 |
assert!(id2.is_send_only(Role::Server)); |
| 193 |
assert!(!id2.is_send_only(Role::Client)); |
| 194 |
assert!(!id2.is_recv_only(Role::Server)); |
| 195 |
assert!(id2.is_recv_only(Role::Client)); |
| 196 |
assert_eq!(id2.as_u64(), 35); |
| 197 |
} |
| 198 |
} |