Skip to main content

Packet

Struct Packet 

Source
pub struct Packet<'de, T>
where T: Serialize + Deserialize<'de>,
{ /* private fields */ }
Expand description

Generic packet format.

Useful for game servers and the like.

Packet supports writing to anything that implements Write, and reading from anything that implements Read. including std::net::TcpStreams.

Currently it will not work with std::net::UdpSocket since it does not implement Write or Read.

Packet supports a payload of any value that implements serde::Serialize and serde::Deserialize.

The chomp_packet function allows you to read a packet from a Reader and will return a serialized string of the packet, use Packet::deserialize to deserialize it.

Packet::unwrap will give you the original payload stored in the packet.

§Examples

use serde::{Serialize, Deserialize};
use std::io::Cursor;
use xuko_net::packet::{Packet, chomp_packet};

#[derive(Serialize, Deserialize)]
struct Payload {
    x: i32,
    y: f32
}

let packet = Packet::new(Payload {
    x: 6,
    y: 3.14
});

let mut stream = Cursor::new(Vec::new());

packet.send(&mut stream); // Write to a stream
packet.unwrap(); // Unwrap the original value using `Packet::unwrap`

Implementations§

Source§

impl<'de, T: Serialize + Deserialize<'de>> Packet<'de, T>

Source

pub const fn new(payload: T) -> Self

Create a new Packet

Examples found in repository?
examples/packet_sync.rs (line 52)
49fn client() -> Result<(), PacketError> {
50    let mut stream = TcpStream::connect(ADDRESS)?;
51
52    Packet::new(ExamplePacket::SendInfo("Hello!".to_string())).send(&mut stream)?;
53
54    Ok(())
55}
Source

pub fn unwrap(self) -> T

Unwrap the Packet to its underlying type

Examples found in repository?
examples/packet_sync.rs (line 38)
29fn server() -> Result<(), PacketError> {
30    let listener = TcpListener::bind(ADDRESS)?;
31
32    while let Ok((mut stream, addr)) = listener.accept() {
33        println!("request from: {addr}");
34
35        let packet = chomp_packet(&mut stream)?;
36        println!("received packet: {packet}");
37
38        let parsed = Packet::<ExamplePacket>::deserialize(&packet)?.unwrap();
39
40        match parsed {
41            ExamplePacket::GetHardcodedValue => unreachable!(),
42            ExamplePacket::SendInfo(s) => println!("This is what the packet contained: {s}"),
43        }
44    }
45
46    Ok(())
47}
Source

pub fn serialize(&self) -> Result<String, Error>

Serialize Packet into a String

Source

pub fn deserialize(serialized: &'de str) -> Result<Packet<'de, T>, Error>

Deserialize a string into a Packet

Examples found in repository?
examples/packet_sync.rs (line 38)
29fn server() -> Result<(), PacketError> {
30    let listener = TcpListener::bind(ADDRESS)?;
31
32    while let Ok((mut stream, addr)) = listener.accept() {
33        println!("request from: {addr}");
34
35        let packet = chomp_packet(&mut stream)?;
36        println!("received packet: {packet}");
37
38        let parsed = Packet::<ExamplePacket>::deserialize(&packet)?.unwrap();
39
40        match parsed {
41            ExamplePacket::GetHardcodedValue => unreachable!(),
42            ExamplePacket::SendInfo(s) => println!("This is what the packet contained: {s}"),
43        }
44    }
45
46    Ok(())
47}
Source

pub fn send<W: Write>(&self, stream: &mut W) -> Result<(), PacketError>

Write the Packet in its serialized form to a writer

Examples found in repository?
examples/packet_sync.rs (line 52)
49fn client() -> Result<(), PacketError> {
50    let mut stream = TcpStream::connect(ADDRESS)?;
51
52    Packet::new(ExamplePacket::SendInfo("Hello!".to_string())).send(&mut stream)?;
53
54    Ok(())
55}

Auto Trait Implementations§

§

impl<'de, T> Freeze for Packet<'de, T>
where T: Freeze,

§

impl<'de, T> RefUnwindSafe for Packet<'de, T>
where T: RefUnwindSafe,

§

impl<'de, T> Send for Packet<'de, T>
where T: Send + Sync,

§

impl<'de, T> Sync for Packet<'de, T>
where T: Sync,

§

impl<'de, T> Unpin for Packet<'de, T>
where T: Unpin,

§

impl<'de, T> UnsafeUnpin for Packet<'de, T>
where T: UnsafeUnpin,

§

impl<'de, T> UnwindSafe for Packet<'de, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.