Skip to main content

MessageDatagram

Struct MessageDatagram 

Source
pub struct MessageDatagram<D> { /* private fields */ }
Available on crate feature net only.
Expand description

A whole-message sender/receiver over a DatagramSocket (a UdpSocket, a UnixDatagram, or — under the mock feature — a MockDatagramSocket). It owns the socket and reuses one receive buffer, so each datagram is exchanged as a #[bin] value — the datagram counterpart to MessageStream. Unlike a stream, a datagram socket talks to many peers, so every call carries the peer address.

Implementations§

Source§

impl<D> MessageDatagram<D>

Source

pub fn new(sock: D) -> Self

Wrap a datagram socket, with a receive buffer sized for the largest datagram (64 KiB).

Source

pub fn with_capacity(sock: D, capacity: usize) -> Self

Wrap a socket with a receive buffer of capacity bytes. A datagram larger than this is truncated (as the OS itself would truncate an oversized recv).

Source

pub fn get_ref(&self) -> &D

Borrow the underlying socket (e.g. for connect, multicast, or a read timeout).

Source

pub fn get_mut(&mut self) -> &mut D

Mutably borrow the underlying socket.

Source

pub fn into_inner(self) -> D

Recover the underlying socket.

Source§

impl<D: DatagramSocket> MessageDatagram<D>

Source

pub fn send_message<T: BitEncode>( &self, msg: &T, addr: &D::Addr, ) -> Result<usize, BitError>

Encode msg and send it as one datagram to addr. Returns the bytes sent.

§Errors

A codec BitError or an I/O send error.

Source

pub fn recv_message<T: BitDecode + BitEncode>( &mut self, ) -> Result<(T, D::Addr), BitError>

Receive one datagram and decode it as a T, with the sender’s address.

§Errors

A codec BitError (the datagram wasn’t a valid T) or an I/O receive error.

Trait Implementations§

Source§

impl<D: Debug> Debug for MessageDatagram<D>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<D> Freeze for MessageDatagram<D>
where D: Freeze,

§

impl<D> RefUnwindSafe for MessageDatagram<D>
where D: RefUnwindSafe,

§

impl<D> Send for MessageDatagram<D>
where D: Send,

§

impl<D> Sync for MessageDatagram<D>
where D: Sync,

§

impl<D> Unpin for MessageDatagram<D>
where D: Unpin,

§

impl<D> UnsafeUnpin for MessageDatagram<D>
where D: UnsafeUnpin,

§

impl<D> UnwindSafe for MessageDatagram<D>
where D: UnwindSafe,

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more