Struct Packet

Source
pub struct Packet { /* private fields */ }
Expand description

Gives structure to data to be sent or received from stream.

Packet is the lowest abstraction above buffer in this library.

§Fields

  • size – size of the whole packet in number of bytes. It is u16 so that packet can not have size over u16::MAX
  • kindkind of packet.
  • content – data stored in the packet.

Implementations§

Source§

impl Packet

Source

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]))?;
Source

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.

Source

pub fn send(self, stream: &mut TcpStream) -> Result<(), Error>

Sends self via stream.

This takes ownership of self.

Source

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.

Source

pub fn send_to_connected(self, socket: UdpSocket) -> Result<(), Error>

Sends self to connected UdpSocket.

This takes ownership of self.

Source

pub fn receive(stream: &mut TcpStream) -> Result<Packet, Error>

Receives a Packet from stream.

Source

pub fn receive_from(socket: UdpSocket) -> Result<(Packet, SocketAddr), Error>

Receives a Packet and a SocketAddr on a UdpSocket.

Source

pub fn receive_from_connected(socket: UdpSocket) -> Result<Packet, Error>

Receives a Packet on a connected UdpSocket.

Source

pub fn peek(stream: &mut TcpStream) -> Result<Packet, Error>

Peeks on data on TcpStream if those data are valid, Packet is created.

Since TcpStream::peek is used to retrieve data, calling this again returns the same data.

Source

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.

Source

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.

Source

pub fn number_of_packets(length: usize) -> u32

Returns number of packets needed to send data with given byte length.

Source

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.

Source

pub fn size(&self) -> u16

Returns size.

Source

pub fn kind(&self) -> PacketKind

Returns kind.

Source

pub fn content(&self) -> Bytes

Returns content.

Content is cloned.

Source

pub fn content_ref(&self) -> &Bytes

Returns a reference to content.

Source

pub fn content_mut(&mut self) -> &mut Bytes

Returns a mutable reference to content.

Source

pub fn content_move(self) -> Bytes

Takes ownership of self and returns content.

Source

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.

Source

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.

Source

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.

Source

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§

Source§

impl Clone for Packet

Source§

fn clone(&self) -> Packet

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Packet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Packet

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Packet

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Packet> for Bytes

Source§

fn from(value: Packet) -> Self

Converts to this type from the input type.
Source§

impl FromRon<'_> for Packet

Source§

fn from_ron(ron: &'a str) -> Result<Self, Error>

Creates an implementor from RON. Read more
Source§

impl PartialEq for Packet

Source§

fn eq(&self, other: &Packet) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Packet

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl ToRon for Packet

Source§

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

Returns a RON from implementor. Read more
Source§

fn to_ron_pretty(&self, config: Option<PrettyConfig>) -> Result<String, Error>

Returns a pretty formatted RON from implementor. Read more
Source§

impl TryFrom<&[u8]> for Packet

Source§

type Error = Error

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

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&Bytes> for Packet

Source§

type Error = Error

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

fn try_from(value: &Bytes) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Bytes> for Packet

Source§

type Error = Error

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

fn try_from(value: Bytes) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl StructuralPartialEq for Packet

Auto Trait Implementations§

§

impl Freeze for Packet

§

impl RefUnwindSafe for Packet

§

impl Send for Packet

§

impl Sync for Packet

§

impl Unpin for Packet

§

impl UnwindSafe for Packet

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,