Struct UdpSocket

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

A simulated UDP socket.

All methods must be called from a host within a Turmoil simulation.

Implementations§

Source§

impl UdpSocket

Source

pub async fn bind<A: ToSocketAddrs>(addr: A) -> Result<UdpSocket>

This function will create a new UDP socket and attempt to bind it to the addr provided.

Binding with a port number of 0 will request that the OS assigns a port to this listener. The port allocated can be queried via the local_addr method.

Only 0.0.0.0, ::, or localhost are currently supported.

Source

pub async fn send_to<A: ToSocketAddrs>( &self, buf: &[u8], target: A, ) -> Result<usize>

Sends data on the socket to the given address. On success, returns the number of bytes written.

Address type can be any implementor of ToSocketAddrs trait. See its documentation for concrete examples.

It is possible for addr to yield multiple addresses, but send_to will only send data to the first address yielded by addr.

This will return an error when the IP version of the local socket does not match that returned from ToSocketAddrs.

§Cancel safety

This method is cancel safe. If send_to is used as the event in a tokio::select! statement and some other branch completes first, then it is guaranteed that the message was not sent.

Source

pub fn try_send_to<A: ToSocketAddrs>( &self, buf: &[u8], target: A, ) -> Result<usize>

Tries to send data on the socket to the given address, but if the send is blocked this will return right away.

This function is usually paired with writable().

§Returns

If successful, returns the number of bytes sent

Users should ensure that when the remote cannot receive, the ErrorKind::WouldBlock is properly handled. An error can also occur if the IP version of the socket does not match that of target.

Source

pub async fn writable(&self) -> Result<()>

Waits for the socket to become writable.

This function is usually paired with try_send_to().

The function may complete without the socket being writable. This is a false-positive and attempting a try_send_to() will return with io::ErrorKind::WouldBlock.

§Cancel safety

This method is cancel safe. Once a readiness event occurs, the method will continue to return immediately until the readiness event is consumed by an attempt to write that fails with WouldBlock or Poll::Pending.

Source

pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>

Receives a single datagram message on the socket. On success, returns the number of bytes read and the origin.

The function must be called with valid byte array buf of sufficient size to hold the message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be discarded.

Source

pub fn try_recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>

Tries to receive a single datagram message on the socket. On success, returns the number of bytes read and the origin.

The function must be called with valid byte array buf of sufficient size to hold the message bytes. If a message is too long to fit in the supplied buffer, excess bytes may be discarded.

When there is no pending data, Err(io::ErrorKind::WouldBlock) is returned. This function is usually paired with readable().

Source

pub async fn readable(&self) -> Result<()>

Waits for the socket to become readable.

This function is usually paired with try_recv_from().

The function may complete without the socket being readable. This is a false-positive and attempting a try_recv_from() will return with io::ErrorKind::WouldBlock.

§Cancel safety

This method is cancel safe. Once a readiness event occurs, the method will continue to return immediately until the readiness event is consumed by an attempt to read that fails with WouldBlock or Poll::Pending.

Source

pub fn local_addr(&self) -> Result<SocketAddr>

Returns the local address that this socket is bound to.

§Example
use turmoil::net::UdpSocket;

let addr = "0.0.0.0:8080".parse::<SocketAddr>().unwrap();
let sock = UdpSocket::bind(addr).await?;
// the address the socket is bound to
let local_addr = sock.local_addr()?;
Source

pub fn broadcast(&self) -> Result<bool>

Gets the value of the SO_BROADCAST option for this socket.

For more information about this option, see set_broadcast.

Source

pub fn set_broadcast(&self, on: bool) -> Result<()>

Sets the value of the SO_BROADCAST option for this socket.

When enabled, this socket is allowed to send packets to a broadcast address.

Source

pub fn multicast_loop_v4(&self) -> Result<bool>

Gets the value of the IP_MULTICAST_LOOP option for this socket.

For more information about this option, see set_multicast_loop_v4.

Source

pub fn set_multicast_loop_v4(&self, on: bool) -> Result<()>

Sets the value of the IP_MULTICAST_LOOP option for this socket.

If enabled, multicast packets will be looped back to the local socket.

§Note

This may not have any affect on IPv6 sockets.

Source

pub fn multicast_loop_v6(&self) -> Result<bool>

Gets the value of the IPV6_MULTICAST_LOOP option for this socket.

For more information about this option, see set_multicast_loop_v6.

Source

pub fn set_multicast_loop_v6(&self, on: bool) -> Result<()>

Sets the value of the IPV6_MULTICAST_LOOP option for this socket.

Controls whether this socket sees the multicast packets it sends itself.

§Note

This may not have any affect on IPv4 sockets.

Source

pub fn join_multicast_v4( &self, multiaddr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<()>

Executes an operation of the IP_ADD_MEMBERSHIP type.

This function specifies a new multicast group for this socket to join. The address must be a valid multicast address, and interface is the address of the local interface with which the system should join the multicast group. If it’s equal to INADDR_ANY then an appropriate interface is chosen by the system.

Currently, the interface argument only supports 127.0.0.1 and 0.0.0.0.

Source

pub fn join_multicast_v6( &self, multiaddr: &Ipv6Addr, interface: u32, ) -> Result<()>

Executes an operation of the IPV6_ADD_MEMBERSHIP type.

This function specifies a new multicast group for this socket to join. The address must be a valid multicast address, and interface is the index of the interface to join/leave (or 0 to indicate any interface).

Currently, the interface argument only supports 0.

Source

pub fn leave_multicast_v4( &self, multiaddr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<()>

Executes an operation of the IP_DROP_MEMBERSHIP type.

For more information about this option, see join_multicast_v4.

Source

pub fn leave_multicast_v6( &self, multiaddr: &Ipv6Addr, interface: u32, ) -> Result<()>

Executes an operation of the IPV6_DROP_MEMBERSHIP type.

For more information about this option, see join_multicast_v6.

Trait Implementations§

Source§

impl Debug for UdpSocket

Source§

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

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

impl Drop for UdpSocket

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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