pub struct UdpSocket(/* private fields */);Expand description
The UDP socket type for the STD network stack.
Implementations§
Source§impl UdpSocket
impl UdpSocket
Sourcepub const fn new(socket: Async<StdUdpSocket>) -> Self
pub const fn new(socket: Async<StdUdpSocket>) -> Self
Create a new UDP socket from the given async UDP socket.
§Arguments
socket: The async UDP socket to wrap.
Sourcepub fn release(self) -> Async<StdUdpSocket>
pub fn release(self) -> Async<StdUdpSocket>
Release the underlying async UDP socket.
Methods from Deref<Target = Async<StdUdpSocket>>§
Sourcepub fn get_ref(&self) -> &T
pub fn get_ref(&self) -> &T
Gets a reference to the inner I/O handle.
§Examples
use async_io::Async;
use std::net::TcpListener;
let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
let inner = listener.get_ref();Sourcepub fn readable(&self) -> Readable<'_, T> ⓘ
pub fn readable(&self) -> Readable<'_, T> ⓘ
Waits until the I/O handle is readable.
This method completes when a read operation on this I/O handle wouldn’t block.
§Examples
use async_io::Async;
use std::net::TcpListener;
let mut listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
// Wait until a client can be accepted.
listener.readable().await?;Sourcepub fn readable_owned(self: Arc<Async<T>>) -> ReadableOwned<T> ⓘ
pub fn readable_owned(self: Arc<Async<T>>) -> ReadableOwned<T> ⓘ
Waits until the I/O handle is readable.
This method completes when a read operation on this I/O handle wouldn’t block.
Sourcepub fn writable(&self) -> Writable<'_, T> ⓘ
pub fn writable(&self) -> Writable<'_, T> ⓘ
Waits until the I/O handle is writable.
This method completes when a write operation on this I/O handle wouldn’t block.
§Examples
use async_io::Async;
use std::net::{TcpStream, ToSocketAddrs};
let addr = "example.com:80".to_socket_addrs()?.next().unwrap();
let stream = Async::<TcpStream>::connect(addr).await?;
// Wait until the stream is writable.
stream.writable().await?;Sourcepub fn writable_owned(self: Arc<Async<T>>) -> WritableOwned<T> ⓘ
pub fn writable_owned(self: Arc<Async<T>>) -> WritableOwned<T> ⓘ
Waits until the I/O handle is writable.
This method completes when a write operation on this I/O handle wouldn’t block.
Sourcepub fn poll_readable(&self, cx: &mut Context<'_>) -> Poll<Result<(), Error>>
pub fn poll_readable(&self, cx: &mut Context<'_>) -> Poll<Result<(), Error>>
Polls the I/O handle for readability.
When this method returns Poll::Ready, that means the OS has delivered an event
indicating readability since the last time this task has called the method and received
Poll::Pending.
§Caveats
Two different tasks should not call this method concurrently. Otherwise, conflicting tasks will just keep waking each other in turn, thus wasting CPU time.
Note that the AsyncRead implementation for Async also uses this method.
§Examples
use async_io::Async;
use std::future::poll_fn;
use std::net::TcpListener;
let mut listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
// Wait until a client can be accepted.
poll_fn(|cx| listener.poll_readable(cx)).await?;Sourcepub fn poll_writable(&self, cx: &mut Context<'_>) -> Poll<Result<(), Error>>
pub fn poll_writable(&self, cx: &mut Context<'_>) -> Poll<Result<(), Error>>
Polls the I/O handle for writability.
When this method returns Poll::Ready, that means the OS has delivered an event
indicating writability since the last time this task has called the method and received
Poll::Pending.
§Caveats
Two different tasks should not call this method concurrently. Otherwise, conflicting tasks will just keep waking each other in turn, thus wasting CPU time.
Note that the AsyncWrite implementation for Async also uses this method.
§Examples
use async_io::Async;
use std::future::poll_fn;
use std::net::{TcpStream, ToSocketAddrs};
let addr = "example.com:80".to_socket_addrs()?.next().unwrap();
let stream = Async::<TcpStream>::connect(addr).await?;
// Wait until the stream is writable.
poll_fn(|cx| stream.poll_writable(cx)).await?;Sourcepub async fn read_with<R>(
&self,
op: impl FnMut(&T) -> Result<R, Error>,
) -> Result<R, Error>
pub async fn read_with<R>( &self, op: impl FnMut(&T) -> Result<R, Error>, ) -> Result<R, Error>
Performs a read operation asynchronously.
The I/O handle is registered in the reactor and put in non-blocking mode. This method
invokes the op closure in a loop until it succeeds or returns an error other than
io::ErrorKind::WouldBlock. In between iterations of the loop, it waits until the OS
sends a notification that the I/O handle is readable.
The closure receives a shared reference to the I/O handle.
§Examples
use async_io::Async;
use std::net::TcpListener;
let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;
// Accept a new client asynchronously.
let (stream, addr) = listener.read_with(|l| l.accept()).await?;Sourcepub async fn write_with<R>(
&self,
op: impl FnMut(&T) -> Result<R, Error>,
) -> Result<R, Error>
pub async fn write_with<R>( &self, op: impl FnMut(&T) -> Result<R, Error>, ) -> Result<R, Error>
Performs a write operation asynchronously.
The I/O handle is registered in the reactor and put in non-blocking mode. This method
invokes the op closure in a loop until it succeeds or returns an error other than
io::ErrorKind::WouldBlock. In between iterations of the loop, it waits until the OS
sends a notification that the I/O handle is writable.
The closure receives a shared reference to the I/O handle.
§Examples
use async_io::Async;
use std::net::UdpSocket;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 8000))?;
socket.get_ref().connect("127.0.0.1:9000")?;
let msg = b"hello";
let len = socket.write_with(|s| s.send(msg)).await?;Sourcepub async fn recv_from(
&self,
buf: &mut [u8],
) -> Result<(usize, SocketAddr), Error>
pub async fn recv_from( &self, buf: &mut [u8], ) -> Result<(usize, SocketAddr), Error>
Receives a single datagram message.
Returns the number of bytes read and the address the message came from.
This method must be called with a valid byte slice of sufficient size to hold the message. If the message is too long to fit, excess bytes may get discarded.
§Examples
use async_io::Async;
use std::net::UdpSocket;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 8000))?;
let mut buf = [0u8; 1024];
let (len, addr) = socket.recv_from(&mut buf).await?;Sourcepub async fn peek_from(
&self,
buf: &mut [u8],
) -> Result<(usize, SocketAddr), Error>
pub async fn peek_from( &self, buf: &mut [u8], ) -> Result<(usize, SocketAddr), Error>
Receives a single datagram message without removing it from the queue.
Returns the number of bytes read and the address the message came from.
This method must be called with a valid byte slice of sufficient size to hold the message. If the message is too long to fit, excess bytes may get discarded.
§Examples
use async_io::Async;
use std::net::UdpSocket;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 8000))?;
let mut buf = [0u8; 1024];
let (len, addr) = socket.peek_from(&mut buf).await?;Sourcepub async fn send_to<A>(&self, buf: &[u8], addr: A) -> Result<usize, Error>where
A: Into<SocketAddr>,
pub async fn send_to<A>(&self, buf: &[u8], addr: A) -> Result<usize, Error>where
A: Into<SocketAddr>,
Sends data to the specified address.
Returns the number of bytes written.
§Examples
use async_io::Async;
use std::net::UdpSocket;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 0))?;
let addr = socket.get_ref().local_addr()?;
let msg = b"hello";
let len = socket.send_to(msg, addr).await?;Sourcepub async fn recv(&self, buf: &mut [u8]) -> Result<usize, Error>
pub async fn recv(&self, buf: &mut [u8]) -> Result<usize, Error>
Receives a single datagram message from the connected peer.
Returns the number of bytes read.
This method must be called with a valid byte slice of sufficient size to hold the message. If the message is too long to fit, excess bytes may get discarded.
The connect method connects this socket to a remote address.
This method will fail if the socket is not connected.
§Examples
use async_io::Async;
use std::net::UdpSocket;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 8000))?;
socket.get_ref().connect("127.0.0.1:9000")?;
let mut buf = [0u8; 1024];
let len = socket.recv(&mut buf).await?;Sourcepub async fn peek(&self, buf: &mut [u8]) -> Result<usize, Error>
pub async fn peek(&self, buf: &mut [u8]) -> Result<usize, Error>
Receives a single datagram message from the connected peer without removing it from the queue.
Returns the number of bytes read and the address the message came from.
This method must be called with a valid byte slice of sufficient size to hold the message. If the message is too long to fit, excess bytes may get discarded.
The connect method connects this socket to a remote address.
This method will fail if the socket is not connected.
§Examples
use async_io::Async;
use std::net::UdpSocket;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 8000))?;
socket.get_ref().connect("127.0.0.1:9000")?;
let mut buf = [0u8; 1024];
let len = socket.peek(&mut buf).await?;Sourcepub async fn send(&self, buf: &[u8]) -> Result<usize, Error>
pub async fn send(&self, buf: &[u8]) -> Result<usize, Error>
Sends data to the connected peer.
Returns the number of bytes written.
The connect method connects this socket to a remote address.
This method will fail if the socket is not connected.
§Examples
use async_io::Async;
use std::net::UdpSocket;
let socket = Async::<UdpSocket>::bind(([127, 0, 0, 1], 8000))?;
socket.get_ref().connect("127.0.0.1:9000")?;
let msg = b"hello";
let len = socket.send(msg).await?;