tunneler_core/message/
kind.rs

1/// The Type/Kind of Message
2#[derive(Debug, PartialEq, Clone)]
3pub enum MessageType {
4    /// Indicates a new Connection should be established
5    /// for the provided id
6    Connect,
7    /// Indicates the Connection with the given id should
8    /// be closed
9    Close,
10    /// Indicates that this message contains Data that should
11    /// be handled accordingly
12    Data,
13    /// A simple Heartbeat connection to signal that the
14    /// other side is still there and ready to accept messages
15    /// as well as making sure that the connection is not closed
16    /// by the OS or other factors
17    Heartbeat,
18    /// This is the 1. message a Client sends when establishing
19    /// a connection with the Server
20    Establish,
21    /// This is the 2. message of the Handshake where the the Server
22    /// sends it public key over to the Client
23    Key,
24    /// This is the 3. message of the Handshake where the Client
25    /// sends the key/password to the Server after it was encrypted
26    /// with the previously received Public-Key
27    Verify,
28    /// This is the 4. and last message of the Handshake which is simply
29    /// send by the Server to acknowledge the new connection established
30    Acknowledge,
31    /// Signals the EOF from a request that is not yet closed
32    EOF,
33    /// This is the 5. message in the Handshake and is send by
34    /// the client along with other Data to inform the Server about the
35    /// desired Config to use
36    Config,
37}
38
39impl MessageType {
40    /// Deserializes the Kind/Type from a single Byte/u8
41    ///
42    /// Returns:
43    /// * None if the Byte was not a valid type/kind
44    /// * Some with the type/kind of the byte
45    pub fn deserialize(data: u8) -> Option<MessageType> {
46        match data {
47            1 => Some(MessageType::Connect),
48            2 => Some(MessageType::Close),
49            3 => Some(MessageType::Data),
50            4 => Some(MessageType::Heartbeat),
51            5 => Some(MessageType::Establish),
52            6 => Some(MessageType::Key),
53            7 => Some(MessageType::Verify),
54            8 => Some(MessageType::Acknowledge),
55            9 => Some(MessageType::EOF),
56            10 => Some(MessageType::Config),
57            _ => None,
58        }
59    }
60
61    /// Serializes the Type/Kind into a single Byte that can
62    /// then be send to a Client or Server
63    pub fn serialize(&self) -> u8 {
64        match *self {
65            MessageType::Connect => 1,
66            MessageType::Close => 2,
67            MessageType::Data => 3,
68            MessageType::Heartbeat => 4,
69            MessageType::Establish => 5,
70            MessageType::Key => 6,
71            MessageType::Verify => 7,
72            MessageType::Acknowledge => 8,
73            MessageType::EOF => 9,
74            MessageType::Config => 10,
75        }
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn message_type_deserialize_connect() {
85        assert_eq!(Some(MessageType::Connect), MessageType::deserialize(1));
86    }
87    #[test]
88    fn message_type_deserialize_close() {
89        assert_eq!(Some(MessageType::Close), MessageType::deserialize(2));
90    }
91    #[test]
92    fn message_type_deserialize_data() {
93        assert_eq!(Some(MessageType::Data), MessageType::deserialize(3));
94    }
95    #[test]
96    fn message_type_deserialize_heartbeat() {
97        assert_eq!(Some(MessageType::Heartbeat), MessageType::deserialize(4));
98    }
99    #[test]
100    fn message_type_deserialize_establish() {
101        assert_eq!(Some(MessageType::Establish), MessageType::deserialize(5));
102    }
103    #[test]
104    fn message_type_deserialize_key() {
105        assert_eq!(Some(MessageType::Key), MessageType::deserialize(6));
106    }
107    #[test]
108    fn message_type_deserialize_verify() {
109        assert_eq!(Some(MessageType::Verify), MessageType::deserialize(7));
110    }
111    #[test]
112    fn message_type_deserialize_acknowledge() {
113        assert_eq!(Some(MessageType::Acknowledge), MessageType::deserialize(8));
114    }
115    #[test]
116    fn message_type_deserialize_eof() {
117        assert_eq!(Some(MessageType::EOF), MessageType::deserialize(9));
118    }
119    #[test]
120    fn message_type_deserialize_invalid() {
121        assert_eq!(None, MessageType::deserialize(123));
122    }
123
124    #[test]
125    fn message_type_serialize_connect() {
126        assert_eq!(1, MessageType::Connect.serialize());
127    }
128    #[test]
129    fn message_type_serialize_close() {
130        assert_eq!(2, MessageType::Close.serialize());
131    }
132    #[test]
133    fn message_type_serialize_data() {
134        assert_eq!(3, MessageType::Data.serialize());
135    }
136    #[test]
137    fn message_type_serialize_heartbeat() {
138        assert_eq!(4, MessageType::Heartbeat.serialize());
139    }
140    #[test]
141    fn message_type_serialize_establish() {
142        assert_eq!(5, MessageType::Establish.serialize());
143    }
144    #[test]
145    fn message_type_serialize_key() {
146        assert_eq!(6, MessageType::Key.serialize());
147    }
148    #[test]
149    fn message_type_serialize_verify() {
150        assert_eq!(7, MessageType::Verify.serialize());
151    }
152    #[test]
153    fn message_type_serialize_acknowledge() {
154        assert_eq!(8, MessageType::Acknowledge.serialize());
155    }
156    #[test]
157    fn message_type_serialize_eof() {
158        assert_eq!(9, MessageType::EOF.serialize());
159    }
160}