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>
impl<'de, T: Serialize + Deserialize<'de>> Packet<'de, T>
Sourcepub fn unwrap(self) -> T
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}Sourcepub fn deserialize(serialized: &'de str) -> Result<Packet<'de, T>, Error>
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}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>
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>where
T: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more