1use super::*;
2use shared::error::{Error, Result};
3
4const CHANNEL_TYPE_RELIABLE: u8 = 0x00;
5const CHANNEL_TYPE_RELIABLE_UNORDERED: u8 = 0x80;
6const CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT: u8 = 0x01;
7const CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT_UNORDERED: u8 = 0x81;
8const CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED: u8 = 0x02;
9const CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED_UNORDERED: u8 = 0x82;
10const CHANNEL_TYPE_LEN: usize = 1;
11
12pub const CHANNEL_PRIORITY_BELOW_NORMAL: u16 = 128;
14pub const CHANNEL_PRIORITY_NORMAL: u16 = 256;
15pub const CHANNEL_PRIORITY_HIGH: u16 = 512;
16pub const CHANNEL_PRIORITY_EXTRA_HIGH: u16 = 1024;
17
18#[derive(Default, Eq, PartialEq, Copy, Clone, Debug)]
19pub enum ChannelType {
20 #[default]
23 Reliable,
24 ReliableUnordered,
27 PartialReliableRexmit,
31 PartialReliableRexmitUnordered,
35 PartialReliableTimed,
41 PartialReliableTimedUnordered,
46}
47
48impl MarshalSize for ChannelType {
49 fn marshal_size(&self) -> usize {
50 CHANNEL_TYPE_LEN
51 }
52}
53
54impl Marshal for ChannelType {
55 fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
56 let required_len = self.marshal_size();
57 if buf.remaining_mut() < required_len {
58 return Err(Error::UnexpectedEndOfBuffer {
59 expected: required_len,
60 actual: buf.remaining_mut(),
61 });
62 }
63
64 let byte = match self {
65 Self::Reliable => CHANNEL_TYPE_RELIABLE,
66 Self::ReliableUnordered => CHANNEL_TYPE_RELIABLE_UNORDERED,
67 Self::PartialReliableRexmit => CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT,
68 Self::PartialReliableRexmitUnordered => CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT_UNORDERED,
69 Self::PartialReliableTimed => CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED,
70 Self::PartialReliableTimedUnordered => CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED_UNORDERED,
71 };
72
73 buf.put_u8(byte);
74
75 Ok(1)
76 }
77}
78
79impl Unmarshal for ChannelType {
80 fn unmarshal<B>(buf: &mut B) -> Result<Self>
81 where
82 Self: Sized,
83 B: Buf,
84 {
85 let required_len = CHANNEL_TYPE_LEN;
86 if buf.remaining() < required_len {
87 return Err(Error::UnexpectedEndOfBuffer {
88 expected: required_len,
89 actual: buf.remaining(),
90 });
91 }
92
93 let b0 = buf.get_u8();
94
95 match b0 {
96 CHANNEL_TYPE_RELIABLE => Ok(Self::Reliable),
97 CHANNEL_TYPE_RELIABLE_UNORDERED => Ok(Self::ReliableUnordered),
98 CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT => Ok(Self::PartialReliableRexmit),
99 CHANNEL_TYPE_PARTIAL_RELIABLE_REXMIT_UNORDERED => {
100 Ok(Self::PartialReliableRexmitUnordered)
101 }
102 CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED => Ok(Self::PartialReliableTimed),
103 CHANNEL_TYPE_PARTIAL_RELIABLE_TIMED_UNORDERED => {
104 Ok(Self::PartialReliableTimedUnordered)
105 }
106 _ => Err(Error::InvalidChannelType(b0)),
107 }
108 }
109}
110
111const CHANNEL_OPEN_HEADER_LEN: usize = 11;
112
113#[derive(Eq, PartialEq, Clone, Debug)]
137pub struct DataChannelOpen {
138 pub channel_type: ChannelType,
139 pub priority: u16,
140 pub reliability_parameter: u32,
141 pub label: Vec<u8>,
142 pub protocol: Vec<u8>,
143}
144
145impl MarshalSize for DataChannelOpen {
146 fn marshal_size(&self) -> usize {
147 let label_len = self.label.len();
148 let protocol_len = self.protocol.len();
149
150 CHANNEL_OPEN_HEADER_LEN + label_len + protocol_len
151 }
152}
153
154impl Marshal for DataChannelOpen {
155 fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
156 let required_len = self.marshal_size();
157 if buf.remaining_mut() < required_len {
158 return Err(Error::UnexpectedEndOfBuffer {
159 expected: required_len,
160 actual: buf.remaining_mut(),
161 });
162 }
163
164 let n = self.channel_type.marshal_to(buf)?;
165 buf = &mut buf[n..];
166 buf.put_u16(self.priority);
167 buf.put_u32(self.reliability_parameter);
168 buf.put_u16(self.label.len() as u16);
169 buf.put_u16(self.protocol.len() as u16);
170 buf.put_slice(self.label.as_slice());
171 buf.put_slice(self.protocol.as_slice());
172 Ok(self.marshal_size())
173 }
174}
175
176impl Unmarshal for DataChannelOpen {
177 fn unmarshal<B>(buf: &mut B) -> Result<Self>
178 where
179 B: Buf,
180 {
181 let required_len = CHANNEL_OPEN_HEADER_LEN;
182 if buf.remaining() < required_len {
183 return Err(Error::UnexpectedEndOfBuffer {
184 expected: required_len,
185 actual: buf.remaining(),
186 });
187 }
188
189 let channel_type = ChannelType::unmarshal(buf)?;
190 let priority = buf.get_u16();
191 let reliability_parameter = buf.get_u32();
192 let label_len = buf.get_u16() as usize;
193 let protocol_len = buf.get_u16() as usize;
194
195 let required_len = label_len + protocol_len;
196 if buf.remaining() < required_len {
197 return Err(Error::UnexpectedEndOfBuffer {
198 expected: required_len,
199 actual: buf.remaining(),
200 });
201 }
202
203 let mut label = vec![0; label_len];
204 let mut protocol = vec![0; protocol_len];
205
206 buf.copy_to_slice(&mut label[..]);
207 buf.copy_to_slice(&mut protocol[..]);
208
209 Ok(Self {
210 channel_type,
211 priority,
212 reliability_parameter,
213 label,
214 protocol,
215 })
216 }
217}
218
219#[cfg(test)]
220mod tests {
221 use bytes::{Bytes, BytesMut};
222
223 use super::*;
224
225 #[test]
226 fn test_channel_type_unmarshal_success() -> Result<()> {
227 let mut bytes = Bytes::from_static(&[0x00]);
228 let channel_type = ChannelType::unmarshal(&mut bytes)?;
229
230 assert_eq!(channel_type, ChannelType::Reliable);
231 Ok(())
232 }
233
234 #[test]
235 fn test_channel_type_unmarshal_invalid() -> Result<()> {
236 let mut bytes = Bytes::from_static(&[0x11]);
237 match ChannelType::unmarshal(&mut bytes) {
238 Ok(_) => panic!("expected Error, but got Ok"),
239 Err(err) => {
240 if Error::InvalidChannelType(0x11) == err {
241 return Ok(());
242 }
243 panic!(
244 "unexpected err {:?}, want {:?}",
245 err,
246 Error::InvalidMessageType(0x01)
247 );
248 }
249 }
250 }
251
252 #[test]
253 fn test_channel_type_unmarshal_unexpected_end_of_buffer() -> Result<()> {
254 let mut bytes = Bytes::from_static(&[]);
255 match ChannelType::unmarshal(&mut bytes) {
256 Ok(_) => panic!("expected Error, but got Ok"),
257 Err(err) => {
258 if (Error::UnexpectedEndOfBuffer {
259 expected: 1,
260 actual: 0,
261 }) == err
262 {
263 return Ok(());
264 }
265 panic!(
266 "unexpected err {:?}, want {:?}",
267 err,
268 Error::InvalidMessageType(0x01)
269 );
270 }
271 }
272 }
273
274 #[test]
275 fn test_channel_type_marshal_size() -> Result<()> {
276 let channel_type = ChannelType::Reliable;
277 let marshal_size = channel_type.marshal_size();
278
279 assert_eq!(marshal_size, 1);
280 Ok(())
281 }
282
283 #[test]
284 fn test_channel_type_marshal() -> Result<()> {
285 let mut buf = BytesMut::with_capacity(1);
286 buf.resize(1, 0u8);
287 let channel_type = ChannelType::Reliable;
288 let bytes_written = channel_type.marshal_to(&mut buf)?;
289 assert_eq!(bytes_written, channel_type.marshal_size());
290
291 let bytes = buf.freeze();
292 assert_eq!(&bytes[..], &[0x00]);
293 Ok(())
294 }
295
296 static MARSHALED_BYTES: [u8; 24] = [
297 0x00, 0x0f, 0x35, 0x00, 0xff, 0x0f, 0x35, 0x00, 0x05, 0x00, 0x08, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, ];
305
306 #[test]
307 fn test_channel_open_unmarshal_success() -> Result<()> {
308 let mut bytes = Bytes::from_static(&MARSHALED_BYTES);
309
310 let channel_open = DataChannelOpen::unmarshal(&mut bytes)?;
311
312 assert_eq!(channel_open.channel_type, ChannelType::Reliable);
313 assert_eq!(channel_open.priority, 3893);
314 assert_eq!(channel_open.reliability_parameter, 16715573);
315 assert_eq!(channel_open.label, b"label");
316 assert_eq!(channel_open.protocol, b"protocol");
317 Ok(())
318 }
319
320 #[test]
321 fn test_channel_open_unmarshal_invalid_channel_type() -> Result<()> {
322 let mut bytes = Bytes::from_static(&[
323 0x11, 0x0f, 0x35, 0x00, 0xff, 0x0f, 0x35, 0x00, 0x05, 0x00, 0x08, ]);
329 match DataChannelOpen::unmarshal(&mut bytes) {
330 Ok(_) => panic!("expected Error, but got Ok"),
331 Err(err) => {
332 if Error::InvalidChannelType(0x11) == err {
333 return Ok(());
334 }
335 panic!(
336 "unexpected err {:?}, want {:?}",
337 err,
338 Error::InvalidMessageType(0x01)
339 );
340 }
341 }
342 }
343
344 #[test]
345 fn test_channel_open_unmarshal_unexpected_end_of_buffer() -> Result<()> {
346 let mut bytes = Bytes::from_static(&[0x00; 5]);
347 match DataChannelOpen::unmarshal(&mut bytes) {
348 Ok(_) => panic!("expected Error, but got Ok"),
349 Err(err) => {
350 if (Error::UnexpectedEndOfBuffer {
351 expected: 11,
352 actual: 5,
353 }) == err
354 {
355 return Ok(());
356 }
357 panic!(
358 "unexpected err {:?}, want {:?}",
359 err,
360 Error::InvalidMessageType(0x01)
361 );
362 }
363 }
364 }
365
366 #[test]
367 fn test_channel_open_unmarshal_unexpected_length_mismatch() -> Result<()> {
368 let mut bytes = Bytes::from_static(&[
369 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x08, ]);
375 match DataChannelOpen::unmarshal(&mut bytes) {
376 Ok(_) => panic!("expected Error, but got Ok"),
377 Err(err) => {
378 if (Error::UnexpectedEndOfBuffer {
379 expected: 13,
380 actual: 0,
381 }) == err
382 {
383 return Ok(());
384 }
385 panic!(
386 "unexpected err {:?}, want {:?}",
387 err,
388 Error::InvalidMessageType(0x01)
389 );
390 }
391 }
392 }
393
394 #[test]
395 fn test_channel_open_marshal_size() -> Result<()> {
396 let channel_open = DataChannelOpen {
397 channel_type: ChannelType::Reliable,
398 priority: 3893,
399 reliability_parameter: 16715573,
400 label: b"label".to_vec(),
401 protocol: b"protocol".to_vec(),
402 };
403
404 let marshal_size = channel_open.marshal_size();
405
406 assert_eq!(marshal_size, 11 + 5 + 8);
407 Ok(())
408 }
409
410 #[test]
411 fn test_channel_open_marshal() -> Result<()> {
412 let channel_open = DataChannelOpen {
413 channel_type: ChannelType::Reliable,
414 priority: 3893,
415 reliability_parameter: 16715573,
416 label: b"label".to_vec(),
417 protocol: b"protocol".to_vec(),
418 };
419
420 let mut buf = BytesMut::with_capacity(11 + 5 + 8);
421 buf.resize(11 + 5 + 8, 0u8);
422 let bytes_written = channel_open.marshal_to(&mut buf).unwrap();
423 let bytes = buf.freeze();
424
425 assert_eq!(bytes_written, channel_open.marshal_size());
426 assert_eq!(&bytes[..], &MARSHALED_BYTES);
427 Ok(())
428 }
429}