why2_chat/network/voice/
mod.rs1pub mod options;
21
22#[cfg(feature = "client")]
23pub mod client;
24
25#[cfg(feature = "server")]
26pub mod server;
27
28use std::
29{
30 io::Error,
31 net::{ UdpSocket, SocketAddr },
32};
33
34use wincode::{ SchemaRead, SchemaWrite };
35
36use crate::
37{
38 crypto,
39 options::SharedKeys,
40};
41
42#[cfg(not(feature = "server"))]
43use crate::options as chat_options;
44
45#[derive(Clone, PartialEq, SchemaRead, SchemaWrite)]
46pub enum VoiceCode
47{
48 PING, PONG, }
51
52#[derive(SchemaRead, SchemaWrite)]
53pub struct VoicePacket {
55 pub voice: Option<Vec<u8>>, pub username: Option<String>, pub code: Option<VoiceCode>, pub id: Option<usize>, pub target_id: Option<usize>, pub seq: usize, pub timestamp: Option<u128>, }
63
64impl Default for VoicePacket
65{
66 fn default() -> Self
67 {
68 VoicePacket
69 {
70 voice: None,
71 id: None,
72 target_id: None,
73 code: None,
74 username: None,
75 seq: 0,
76 timestamp: None,
77 }
78 }
79}
80
81pub fn send (
83 socket: &UdpSocket,
84 mut packet: VoicePacket,
85 #[cfg(feature = "server")] addr: &SocketAddr,
86 #[cfg(feature = "server")] recipient_id: &usize,
87 keys: &SharedKeys
88) -> Result<usize, Error>
89{
90 #[cfg(feature = "server")]
92 {
93 if let Some(mut conn) = server::CONNECTIONS.get_mut(recipient_id) &&
94 let Some(conn) = conn.0.as_mut()
95 {
96 packet.seq = conn.server_seq() + 1;
97 *conn.server_seq_mut() = packet.seq;
98 }
99 }
100
101 #[cfg(feature = "client")]
103 {
104 packet.seq = options::get_seq() + 1;
105 options::set_seq(packet.seq);
106 }
107
108 let packet_bytes = wincode::serialize(&packet).expect("Encoding packet failed");
110
111 #[cfg(feature = "server")]
113 let encrypted_bytes: Vec<u8>;
114
115 #[cfg(not(feature = "server"))]
116 let mut encrypted_bytes: Vec<u8>;
117
118 encrypted_bytes = crypto::encrypt_packet::< { options::GRID_WIDTH }, { options::GRID_HEIGHT } >(packet_bytes, keys);
119
120 #[cfg(feature = "client")]
122 {
123 let id_be_bytes = packet.id.unwrap().to_be_bytes();
124 encrypted_bytes.splice(0..0, id_be_bytes);
125 }
126
127 #[cfg(feature = "server")]
128 {
129 socket.send_to(&encrypted_bytes, addr)
130 }
131
132 #[cfg(not(feature = "server"))]
133 {
134 socket.send(&encrypted_bytes)
135 }
136}
137
138
139pub fn receive(socket: &UdpSocket) -> Option<(VoicePacket, SocketAddr)> {
141 let mut buffer = [0u8; 2048];
142 loop {
144 #[cfg(feature = "client")]
146 if !options::get_use_voice() { break None; }
147
148 let (len, addr) = match socket.recv_from(&mut buffer)
149 {
150 Ok(result) => result,
151 Err(_) => continue
152 };
153
154 let buffer_offset: usize;
155
156 let keys =
158 {
159 #[cfg(feature = "server")]
160 {
161 if len <= 8 { continue; } let id = match buffer[..8].try_into()
164 {
165 Ok(id_be_bytes) => usize::from_be_bytes(id_be_bytes),
166 Err(_) => continue
167 };
168
169 buffer_offset = 8;
171
172 match server::find_key(&id)
173 {
174 Some(k) => k,
175 None => continue
176 }
177 }
178
179 #[cfg(not(feature = "server"))]
180 {
181 buffer_offset = 0;
182 chat_options::get_keys().unwrap()
183 }
184 };
185
186 let decrypted_bytes = match crypto::decrypt_packet::<{ options::GRID_WIDTH }, { options::GRID_HEIGHT }>
188 (buffer[buffer_offset..len].to_vec(), &keys)
189 {
190 Some(d) => d,
191 None => continue
192 };
193
194 return match wincode::deserialize::<VoicePacket>(&decrypted_bytes)
196 {
197 Ok(packet) => Some((packet, addr)),
198 Err(_) => continue
199 }
200 }
201}