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