webrtc_data/message/
message_type.rs

1use super::*;
2use crate::error::Error;
3
4// The first byte in a `Message` that specifies its type:
5pub(crate) const MESSAGE_TYPE_ACK: u8 = 0x02;
6pub(crate) const MESSAGE_TYPE_OPEN: u8 = 0x03;
7pub(crate) const MESSAGE_TYPE_LEN: usize = 1;
8
9type Result<T> = std::result::Result<T, util::Error>;
10
11// A parsed DataChannel message
12#[derive(Eq, PartialEq, Copy, Clone, Debug)]
13pub enum MessageType {
14    DataChannelAck,
15    DataChannelOpen,
16}
17
18impl MarshalSize for MessageType {
19    fn marshal_size(&self) -> usize {
20        MESSAGE_TYPE_LEN
21    }
22}
23
24impl Marshal for MessageType {
25    fn marshal_to(&self, mut buf: &mut [u8]) -> Result<usize> {
26        let b = match self {
27            MessageType::DataChannelAck => MESSAGE_TYPE_ACK,
28            MessageType::DataChannelOpen => MESSAGE_TYPE_OPEN,
29        };
30
31        buf.put_u8(b);
32
33        Ok(1)
34    }
35}
36
37impl Unmarshal for MessageType {
38    fn unmarshal<B>(buf: &mut B) -> Result<Self>
39    where
40        B: Buf,
41    {
42        let required_len = MESSAGE_TYPE_LEN;
43        if buf.remaining() < required_len {
44            return Err(Error::UnexpectedEndOfBuffer {
45                expected: required_len,
46                actual: buf.remaining(),
47            }
48            .into());
49        }
50
51        let b = buf.get_u8();
52
53        match b {
54            MESSAGE_TYPE_ACK => Ok(Self::DataChannelAck),
55            MESSAGE_TYPE_OPEN => Ok(Self::DataChannelOpen),
56            _ => Err(Error::InvalidMessageType(b).into()),
57        }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use bytes::{Bytes, BytesMut};
64
65    use super::*;
66
67    #[test]
68    fn test_message_type_unmarshal_open_success() -> Result<()> {
69        let mut bytes = Bytes::from_static(&[0x03]);
70        let msg_type = MessageType::unmarshal(&mut bytes)?;
71
72        assert_eq!(msg_type, MessageType::DataChannelOpen);
73
74        Ok(())
75    }
76
77    #[test]
78    fn test_message_type_unmarshal_ack_success() -> Result<()> {
79        let mut bytes = Bytes::from_static(&[0x02]);
80        let msg_type = MessageType::unmarshal(&mut bytes)?;
81
82        assert_eq!(msg_type, MessageType::DataChannelAck);
83        Ok(())
84    }
85
86    #[test]
87    fn test_message_type_unmarshal_invalid() -> Result<()> {
88        let mut bytes = Bytes::from_static(&[0x01]);
89        match MessageType::unmarshal(&mut bytes) {
90            Ok(_) => panic!("expected Error, but got Ok"),
91            Err(err) => {
92                if let Some(&Error::InvalidMessageType(0x01)) = err.downcast_ref::<Error>() {
93                    return Ok(());
94                }
95                panic!(
96                    "unexpected err {:?}, want {:?}",
97                    err,
98                    Error::InvalidMessageType(0x01)
99                );
100            }
101        }
102    }
103
104    #[test]
105    fn test_message_type_marshal_size() -> Result<()> {
106        let ack = MessageType::DataChannelAck;
107        let marshal_size = ack.marshal_size();
108
109        assert_eq!(marshal_size, MESSAGE_TYPE_LEN);
110        Ok(())
111    }
112
113    #[test]
114    fn test_message_type_marshal() -> Result<()> {
115        let mut buf = BytesMut::with_capacity(MESSAGE_TYPE_LEN);
116        buf.resize(MESSAGE_TYPE_LEN, 0u8);
117        let msg_type = MessageType::DataChannelAck;
118        let n = msg_type.marshal_to(&mut buf)?;
119        let bytes = buf.freeze();
120
121        assert_eq!(n, MESSAGE_TYPE_LEN);
122        assert_eq!(&bytes[..], &[0x02]);
123        Ok(())
124    }
125}