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
impl UdpSocket
Sourcepub async fn bind<A: ToSocketAddrs>(addr: A) -> Result<UdpSocket>
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.
Sourcepub async fn send_to<A: ToSocketAddrs>(
&self,
buf: &[u8],
target: A,
) -> Result<usize>
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.
Sourcepub fn try_send_to<A: ToSocketAddrs>(
&self,
buf: &[u8],
target: A,
) -> Result<usize>
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
.
Sourcepub async fn writable(&self) -> Result<()>
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
.
Sourcepub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
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.
Sourcepub fn try_recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>
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()
.
Sourcepub async fn readable(&self) -> Result<()>
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
.
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
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()?;
Sourcepub fn broadcast(&self) -> Result<bool>
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
.
Sourcepub fn set_broadcast(&self, on: bool) -> Result<()>
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.
Sourcepub fn multicast_loop_v4(&self) -> Result<bool>
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
.
Sourcepub fn set_multicast_loop_v4(&self, on: bool) -> Result<()>
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.
Sourcepub fn multicast_loop_v6(&self) -> Result<bool>
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
.
Sourcepub fn set_multicast_loop_v6(&self, on: bool) -> Result<()>
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.
Sourcepub fn join_multicast_v4(
&self,
multiaddr: Ipv4Addr,
interface: Ipv4Addr,
) -> Result<()>
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
.
Sourcepub fn join_multicast_v6(
&self,
multiaddr: &Ipv6Addr,
interface: u32,
) -> Result<()>
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
.
Sourcepub fn leave_multicast_v4(
&self,
multiaddr: Ipv4Addr,
interface: Ipv4Addr,
) -> Result<()>
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
.
Sourcepub fn leave_multicast_v6(
&self,
multiaddr: &Ipv6Addr,
interface: u32,
) -> Result<()>
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
.