Skip to main content

rtc_datachannel/message/
message_channel_threshold.rs

1use super::*;
2use shared::error::Result;
3
4/// The data-part of an data-channel CLOSE message without the message type.
5///
6/// # Memory layout
7///
8/// ```plain
9/// 0                   1                   2                   3
10/// 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
11///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12///|  Message Type |
13///+-+-+-+-+-+-+-+-+
14/// ```
15#[derive(Eq, PartialEq, Copy, Clone, Debug)]
16pub enum DataChannelThreshold {
17    Low(u32),
18    High(u32),
19} // internal usage only
20
21impl MarshalSize for DataChannelThreshold {
22    fn marshal_size(&self) -> usize {
23        1 + 4
24    }
25}
26
27impl Marshal for DataChannelThreshold {
28    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
29        match *self {
30            DataChannelThreshold::Low(threshold) => {
31                buf.put_u8(0);
32                buf.put_u32(threshold);
33            }
34            DataChannelThreshold::High(threshold) => {
35                buf.put_u8(1);
36                buf.put_u32(threshold);
37            }
38        }
39
40        Ok(self.marshal_size())
41    }
42}
43
44impl Unmarshal for DataChannelThreshold {
45    fn unmarshal<B>(buf: &mut B) -> Result<Self>
46    where
47        Self: Sized,
48        B: Buf,
49    {
50        let t = buf.get_u8();
51        let v = buf.get_u32();
52        if t == 0 {
53            Ok(DataChannelThreshold::Low(v))
54        } else {
55            Ok(DataChannelThreshold::High(v))
56        }
57    }
58}