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