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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use super::{HeaderReader, HeaderWriter};
use crate::error::Result;
use crate::net::constants::STANDARD_HEADER_SIZE;
use crate::packet::{DeliveryGuarantee, EnumConverter, OrderingGuarantee, PacketType};
use crate::protocol_version::ProtocolVersion;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::io::Cursor;

#[derive(Copy, Clone, Debug)]
/// This header will be included in each packet, and contains some basic information.
pub struct StandardHeader {
    protocol_version: u16,
    packet_type: PacketType,
    delivery_guarantee: DeliveryGuarantee,
    ordering_guarantee: OrderingGuarantee,
}

impl StandardHeader {
    /// Create new heartbeat header.
    pub fn new(
        delivery_guarantee: DeliveryGuarantee,
        ordering_guarantee: OrderingGuarantee,
        packet_type: PacketType,
    ) -> Self {
        StandardHeader {
            protocol_version: ProtocolVersion::get_crc16(),
            delivery_guarantee,
            ordering_guarantee,
            packet_type,
        }
    }

    /// Returns the protocol version
    #[cfg(test)]
    pub fn protocol_version(&self) -> u16 {
        self.protocol_version
    }

    /// Returns the DeliveryGuarantee
    pub fn delivery_guarantee(&self) -> DeliveryGuarantee {
        self.delivery_guarantee
    }

    /// Returns the OrderingGuarantee
    pub fn ordering_guarantee(&self) -> OrderingGuarantee {
        self.ordering_guarantee
    }

    /// Returns the PacketType
    #[cfg(test)]
    pub fn packet_type(&self) -> PacketType {
        self.packet_type
    }

    /// Returns true if the packet is a fragment, false if not
    pub fn is_fragment(&self) -> bool {
        self.packet_type == PacketType::Fragment
    }

    /// Checks if the protocol version in the packet is a valid version
    pub fn is_current_protocol(&self) -> bool {
        ProtocolVersion::valid_version(self.protocol_version)
    }
}

impl Default for StandardHeader {
    fn default() -> Self {
        StandardHeader::new(
            DeliveryGuarantee::Unreliable,
            OrderingGuarantee::None,
            PacketType::Packet,
        )
    }
}

impl HeaderWriter for StandardHeader {
    type Output = Result<()>;

    fn parse(&self, buffer: &mut Vec<u8>) -> Self::Output {
        buffer.write_u16::<BigEndian>(self.protocol_version)?;
        buffer.write_u8(self.packet_type.to_u8())?;
        buffer.write_u8(self.delivery_guarantee.to_u8())?;
        buffer.write_u8(self.ordering_guarantee.to_u8())?;
        Ok(())
    }
}

impl HeaderReader for StandardHeader {
    type Header = Result<StandardHeader>;

    fn read(rdr: &mut Cursor<&[u8]>) -> Self::Header {
        let protocol_version = rdr.read_u16::<BigEndian>()?; /* protocol id */
        let packet_id = rdr.read_u8()?;
        let delivery_guarantee_id = rdr.read_u8()?;
        let order_guarantee_id = rdr.read_u8()?;

        let header = StandardHeader {
            protocol_version,
            packet_type: PacketType::from_u8(packet_id),
            delivery_guarantee: DeliveryGuarantee::from_u8(delivery_guarantee_id),
            ordering_guarantee: OrderingGuarantee::from_u8(order_guarantee_id),
        };

        Ok(header)
    }

    /// Get the size of this header.
    fn size() -> u8 {
        STANDARD_HEADER_SIZE
    }
}

#[cfg(test)]
mod tests {
    use crate::net::constants::STANDARD_HEADER_SIZE;
    use crate::packet::header::{HeaderReader, HeaderWriter, StandardHeader};
    use crate::packet::{DeliveryGuarantee, EnumConverter, OrderingGuarantee, PacketType};
    use std::io::Cursor;

    #[test]
    fn serialize() {
        let mut buffer = Vec::new();
        let header = StandardHeader::new(
            DeliveryGuarantee::Unreliable,
            OrderingGuarantee::Sequenced(None),
            PacketType::Packet,
        );
        header.parse(&mut buffer).is_ok();

        // [0 .. 3] protocol version
        assert_eq!(buffer[2], PacketType::Packet.to_u8());
        assert_eq!(buffer[3], DeliveryGuarantee::Unreliable.to_u8());
        assert_eq!(buffer[4], OrderingGuarantee::Sequenced(None).to_u8());
    }

    #[test]
    fn deserialize() {
        let buffer = vec![0, 1, 0, 1, 1];

        let mut cursor = Cursor::new(buffer.as_slice());

        let header = StandardHeader::read(&mut cursor).unwrap();

        assert_eq!(header.protocol_version(), 1);
        assert_eq!(header.packet_type(), PacketType::Packet);
        assert_eq!(header.delivery_guarantee(), DeliveryGuarantee::Reliable);
        assert_eq!(
            header.ordering_guarantee(),
            OrderingGuarantee::Sequenced(None)
        );
    }

    #[test]
    fn size() {
        assert_eq!(StandardHeader::size(), STANDARD_HEADER_SIZE);
    }
}