rekt_lib/datagrams/
connect_requests.rs

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
use std::mem::size_of;

use crate::enums::datagram_type::DatagramType;
use crate::libs::types::{ClientId, Size};
use crate::libs::utils::{get_bytes_from_slice, get_u16_at_pos, get_u64_at_pos};

// Sent to the broker to start a connection
#[repr(C)]
pub struct DtgConnect {
    pub datagram_type: DatagramType,
}

impl DtgConnect {
    pub const fn new() -> DtgConnect {
        DtgConnect {
            datagram_type: DatagramType::Connect
        }
    }

    pub fn as_bytes(&self) -> Vec<u8>
    {
        return [u8::from(self.datagram_type)].into();
    }

    pub const fn get_default_byte_size() -> usize { return 1 }
}

impl<'a> TryFrom<&'a [u8]> for DtgConnect {
    type Error = &'a str;

    fn try_from(buffer: &'a [u8]) -> Result<Self, Self::Error> {
        if buffer.len() < DtgConnect::get_default_byte_size()
        {
            return Err("Payload len is to short for a DtgConnect.");
        }

        Ok(DtgConnect {
            datagram_type: DatagramType::from(buffer[0]),
        })
    }
}


//===== Sent to acknowledge the connexion with success

#[repr(C)]
pub struct DtgConnectAck {
    pub datagram_type: DatagramType,
    pub peer_id: ClientId,
    pub heartbeat_period: u16,
}

impl DtgConnectAck {
    pub const fn new(peer_id: ClientId, heartbeat_period: u16) -> DtgConnectAck {
        DtgConnectAck {
            datagram_type: DatagramType::ConnectAck,
            peer_id,
            heartbeat_period,
        }
    }

    pub fn as_bytes(&self) -> Vec<u8>
    {
        let mut bytes: Vec<u8> = Vec::with_capacity(DtgConnectAck::get_default_byte_size());
        bytes.push(u8::from(self.datagram_type));
        bytes.extend(self.peer_id.to_le_bytes().into_iter());
        bytes.extend(self.heartbeat_period.to_le_bytes().into_iter());

        return bytes;
    }

    pub fn get_default_byte_size() -> usize { return 11 }
}

impl<'a> TryFrom<&'a [u8]> for DtgConnectAck {
    type Error = &'a str;

    fn try_from(buffer: &'a [u8]) -> Result<Self, Self::Error> {
        if buffer.len() < DtgConnectAck::get_default_byte_size()
        {
            return Err("Payload len is to short for a DtgConnectAck.");
        }

        let peer_id = get_u64_at_pos(buffer, 1)?;
        let heartbeat_period = get_u16_at_pos(buffer, 9)?;

        Ok(DtgConnectAck {
            datagram_type: DatagramType::from(buffer[0]),
            peer_id,
            heartbeat_period,
        })
    }
}


#[repr(C)]
pub struct DtgConnectNack {
    pub datagram_type: DatagramType,
    pub size: Size,
    pub payload: Vec<u8>,
}

impl DtgConnectNack {
    pub fn new(message: &str) -> DtgConnectNack {
        let reason: Vec<u8> = message.as_bytes().into();
        let message_size = reason.len() as Size;

        DtgConnectNack {
            datagram_type: DatagramType::ConnectNack,
            size: message_size,
            payload: reason,
        }
    }

    pub fn as_bytes(&self) -> Vec<u8>
    {
        let mut bytes: Vec<u8> = Vec::with_capacity(DtgConnectNack::get_default_byte_size() + self.size as usize);
        bytes.push(u8::from(self.datagram_type));
        bytes.extend(self.size.to_le_bytes().into_iter());
        bytes.extend(&mut self.payload.iter());

        return bytes;
    }

    pub const fn get_default_byte_size() -> usize { return 3 }
}

impl<'a> TryFrom<&'a [u8]> for DtgConnectNack {
    type Error = &'a str;

    fn try_from(buffer: &'a [u8]) -> Result<Self, Self::Error> {
        if buffer.len() < DtgConnectNack::get_default_byte_size()
        {
            return Err("Payload len is to short for a RQ_Connect_Ack_Error.");
        }
        let size = get_u16_at_pos(buffer, 1)?;

        Ok(DtgConnectNack {
            datagram_type: DatagramType::from(buffer[0]),
            size,
            payload: get_bytes_from_slice(buffer, DtgConnectNack::get_default_byte_size(), (DtgConnectNack::get_default_byte_size() + size as usize)),
        })
    }
}