1 #[cfg(test)]
2 mod transport_cc_extension_test;
3 
4 use crate::error::Error;
5 use serde::{Deserialize, Serialize};
6 use util::marshal::{Marshal, MarshalSize, Unmarshal};
7 
8 use bytes::{Buf, BufMut};
9 
10 // transport-wide sequence
11 pub const TRANSPORT_CC_EXTENSION_SIZE: usize = 2;
12 
13 /// TransportCCExtension is a extension payload format in
14 /// https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01
15 /// 0                   1                   2                   3
16 /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
17 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18 /// |       0xBE    |    0xDE       |           length=1            |
19 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20 /// |  ID   | L=1   |transport-wide sequence number | zero padding  |
21 /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22 #[derive(PartialEq, Eq, Debug, Default, Copy, Clone, Serialize, Deserialize)]
23 pub struct TransportCcExtension {
24     pub transport_sequence: u16,
25 }
26 
27 impl Unmarshal for TransportCcExtension {
28     /// Unmarshal parses the passed byte slice and stores the result in the members
unmarshal<B>(raw_packet: &mut B) -> Result<Self, util::Error> where Self: Sized, B: Buf,29     fn unmarshal<B>(raw_packet: &mut B) -> Result<Self, util::Error>
30     where
31         Self: Sized,
32         B: Buf,
33     {
34         if raw_packet.remaining() < TRANSPORT_CC_EXTENSION_SIZE {
35             return Err(Error::ErrBufferTooSmall.into());
36         }
37         let b0 = raw_packet.get_u8();
38         let b1 = raw_packet.get_u8();
39 
40         let transport_sequence = ((b0 as u16) << 8) | b1 as u16;
41         Ok(TransportCcExtension { transport_sequence })
42     }
43 }
44 
45 impl MarshalSize for TransportCcExtension {
46     /// MarshalSize returns the size of the TransportCcExtension once marshaled.
marshal_size(&self) -> usize47     fn marshal_size(&self) -> usize {
48         TRANSPORT_CC_EXTENSION_SIZE
49     }
50 }
51 
52 impl Marshal for TransportCcExtension {
53     /// Marshal serializes the members to buffer
marshal_to(&self, mut buf: &mut [u8]) -> Result<usize, util::Error>54     fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize, util::Error> {
55         if buf.remaining_mut() < TRANSPORT_CC_EXTENSION_SIZE {
56             return Err(Error::ErrBufferTooSmall.into());
57         }
58         buf.put_u16(self.transport_sequence);
59         Ok(TRANSPORT_CC_EXTENSION_SIZE)
60     }
61 }
62