fire_stream/packet/
packet.rs

1use super::{PacketBytes, Result};
2use bytes::Bytes;
3
4pub trait Packet<B>: Sized
5where B: PacketBytes {// gets created if header and body are ready
6	type Header: PacketHeader;
7
8	fn header(&self) -> &Self::Header;
9
10	fn header_mut(&mut self) -> &mut Self::Header;
11
12	/// Returns an empty header
13	/// this is used internally by this crate.
14	///
15	/// ## Important
16	/// `body_len` needs to return 0
17	fn empty() -> Self;
18
19	/// The `bytes.body()` length should always be the same value has
20	/// `header.body_len()`
21	fn from_bytes_and_header(bytes: B, header: Self::Header) -> Result<Self>;
22
23	fn into_bytes(self) -> B;
24}
25
26// Header should be as small as possible
27pub trait PacketHeader: Clone + Sized {
28	/// how long is the header written to bytes
29	const LEN: u32;
30
31	/// bytes.len() == Self::LEN
32	/// 
33	/// ## Implementor
34	/// If an Error get's returned this means that the stream from where the
35	/// bytes are read will be terminated.
36	fn from_bytes(bytes: Bytes) -> Result<Self>;
37
38	/// Returns the length of the body
39	fn body_len(&self) -> u32;
40
41	fn flags(&self) -> &Flags;
42
43	fn set_flags(&mut self, flags: Flags);
44
45	/// Returns the internal flags.
46	/// 
47	/// ## Note
48	/// This is returned as a number so that outside of this crate
49	/// nobody can rely on the information contained within.
50	fn id(&self) -> u32;
51
52	// /// Sets if this message is push.
53	fn set_id(&mut self, id: u32);
54}
55
56
57
58// we should have a ping && pong
59// a close
60// and a 
61
62
63// if IS_PUSH && IS_REQ (we have an internal message)
64
65macro_rules! kind {
66	($($variant:ident = $val:expr),*) => {
67		#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68		pub(crate) enum Kind {
69			$($variant),*,
70			Unknown
71		}
72
73		impl Kind {
74			fn from_num(num: u8) -> Self {
75				debug_assert!(num <= MAX_KIND);
76				match num {
77					$($val => Self::$variant),*,
78					_ => Self::Unknown
79				}
80			}
81
82			fn as_num(&self) -> u8 {
83				match self {
84					$(Self::$variant => $val),*,
85					Self::Unknown => MAX_KIND
86				}
87			}
88
89			#[cfg_attr(not(feature = "connection"), allow(dead_code))]
90			pub(crate) fn to_str(&self) -> &'static str {
91				match self {
92					$(Self::$variant => stringify!($variant)),*,
93					Self::Unknown => "Unknown"
94				}
95			}
96		}
97	}
98}
99
100// max is 2^4 = 16
101kind!{
102	Request = 1,
103	Response = 2,
104	NoResponse = 3,
105	Stream = 4,
106	RequestReceiver = 5,
107	RequestSender = 6,
108	StreamClosed = 7,
109	Close = 8,
110	Ping = 9,
111	MalformedRequest = 10
112}
113
114// equivalent to expect response
115const KIND_OFFSET: u8 = 4;
116const MAX_KIND: u8 = u8::MAX >> KIND_OFFSET;
117const KIND_MASK: u8 = 0b1111 << KIND_OFFSET;
118// const IS_STREAM: u8 = 1 << 3;
119// const IS_LAST: u8 = 1 << 2;
120
121///  Flags
122/// ```dont_run
123/// +------+----------+
124/// | Kind | Reserved |
125/// +------+----------+
126/// |  4   |     4    |
127/// +------+----------+
128///  In bits
129/// ```
130#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
131pub struct Flags {
132	inner: u8
133}
134
135impl Flags {
136	#[cfg_attr(not(feature = "connection"), allow(dead_code))]
137	pub(crate) fn new(kind: Kind) -> Self {
138		let mut this = Self { inner: 0 };
139		this.set_kind(kind);
140		this
141	}
142
143	pub fn empty() -> Self {
144		Self { inner: 0 }
145	}
146
147	pub fn from_u8(num: u8) -> Result<Self> {
148		Ok(Self { inner: num })
149	}
150
151	pub(crate) fn kind(&self) -> Kind {
152		let num = self.inner >> KIND_OFFSET;
153		Kind::from_num(num)
154	}
155
156	pub(crate) fn set_kind(&mut self, kind: Kind) {
157		let num = kind.as_num();
158		debug_assert!(num < MAX_KIND);
159		// clear mask
160		self.inner &= !KIND_MASK;
161		self.inner |= num << KIND_OFFSET;
162	}
163
164	pub fn as_u8(&self) -> u8 {
165		self.inner
166	}
167
168	pub fn is_request(&self) -> bool {
169		matches!(self.kind(), Kind::Request)
170	}
171
172	pub fn is_response(&self) -> bool {
173		matches!(self.kind(), Kind::Response)
174	}
175}