aboutsummaryrefslogtreecommitdiff
path: root/src/message/handshake/connack.rs
blob: 222c08cd44c55f106aad248449d4f49213be1f08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use failure::Error;

/// Data received right after initializing the connection
///
/// ConnAck is serialized sequentially
#[derive(Debug)]
pub struct ConnAck {
    /// The Flag 0x01 for TLS
    /// and 0x02 for Deflate Compression
    pub flags: u8,
    /// Some extra protocol version specific data
    /// So far unused
    pub extra: i16,
    /// The version of the protocol
    /// 0x00000001 for the legacy protocol
    /// 0x00000002 for the datastream protocol
    ///
    /// Only the datastream protocol is supported by this crate
    pub version: i8,
}

impl Default for ConnAck {
    fn default() -> Self {
        Self {
            flags: 0x00,
            extra: 0x00,
            version: 0x00000002,
        }
    }
}

impl crate::Serialize for ConnAck {
    fn serialize(&self) -> Result<Vec<std::primitive::u8>, Error> {
        let mut bytes: Vec<u8> = Vec::new();

        bytes.append(&mut self.flags.serialize()?);
        bytes.append(&mut self.extra.serialize()?);
        bytes.append(&mut self.version.serialize()?);

        Ok(bytes)
    }
}

impl crate::Deserialize for ConnAck {
    fn parse(b: &[u8]) -> Result<(usize, Self), Error> {
        let (flen, flags) = u8::parse(b)?;
        let (elen, extra) = i16::parse(&b[flen..])?;
        let (vlen, version) = i8::parse(&b[(flen + elen)..])?;

        return Ok((
            flen + elen + vlen,
            Self {
                flags,
                extra,
                version,
            },
        ));
    }
}