g_rust/services/packetinfo/
packetinfomanager.rs

1use std::collections::HashMap;
2use crate::protocol::hdirection::HDirection;
3use crate::protocol::hpacket::HPacket;
4use crate::protocol::vars::packetvariable::PacketVariable;
5use crate::services::packetinfo::packetinfo::PacketInfo;
6
7#[derive(Debug, Clone, Default)]
8pub struct PacketInfoManager {
9    header_id_to_message_incoming: HashMap<i32, Vec<PacketInfo>>,
10    header_id_to_message_outgoing: HashMap<i32, Vec<PacketInfo>>,
11
12    hash_to_message_incoming: HashMap<String, Vec<PacketInfo>>,
13    hash_to_message_outgoing: HashMap<String, Vec<PacketInfo>>,
14
15    name_to_message_incoming: HashMap<String, Vec<PacketInfo>>,
16    name_to_message_outgoing: HashMap<String, Vec<PacketInfo>>,
17
18    packet_info_list: Vec<PacketInfo>
19}
20
21impl PacketVariable for PacketInfoManager {
22    fn from_packet(bytes: Vec<u8>) -> (Self, usize) where Self: Sized {
23        let mut packet = HPacket::from_header_id_and_bytes(0, bytes);
24        let size: i32 = packet.read();
25
26        let mut list: Vec<PacketInfo> = Vec::new();
27        for _ in 0..size {
28            list.push(packet.read());
29        }
30
31        (PacketInfoManager {
32            header_id_to_message_incoming: list.iter()
33                .filter(| i | i.destination == HDirection::ToClient)
34                .map(| i | (i.header_id, i.clone()))
35                .fold(HashMap::new(), | mut res, (k, v)| {
36                    res.entry(k).or_insert_with(|| Vec::new()).push(v);
37                    res
38                }),
39            header_id_to_message_outgoing: list.iter()
40                .filter(| i | i.destination == HDirection::ToServer)
41                .map(| i | (i.header_id, i.clone()))
42                .fold(HashMap::new(), | mut res, (k, v)| {
43                    res.entry(k).or_insert_with(|| Vec::new()).push(v);
44                    res
45                }),
46
47            hash_to_message_incoming: list.iter()
48                .filter(| i | i.destination == HDirection::ToClient)
49                .map(| i | (i.hash.clone(), i.clone()))
50                .fold(HashMap::new(), | mut res, (k, v)| {
51                    res.entry(k).or_insert_with(|| Vec::new()).push(v);
52                    res
53                }),
54            hash_to_message_outgoing: list.iter()
55                .filter(| i | i.destination == HDirection::ToServer)
56                .map(| i | (i.hash.clone(), i.clone()))
57                .fold(HashMap::new(), | mut res, (k, v)| {
58                    res.entry(k).or_insert_with(|| Vec::new()).push(v);
59                    res
60                }),
61
62            name_to_message_incoming: list.iter()
63                .filter(| i | i.destination == HDirection::ToClient)
64                .map(| i | (i.name.clone(), i.clone()))
65                .fold(HashMap::new(), | mut res, (k, v)| {
66                    res.entry(k).or_insert_with(|| Vec::new()).push(v);
67                    res
68                }),
69            name_to_message_outgoing: list.iter()
70                .filter(| i | i.destination == HDirection::ToServer)
71                .map(| i | (i.name.clone(), i.clone()))
72                .fold(HashMap::new(), | mut res, (k, v)| {
73                    res.entry(k).or_insert_with(|| Vec::new()).push(v);
74                    res
75                }),
76
77            packet_info_list: list
78        }, packet.read_index - 6)
79    }
80
81    fn to_packet(&self) -> Vec<u8> {
82        let mut packet = HPacket::from_header_id(0);
83        packet.append(self.packet_info_list.len() as i32);
84        for packet_info in self.packet_info_list.iter() {
85            packet.append(packet_info.clone());
86        }
87        packet.get_bytes()[6..].to_vec()
88    }
89}
90
91impl PacketInfoManager {
92    pub fn get_all_packet_info_from_header_id(&mut self, direction: HDirection, header_id: i32) -> Vec<PacketInfo> {
93        if direction == HDirection::ToClient {
94            self.header_id_to_message_incoming.entry(header_id).or_default().clone()
95        } else {
96            self.header_id_to_message_outgoing.entry(header_id).or_default().clone()
97        }
98    }
99
100    pub fn get_all_packet_info_from_hash(&mut self, direction: HDirection, hash: String) -> Vec<PacketInfo> {
101        if direction == HDirection::ToClient {
102            self.hash_to_message_incoming.entry(hash).or_default().clone()
103        } else {
104            self.hash_to_message_outgoing.entry(hash).or_default().clone()
105        }
106    }
107
108    pub fn get_all_packet_info_from_name(&mut self, direction: HDirection, name: String) -> Vec<PacketInfo> {
109        if direction == HDirection::ToClient {
110            self.name_to_message_incoming.entry(name).or_default().clone()
111        } else {
112            self.name_to_message_outgoing.entry(name).or_default().clone()
113        }
114    }
115
116    pub fn get_packet_info_from_header_id(&mut self, direction: HDirection, header_id: i32) -> Option<PacketInfo> {
117        let all = self.get_all_packet_info_from_header_id(direction, header_id);
118        if all.is_empty() {
119            None
120        } else {
121            Some(all[0].clone())
122        }
123    }
124
125    pub fn get_packet_info_from_hash(&mut self, direction: HDirection, hash: String) -> Option<PacketInfo> {
126        let all = self.get_all_packet_info_from_hash(direction, hash);
127        if all.is_empty() {
128            None
129        } else {
130            Some(all[0].clone())
131        }
132    }
133
134    pub fn get_packet_info_from_name(&mut self, direction: HDirection, name: String) -> Option<PacketInfo> {
135        let all = self.get_all_packet_info_from_name(direction, name);
136        if all.is_empty() {
137            None
138        } else {
139            Some(all[0].clone())
140        }
141    }
142
143    pub fn get_packet_info_list(&mut self) -> Vec<PacketInfo> {
144        self.packet_info_list.clone()
145    }
146}