1mod packet_kind;
2
3use serde::{Deserialize, Serialize};
4
5use itertools::Itertools;
6
7use std::io::{Read, Write};
8use std::net::{SocketAddr, TcpStream, ToSocketAddrs, UdpSocket};
9use std::sync::atomic::{AtomicU16, Ordering};
10
11use crate::bytes::Bytes;
12use crate::error::{Error, ErrorKind};
13use crate::ron::{FromRon, ToRon};
14
15pub use packet_kind::PacketKind;
16
17const PACKET_DESCRIPTION_SIZE: u16 = 4;
19
20static mut MAX_PACKET_SIZE: AtomicU16 = AtomicU16::new(1024);
22
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
34pub struct Packet {
35 size: u16,
36 kind: PacketKind,
37 content: Bytes,
38}
39
40impl ToRon for Packet {}
41impl FromRon<'_> for Packet {}
42
43impl Default for Packet {
44 fn default() -> Self {
45 Packet {
46 size: Packet::description_size(),
47 kind: PacketKind::Empty,
48 content: Bytes::empty(),
49 }
50 }
51}
52
53impl TryFrom<Bytes> for Packet {
54 type Error = Error;
55
56 fn try_from(value: Bytes) -> Result<Self, Self::Error> {
57 if value
60 .get((Packet::description_size() - 1) as usize)
61 .is_none()
62 {
63 return Err(Error::new(
64 ErrorKind::InvalidBufferSize,
65 Some(
66 "impl TryFrom<Bytes> for Packet requires buffer of length of at least 4 bytes."
67 .to_string(),
68 ),
69 ));
70 }
71
72 let size = value.len();
73 let kind = PacketKind::try_from(&value[2..4])?;
75 let content = value[4..size].to_vec();
76
77 Ok(Packet {
78 size: size as u16,
79 kind,
80 content: content.into(),
81 })
82 }
83}
84
85impl TryFrom<&Bytes> for Packet {
86 type Error = Error;
87
88 fn try_from(value: &Bytes) -> Result<Self, Self::Error> {
89 if value
92 .get((Packet::description_size() - 1) as usize)
93 .is_none()
94 {
95 return Err(Error::new(
96 ErrorKind::InvalidBufferSize,
97 Some(
98 "impl TryFrom<Bytes> for Packet requires buffer of length of at least 4 bytes."
99 .to_string(),
100 ),
101 ));
102 }
103
104 let size = value.len();
105 let kind = PacketKind::try_from(&value[2..4])?;
107 let content = value[4..size].to_vec();
108
109 Ok(Packet {
110 size: size as u16,
111 kind,
112 content: content.into(),
113 })
114 }
115}
116
117impl TryFrom<&[u8]> for Packet {
118 type Error = Error;
119
120 fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
121 if value
124 .get((Packet::description_size() - 1) as usize)
125 .is_none()
126 {
127 return Err(Error::new(
128 ErrorKind::InvalidBufferSize,
129 Some(
130 "impl TryFrom<Bytes> for Packet requires buffer of length of at least 4 bytes."
131 .to_string(),
132 ),
133 ));
134 }
135
136 let size = value.len();
137 let kind = PacketKind::try_from(&value[2..4])?;
139 let content = value[4..size].to_vec();
140
141 Ok(Packet {
142 size: size as u16,
143 kind,
144 content: content.into(),
145 })
146 }
147}
148
149impl From<Packet> for Bytes {
150 fn from(mut value: Packet) -> Self {
151 let mut bytes = Bytes::empty();
152
153 bytes.append(&mut value.size.into());
154 bytes.append(&mut value.kind.into());
155 bytes.append(&mut value.content);
156
157 bytes
158 }
159}
160
161impl Packet {
162 pub fn new(kind: PacketKind, content: Bytes) -> Result<Self, Error> {
183 let size = Packet::description_size() + content.len() as u16;
188
189 if size > Packet::max_size() {
190 return Err(Error::new(
191 ErrorKind::TooBigPacket,
192 Some("Created Packet would be bigger than MAX_PACKET_SIZE.".to_string()),
193 ));
194 }
195
196 Ok(Packet {
197 size: size as u16,
198 kind,
199 content,
200 })
201 }
202
203 pub fn new_unchecked(kind: PacketKind, content: Bytes) -> Self {
208 let size = Packet::description_size() + content.len() as u16;
213
214 Packet {
215 size: size as u16,
216 kind,
217 content,
218 }
219 }
220
221 pub fn send(self, stream: &mut TcpStream) -> Result<(), Error> {
225 let packet_bytes: Bytes = self.into();
226 let packet_buff = packet_bytes.as_slice();
227
228 if let Err(e) = stream.write(packet_buff) {
229 let packet = Packet::try_from(packet_buff)?;
230 return Err(Error::new(
231 ErrorKind::WritingToStreamFailed,
232 Some(format!(
233 "Failed to write packet to stream.\n{}\n{}",
234 packet.to_ron_pretty(None)?,
235 e,
236 )),
237 ));
238 }
239
240 Ok(())
241 }
242
243 pub fn send_to<A>(self, socket: UdpSocket, address: A) -> Result<(), Error>
247 where
248 A: ToSocketAddrs,
249 {
250 let packet_bytes: Bytes = self.into();
251 let packet_buff = packet_bytes.as_slice();
252
253 if let Err(e) = socket.send_to(packet_buff, address) {
254 let packet = Packet::try_from(packet_buff)?;
255 return Err(Error::new(
256 ErrorKind::SendingToAddressFailed,
257 Some(format!(
258 "Failed to send packet to address.\n{}\n{}",
259 packet.to_ron_pretty(None)?,
260 e,
261 )),
262 ));
263 }
264
265 Ok(())
266 }
267
268 pub fn send_to_connected(self, socket: UdpSocket) -> Result<(), Error> {
272 let packet_bytes: Bytes = self.into();
273 let packet_buff = packet_bytes.as_slice();
274
275 if let Err(e) = socket.send(packet_buff) {
276 let packet = Packet::try_from(packet_buff)?;
277 return Err(Error::new(
278 ErrorKind::SendingToAddressFailed,
279 Some(format!(
280 "Failed to send packet to connected socket.\n{}\n{}",
281 packet.to_ron_pretty(None)?,
282 e,
283 )),
284 ));
285 }
286
287 Ok(())
288 }
289
290 pub fn receive(stream: &mut TcpStream) -> Result<Packet, Error> {
292 let mut size_buff = vec![0_u8; 2];
294 if let Err(e) = stream.read_exact(&mut size_buff) {
295 return Err(Error::new(
296 ErrorKind::ReadingFromStreamFailed,
297 Some(format!("Failed to read the size of packet. \n({})", e)),
298 ));
299 }
300 let size = u16::try_from(&Bytes::from(size_buff.clone()))?;
301
302 let mut buff = vec![0_u8; (size - 2) as usize];
305 if let Err(e) = stream.read_exact(&mut buff) {
307 return Err(Error::new(
308 ErrorKind::ReadingFromStreamFailed,
309 Some(format!("Failed to read contents of packet. \n({})", e)),
310 ));
311 }
312
313 size_buff.extend(buff);
315 let buff = size_buff;
316
317 Packet::try_from(buff.as_slice())
319 }
320
321 pub fn receive_from(socket: UdpSocket) -> Result<(Packet, SocketAddr), Error> {
323 let mut buff = vec![0_u8; Packet::max_size().into()];
324
325 match socket.recv_from(&mut buff) {
326 Ok((number_of_byte_read, address)) => {
327 buff.truncate(number_of_byte_read);
329 Ok((Packet::try_from(buff.as_slice())?, address))
330 }
331 Err(e) => Err(Error::new(
332 ErrorKind::ReceivingFromAddressFailed,
333 Some(format!(
334 "Failed to receive a packet from an address. \n({e})"
335 )),
336 )),
337 }
338 }
339
340 pub fn receive_from_connected(socket: UdpSocket) -> Result<Packet, Error> {
342 let mut buff = vec![0_u8; Packet::max_size().into()];
343
344 match socket.recv(&mut buff) {
345 Ok(number_of_byte_read) => {
346 buff.truncate(number_of_byte_read);
348 Ok(Packet::try_from(buff.as_slice())?)
349 }
350 Err(e) => Err(Error::new(
351 ErrorKind::ReceivingFromAddressFailed,
352 Some(format!(
353 "Failed to receive a packet from the connected socket. \n({e})"
354 )),
355 )),
356 }
357 }
358
359 pub fn peek(stream: &mut TcpStream) -> Result<Packet, Error> {
363 let mut size_buff = vec![0_u8; 2];
365 if let Err(e) = stream.peek(&mut size_buff) {
366 return Err(Error::new(
367 ErrorKind::ReadingFromStreamFailed,
368 Some(format!("Failed to peek on the size of packet. \n({})", e)),
369 ));
370 }
371
372 let size = u16::try_from(&Bytes::from(size_buff))?;
373
374 let mut buff = vec![0_u8; (size) as usize];
377 if let Err(e) = stream.peek(&mut buff) {
378 return Err(Error::new(
379 ErrorKind::ReadingFromStreamFailed,
380 Some(format!(
381 "Failed to peek on the contents of packet. \n({})",
382 e
383 )),
384 ));
385 }
386
387 Packet::try_from(buff.as_slice())
389 }
390
391 pub fn peek_from(socket: UdpSocket) -> Result<(Self, SocketAddr), Error> {
397 let mut buff = vec![0_u8; Packet::max_size().into()];
398
399 match socket.peek_from(&mut buff) {
400 Ok((number_of_byte_read, address)) => {
401 buff.truncate(number_of_byte_read);
402 Ok((Packet::try_from(buff.as_slice())?, address))
403 }
404 Err(e) => Err(Error::new(
405 ErrorKind::ReceivingFromAddressFailed,
406 Some(format!(
407 "Failed to receive a packet from the address. \n({e})"
408 )),
409 )),
410 }
411 }
412
413 pub fn peek_from_connected(socket: UdpSocket) -> Result<Self, Error> {
417 let mut buff = vec![0_u8; Packet::max_size().into()];
418
419 match socket.peek(&mut buff) {
420 Ok(number_of_byte_read) => {
421 buff.truncate(number_of_byte_read);
422 Ok(Packet::try_from(buff.as_slice())?)
423 }
424 Err(e) => Err(Error::new(
425 ErrorKind::ReceivingFromAddressFailed,
426 Some(format!(
427 "Failed to receive a packet from the connected socket. \n({e})"
428 )),
429 )),
430 }
431 }
432
433 pub fn number_of_packets(length: usize) -> u32 {
437 let mut number_of_packets = length / (Packet::max_content_size() as usize);
439 if length % (Packet::max_content_size() as usize) != 0 {
441 number_of_packets += 1;
442 }
443 number_of_packets as u32
444 }
445
446 pub fn split_to_max_packet_size(buffer: Bytes) -> Vec<Bytes> {
451 let vectored_content: Vec<Bytes> = buffer
456 .into_iter()
457 .chunks(Packet::max_content_size() as usize)
458 .into_iter()
459 .map(|chunk| chunk.collect::<Vec<u8>>().into())
460 .collect::<Vec<Bytes>>();
461
462 vectored_content
463 }
464
465 pub fn size(&self) -> u16 {
467 self.size
468 }
469
470 pub fn kind(&self) -> PacketKind {
472 self.kind.clone()
473 }
474
475 pub fn content(&self) -> Bytes {
479 self.content.clone()
480 }
481
482 pub fn content_ref(&self) -> &Bytes {
484 &self.content
485 }
486
487 pub fn content_mut(&mut self) -> &mut Bytes {
489 &mut self.content
490 }
491
492 pub fn content_move(self) -> Bytes {
494 self.content
495 }
496
497 pub fn max_size() -> u16 {
507 unsafe { MAX_PACKET_SIZE.load(Ordering::SeqCst) }
508 }
509
510 pub fn set_max_size(size: u16) {
517 unsafe {
518 MAX_PACKET_SIZE.store(size, Ordering::SeqCst);
519 }
520 }
521
522 pub const fn description_size() -> u16 {
525 PACKET_DESCRIPTION_SIZE
526 }
527
528 pub fn max_content_size() -> u16 {
532 Packet::max_size() - Packet::description_size()
533 }
534}