Skip to main content

moq_transport/setup/
server.rs

1use super::Version;
2use crate::coding::{Decode, DecodeError, Encode, EncodeError, KeyValuePairs};
3
4/// Sent by the server in response to a client setup.
5/// This SERVER_SETUP message is used by moq-transport draft versions 11 and later.
6/// Id = 0x21 vs 0x41 for versions <= 10.
7#[derive(Debug)]
8pub struct Server {
9    /// The list of supported versions in preferred order.
10    pub version: Version,
11
12    /// Setup Parameters, ie: MAX_REQUEST_ID, MAX_AUTH_TOKEN_CACHE_SIZE,
13    /// AUTHORIZATION_TOKEN, etc.
14    pub params: KeyValuePairs,
15}
16
17impl Decode for Server {
18    /// Decode the server setup.
19    fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
20        let typ = u64::decode(r)?;
21        if typ != 0x21 {
22            // SERVER_SETUP message ID for draft versions 11 and later
23            return Err(DecodeError::InvalidMessage(typ));
24        }
25
26        let _len = u16::decode(r)?;
27        // TODO: Check the length of the message.
28
29        let version = Version::decode(r)?;
30        let params = KeyValuePairs::decode(r)?;
31
32        Ok(Self { version, params })
33    }
34}
35
36impl Encode for Server {
37    fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
38        (0x21_u64).encode(w)?; // SERVER_SETUP message ID for draft versions 11 and later
39
40        // Find out the length of the message
41        // by encoding it into a buffer and then encoding the length.
42        // This is a bit wasteful, but it's the only way to know the length.
43        // TODO SLG - perhaps we can store the position of the Length field in the BufMut and
44        //       write the length later, to avoid the copy of the message bytes?
45        let mut buf = Vec::new();
46
47        self.version.encode(&mut buf).unwrap();
48        self.params.encode(&mut buf).unwrap();
49
50        // Make sure buf.len() <= u16::MAX
51        if buf.len() > u16::MAX as usize {
52            return Err(EncodeError::MsgBoundsExceeded);
53        }
54        (buf.len() as u16).encode(w)?;
55
56        // At least don't encode the message twice.
57        // Instead, write the buffer directly to the writer.
58        Self::encode_remaining(w, buf.len())?;
59        w.put_slice(&buf);
60
61        Ok(())
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::setup::ParameterType;
69    use bytes::BytesMut;
70
71    #[test]
72    fn encode_decode() {
73        let mut buf = BytesMut::new();
74
75        let mut params = KeyValuePairs::default();
76        params.set_intvalue(ParameterType::MaxRequestId.into(), 1000);
77
78        let server = Server {
79            version: Version::DRAFT_14,
80            params,
81        };
82
83        server.encode(&mut buf).unwrap();
84
85        #[rustfmt::skip]
86        assert_eq!(
87            buf.to_vec(),
88            vec![
89                0x21, // Type
90                0x00, 0x0c, // Length
91                0xC0, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x0E, // Version DRAFT_14 (0xff00000E)
92                0x01, // 1 Param
93                0x02, 0x43, 0xe8, // Key=2 (MaxRequestId), Value=1000
94            ]
95        );
96
97        let decoded = Server::decode(&mut buf).unwrap();
98        assert_eq!(decoded.version, server.version);
99        assert_eq!(decoded.params, server.params);
100    }
101}