webrtc_ice/priority/
mod.rs

1#[cfg(test)]
2mod priority_test;
3
4use stun::attributes::ATTR_PRIORITY;
5use stun::checks::*;
6use stun::message::*;
7
8/// Represents PRIORITY attribute.
9#[derive(Default, PartialEq, Eq, Debug, Copy, Clone)]
10pub struct PriorityAttr(pub u32);
11
12const PRIORITY_SIZE: usize = 4; // 32 bit
13
14impl Setter for PriorityAttr {
15    // add_to adds PRIORITY attribute to message.
16    fn add_to(&self, m: &mut Message) -> Result<(), stun::Error> {
17        let mut v = vec![0_u8; PRIORITY_SIZE];
18        v.copy_from_slice(&self.0.to_be_bytes());
19        m.add(ATTR_PRIORITY, &v);
20        Ok(())
21    }
22}
23
24impl PriorityAttr {
25    /// Decodes PRIORITY attribute from message.
26    pub fn get_from(&mut self, m: &Message) -> Result<(), stun::Error> {
27        let v = m.get(ATTR_PRIORITY)?;
28
29        check_size(ATTR_PRIORITY, v.len(), PRIORITY_SIZE)?;
30
31        let p = u32::from_be_bytes([v[0], v[1], v[2], v[3]]);
32        self.0 = p;
33
34        Ok(())
35    }
36}