rudp2plib/network/
request.rs

1use std::{cmp::Ordering, fmt::Debug, net::SocketAddr};
2
3use serialize_bits::{des::DeserializerData, ser::SerializerData};
4
5#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
6pub enum Type {
7    Connection,
8    Disconnection,
9    Message,
10    ShareConnection,
11}
12
13impl Type {
14    pub fn from_code(code: u8) -> Self {
15        match code {
16            0 => Self::Connection,
17            1 => Self::Disconnection,
18            2 => Self::Message,
19            8 => Self::ShareConnection,
20            _ => panic!("Code is not valid !"),
21        }
22    }
23
24    pub fn to_code(&self) -> u8 {
25        match self {
26            Type::Connection => 0,
27            Type::Disconnection => 1,
28            Type::Message => 2,
29            Type::ShareConnection => 8,
30        }
31    }
32}
33
34impl SerializerData for Type {
35    fn to_data(&self) -> Vec<u8> {
36        self.to_code().to_data()
37    }
38}
39
40impl DeserializerData for Type {
41    fn from_data(data: &Vec<u8>, index: usize) -> (Self, usize)
42    where
43        Self: Sized,
44    {
45        let (value, index) = u8::from_data(data, index);
46        (Type::from_code(value), index)
47    }
48}
49
50#[derive(Clone, Eq, PartialEq, Ord)]
51pub struct RequestPart {
52    pub uid: String,
53    pub request_type: Type,
54    pub start: usize,
55    pub total: usize,
56    pub content_size: usize,
57    pub content: Vec<u8>,
58    pub sender: SocketAddr,
59}
60
61impl SerializerData for RequestPart {
62    fn to_data(&self) -> Vec<u8> {
63        let mut res = Vec::new();
64        res.append(&mut self.uid.to_data());
65        res.append(&mut self.request_type.to_data());
66        res.append(&mut self.start.to_data());
67        res.append(&mut self.total.to_data());
68        res.append(&mut self.content_size.to_data());
69        res.append(&mut self.content.to_data());
70        res.append(&mut self.sender.to_data());
71        res
72    }
73}
74
75impl DeserializerData for RequestPart {
76    fn from_data(data: &Vec<u8>, index: usize) -> (Self, usize)
77    where
78        Self: Sized,
79    {
80        let (uid, index) = String::from_data(data, index);
81        let (request_type, index) = Type::from_data(data, index);
82        let (start, index) = usize::from_data(data, index);
83        let (total, index) = usize::from_data(data, index);
84        let (content_size, index) = usize::from_data(data, index);
85        let (content, index) = Vec::from_data(data, index);
86        let (sender, index) = SocketAddr::from_data(data, index);
87        (
88            Self {
89                uid,
90                request_type,
91                start,
92                total,
93                content_size,
94                content,
95                sender,
96            },
97            index,
98        )
99    }
100}
101
102impl Debug for RequestPart {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        f.debug_struct("RequestPart")
105            .field("uid", &self.uid)
106            .field("request_type", &self.request_type)
107            .field("start", &self.start)
108            .field("total", &self.total)
109            .field("content_size", &self.content_size)
110            .field("sender", &self.sender)
111            .finish()
112    }
113}
114
115impl PartialOrd for RequestPart {
116    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
117        if self.start < other.start {
118            Some(Ordering::Less)
119        } else if self.start > other.start {
120            Some(Ordering::Greater)
121        } else {
122            Some(Ordering::Equal)
123        }
124    }
125}