Struct edge_nal_std::UdpSocket

source ·
pub struct UdpSocket(/* private fields */);

Implementations§

source§

impl UdpSocket

source

pub const fn new(socket: Async<StdUdpSocket>) -> Self

source

pub fn release(self) -> Async<StdUdpSocket>

source

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

source

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

Methods from Deref<Target = Async<StdUdpSocket>>§

source

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();
source

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?;
source

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.

source

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?;
source

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.

source

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 futures_lite::future;
use std::net::TcpListener;

let mut listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 0))?;

// Wait until a client can be accepted.
future::poll_fn(|cx| listener.poll_readable(cx)).await?;
source

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 futures_lite::future;
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.
future::poll_fn(|cx| stream.poll_writable(cx)).await?;
source

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?;
source

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?;
source

pub async fn accept(&self) -> Result<(Async<TcpStream>, SocketAddr), Error>

Accepts a new incoming TCP connection.

When a connection is established, it will be returned as a TCP stream together with its remote address.

§Examples
use async_io::Async;
use std::net::TcpListener;

let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 8000))?;
let (stream, addr) = listener.accept().await?;
println!("Accepted client: {}", addr);
source

pub fn incoming( &self, ) -> impl Stream<Item = Result<Async<TcpStream>, Error>> + Send

Returns a stream of incoming TCP connections.

The stream is infinite, i.e. it never stops with a None.

§Examples
use async_io::Async;
use futures_lite::{pin, stream::StreamExt};
use std::net::TcpListener;

let listener = Async::<TcpListener>::bind(([127, 0, 0, 1], 8000))?;
let incoming = listener.incoming();
pin!(incoming);

while let Some(stream) = incoming.next().await {
    let stream = stream?;
    println!("Accepted client: {}", stream.get_ref().peer_addr()?);
}
source

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

Reads data from the stream without removing it from the buffer.

Returns the number of bytes read. Successive calls of this method read the same data.

§Examples
use async_io::Async;
use futures_lite::{io::AsyncWriteExt, stream::StreamExt};
use std::net::{TcpStream, ToSocketAddrs};

let addr = "example.com:80".to_socket_addrs()?.next().unwrap();
let mut stream = Async::<TcpStream>::connect(addr).await?;

stream
    .write_all(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
    .await?;

let mut buf = [0u8; 1024];
let len = stream.peek(&mut buf).await?;
source

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?;
source

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?;
source

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 writen.

§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?;
source

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?;
source

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?;
source

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?;
source

pub async fn accept(&self) -> Result<(Async<UnixStream>, SocketAddr), Error>

Accepts a new incoming UDS stream connection.

When a connection is established, it will be returned as a stream together with its remote address.

§Examples
use async_io::Async;
use std::os::unix::net::UnixListener;

let listener = Async::<UnixListener>::bind("/tmp/socket")?;
let (stream, addr) = listener.accept().await?;
println!("Accepted client: {:?}", addr);
source

pub fn incoming( &self, ) -> impl Stream<Item = Result<Async<UnixStream>, Error>> + Send

Returns a stream of incoming UDS connections.

The stream is infinite, i.e. it never stops with a None item.

§Examples
use async_io::Async;
use futures_lite::{pin, stream::StreamExt};
use std::os::unix::net::UnixListener;

let listener = Async::<UnixListener>::bind("/tmp/socket")?;
let incoming = listener.incoming();
pin!(incoming);

while let Some(stream) = incoming.next().await {
    let stream = stream?;
    println!("Accepted client: {:?}", stream.get_ref().peer_addr()?);
}
source

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

Receives data from the socket.

Returns the number of bytes read and the address the message came from.

§Examples
use async_io::Async;
use std::os::unix::net::UnixDatagram;

let socket = Async::<UnixDatagram>::bind("/tmp/socket")?;

let mut buf = [0u8; 1024];
let (len, addr) = socket.recv_from(&mut buf).await?;
source

pub async fn send_to<P>(&self, buf: &[u8], path: P) -> Result<usize, Error>
where P: AsRef<Path>,

Sends data to the specified address.

Returns the number of bytes written.

§Examples
use async_io::Async;
use std::os::unix::net::UnixDatagram;

let socket = Async::<UnixDatagram>::unbound()?;

let msg = b"hello";
let addr = "/tmp/socket";
let len = socket.send_to(msg, addr).await?;
source

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

Receives data from the connected peer.

Returns the number of bytes read and the address the message came from.

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::os::unix::net::UnixDatagram;

let socket = Async::<UnixDatagram>::bind("/tmp/socket1")?;
socket.get_ref().connect("/tmp/socket2")?;

let mut buf = [0u8; 1024];
let len = socket.recv(&mut buf).await?;
source

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::os::unix::net::UnixDatagram;

let socket = Async::<UnixDatagram>::bind("/tmp/socket1")?;
socket.get_ref().connect("/tmp/socket2")?;

let msg = b"hello";
let len = socket.send(msg).await?;

Trait Implementations§

source§

impl Deref for UdpSocket

source§

type Target = Async<UdpSocket>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl ErrorType for &UdpSocket

source§

type Error = Error

Error type of all the IO operations on this type.
source§

impl ErrorType for UdpSocket

source§

type Error = Error

Error type of all the IO operations on this type.
source§

impl MulticastV4 for &UdpSocket

source§

async fn join_v4( &mut self, multicast_addr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<(), Self::Error>

source§

async fn leave_v4( &mut self, multicast_addr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<(), Self::Error>

source§

impl MulticastV4 for UdpSocket

source§

async fn join_v4( &mut self, multicast_addr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<(), Self::Error>

source§

async fn leave_v4( &mut self, multicast_addr: Ipv4Addr, interface: Ipv4Addr, ) -> Result<(), Self::Error>

source§

impl MulticastV6 for &UdpSocket

source§

async fn join_v6( &mut self, multicast_addr: Ipv6Addr, interface: u32, ) -> Result<(), Self::Error>

source§

async fn leave_v6( &mut self, multicast_addr: Ipv6Addr, interface: u32, ) -> Result<(), Self::Error>

source§

impl MulticastV6 for UdpSocket

source§

async fn join_v6( &mut self, multicast_addr: Ipv6Addr, interface: u32, ) -> Result<(), Self::Error>

source§

async fn leave_v6( &mut self, multicast_addr: Ipv6Addr, interface: u32, ) -> Result<(), Self::Error>

source§

impl Readable for &UdpSocket

source§

async fn readable(&mut self) -> Result<(), Self::Error>

source§

impl Readable for UdpSocket

source§

async fn readable(&mut self) -> Result<(), Self::Error>

source§

impl UdpReceive for &UdpSocket

source§

async fn receive( &mut self, buffer: &mut [u8], ) -> Result<(usize, SocketAddr), Self::Error>

Receive a datagram into the provided buffer. Read more
source§

impl UdpReceive for UdpSocket

source§

async fn receive( &mut self, buffer: &mut [u8], ) -> Result<(usize, SocketAddr), Self::Error>

Receive a datagram into the provided buffer. Read more
source§

impl UdpSend for &UdpSocket

source§

async fn send( &mut self, remote: SocketAddr, data: &[u8], ) -> Result<(), Self::Error>

Send the provided data to a peer: Read more
source§

impl UdpSend for UdpSocket

source§

async fn send( &mut self, remote: SocketAddr, data: &[u8], ) -> Result<(), Self::Error>

Send the provided data to a peer: Read more
source§

impl UdpSplit for UdpSocket

source§

type Receive<'a> = &'a UdpSocket where Self: 'a

source§

type Send<'a> = &'a UdpSocket where Self: 'a

source§

fn split(&mut self) -> (Self::Receive<'_>, Self::Send<'_>)

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<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