pub struct Packet { /* private fields */ }
Expand description
Implementations
sourceimpl Packet
impl Packet
sourcepub fn new(kind: PacketKind, content: Bytes) -> Result<Self, Error>
pub fn new(kind: PacketKind, content: Bytes) -> Result<Self, Error>
Creates a new Packet.
Returns an Error if created packet would be bigger than
MAX_PACKET_SIZE
.
To avoid this check, use Packet::new_unchecked.
Size of packet is derived from content
.
Examples
Content packet is created like that.
let packet = Packet::new(PacketKind::End, Bytes::from([1_u8, 2_u8]))?;
sourcepub fn new_unchecked(kind: PacketKind, content: Bytes) -> Self
pub fn new_unchecked(kind: PacketKind, content: Bytes) -> Self
Creates a new Packet without checking if resulting packet size is not bigger than
MAX_PACKET_SIZE
.
Size of packet is derived from content
.
sourcepub fn send(self, stream: &mut TcpStream) -> Result<(), Error>
pub fn send(self, stream: &mut TcpStream) -> Result<(), Error>
Sends self
via stream
.
This takes ownership of self
.
sourcepub fn send_to<A>(self, socket: UdpSocket, address: A) -> Result<(), Error> where
A: ToSocketAddrs,
pub fn send_to<A>(self, socket: UdpSocket, address: A) -> Result<(), Error> where
A: ToSocketAddrs,
Sends self
to given address
on UdpSocket.
This takes ownership of self
.
sourcepub fn send_to_connected(self, socket: UdpSocket) -> Result<(), Error>
pub fn send_to_connected(self, socket: UdpSocket) -> Result<(), Error>
Sends self
to connected UdpSocket.
This takes ownership of self
.
sourcepub fn receive_from(socket: UdpSocket) -> Result<(Packet, SocketAddr), Error>
pub fn receive_from(socket: UdpSocket) -> Result<(Packet, SocketAddr), Error>
Receives a Packet and a SocketAddr on a UdpSocket.
sourcepub fn peek_from(socket: UdpSocket) -> Result<(Self, SocketAddr), Error>
pub fn peek_from(socket: UdpSocket) -> Result<(Self, SocketAddr), Error>
Peeks on data on socket
, if valid data are received a Packet is created and
returned together with a SocketAddr.
Since UdpSocket::peek_from
is used to retrieve data,
calling this again returns the same data.
sourcepub fn peek_from_connected(socket: UdpSocket) -> Result<Self, Error>
pub fn peek_from_connected(socket: UdpSocket) -> Result<Self, Error>
Peeks on data on a connected socket
, if valid data are received a Packet is created.
Since UdpSocket::peek
is used to retrieve data, calling this again returns the same data.
sourcepub fn number_of_packets(length: usize) -> u32
pub fn number_of_packets(length: usize) -> u32
Returns number of packets needed to send data with given byte length
.
sourcepub fn split_to_max_packet_size(buffer: Bytes) -> Vec<Bytes>
pub fn split_to_max_packet_size(buffer: Bytes) -> Vec<Bytes>
Takes Bytes and returns Vec of Bytes each with maximum length of
MAX_CONTENT_SIZE
.
sourcepub fn kind(&self) -> PacketKind
pub fn kind(&self) -> PacketKind
Returns kind
.
sourcepub fn content_ref(&self) -> &Bytes
pub fn content_ref(&self) -> &Bytes
Returns a reference to content
.
sourcepub fn content_mut(&mut self) -> &mut Bytes
pub fn content_mut(&mut self) -> &mut Bytes
Returns a mutable reference to content
.
sourcepub fn content_move(self) -> Bytes
pub fn content_move(self) -> Bytes
Takes ownership of self
and returns content
.
sourcepub fn max_size() -> u16
pub fn max_size() -> u16
Returns maximum number of bytes that can one Packet hold as u16. This is global value for every Packet that can be changed with Packet::set_max_size.
Minimum value is 5 bytes, 2 for packet size
(u16), 2 for packet kind
,
and at least 1 for content
.
This accesses a mutable
static with AtomicU16 to provide global access to this data
and an ability to change its value for specific needs, but since AtomicU16 is used
it is capped by u16::MAX.
sourcepub fn set_max_size(size: u16)
pub fn set_max_size(size: u16)
Sets MAX_PACKET_SIZE
to size
.
Minimum value is 5 bytes, 2 for packet size
(u16), 2 for packet kind
,
and at least 1 for content
.
For more information look at Packet::max_size.
sourcepub const fn description_size() -> u16
pub const fn description_size() -> u16
Returns minimal number of bytes that is every packet guaranteed to have, 2 bytes are for its size and 2 for its kind as u16.
sourcepub fn max_content_size() -> u16
pub fn max_content_size() -> u16
Maximum amount of bytes that a Packet can use for its content, its lower than
MAX_PACKET_SIZE
by
PACKET_DESCRIPTION_SIZE
.
Trait Implementations
sourceimpl<'de> Deserialize<'de> for Packet
impl<'de> Deserialize<'de> for Packet
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for Packet
Auto Trait Implementations
impl RefUnwindSafe for Packet
impl Send for Packet
impl Sync for Packet
impl Unpin for Packet
impl UnwindSafe for Packet
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more