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)]
16/// A buffered-amount threshold crossing, reported internally so the channel can raise
17/// `OnBufferedAmountLow`/`OnBufferedAmountHigh`.
18///
19/// Not a DCEP message — it never appears on the wire.
20pub enum DataChannelThreshold {
21    /// The buffered amount fell to or below the low threshold, carrying its value.
22    Low(u32),
23    /// The buffered amount rose to or above the high threshold, carrying its value.
24    High(u32),
25} // internal usage only
26
27impl MarshalSize for DataChannelThreshold {
28    fn marshal_size(&self) -> usize {
29        1 + 4
30    }
31}
32
33impl Marshal for DataChannelThreshold {
34    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
35        match *self {
36            DataChannelThreshold::Low(threshold) => {
37                buf.put_u8(0);
38                buf.put_u32(threshold);
39            }
40            DataChannelThreshold::High(threshold) => {
41                buf.put_u8(1);
42                buf.put_u32(threshold);
43            }
44        }
45
46        Ok(self.marshal_size())
47    }
48}
49
50impl Unmarshal for DataChannelThreshold {
51    fn unmarshal<B>(buf: &mut B) -> Result<Self>
52    where
53        Self: Sized,
54        B: Buf,
55    {
56        let t = buf.get_u8();
57        let v = buf.get_u32();
58        if t == 0 {
59            Ok(DataChannelThreshold::Low(v))
60        } else {
61            Ok(DataChannelThreshold::High(v))
62        }
63    }
64}