rekt_common/datagrams/
heartbeat_requests.rs

1use crate::enums::datagram_type::DatagramType;
2
3//===== Sent to maintain the connexion
4#[repr(C)]
5pub struct DtgHeartbeat {
6    pub datagram_type: DatagramType,
7}
8
9impl DtgHeartbeat {
10    pub const fn new() -> DtgHeartbeat {
11        DtgHeartbeat {
12            datagram_type: DatagramType::Heartbeat
13        }
14    }
15
16    pub fn as_bytes(&self) -> Vec<u8>
17    {
18        return [u8::from(self.datagram_type)].into();
19    }
20}
21
22impl<'a> TryFrom<&'a [u8]> for DtgHeartbeat {
23    type Error = &'a str;
24
25    fn try_from(buffer: &'a [u8]) -> Result<Self, Self::Error> {
26        if buffer.len() < 1 {
27            return Err("Payload len is to short for a DtgHeartbeat.");
28        }
29
30        Ok(DtgHeartbeat {
31            datagram_type: DatagramType::from(buffer[0]),
32        })
33    }
34}
35
36//===== Sent to request a Heartbeat if a pear do not receive his
37// normal heartbeat.
38#[repr(C)]
39pub struct DtgHeartbeatRequest {
40    pub datagram_type: DatagramType,
41}
42
43impl DtgHeartbeatRequest {
44    pub const fn new() -> DtgHeartbeatRequest {
45        DtgHeartbeatRequest {
46            datagram_type: DatagramType::HeartbeatRequest
47        }
48    }
49
50    pub fn as_bytes(&self) -> Vec<u8>
51    {
52        return [u8::from(self.datagram_type)].into();
53    }
54}
55
56impl<'a> TryFrom<&'a [u8]> for DtgHeartbeatRequest {
57    type Error = &'a str;
58
59    fn try_from(buffer: &'a [u8]) -> Result<Self, Self::Error> {
60        if buffer.len() < 1 {
61            return Err("Payload len is to short for a DtgHeartbeatRequest.");
62        }
63
64        Ok(DtgHeartbeatRequest {
65            datagram_type: DatagramType::from(buffer[0]),
66        })
67    }
68}