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