Struct monoio::net::udp::UdpSocket

source ·
pub struct UdpSocket { /* private fields */ }
Expand description

A UDP socket.

After creating a UdpSocket by [bind]ing it to a socket address, data can be [sent to] and [received from] any other socket address.

Although UDP is a connectionless protocol, this implementation provides an interface to set an address where data should be sent and received from. After setting a remote address with [connect], data can be sent to and received from that address with [send] and [recv].

Implementations§

source§

impl UdpSocket

source

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

Creates a UDP socket from the given address.

source

pub async fn recv_from<T: IoBufMut>( &self, buf: T ) -> BufResult<(usize, SocketAddr), T>

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

source

pub async fn send_to<T: IoBuf>( &self, buf: T, socket_addr: SocketAddr ) -> BufResult<usize, T>

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

source

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

Returns the socket address of the remote peer this socket was connected to.

source

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

Returns the socket address that this socket was created from.

source

pub async fn connect(&self, socket_addr: SocketAddr) -> Result<()>

Connects this UDP socket to a remote address, allowing the send and recv syscalls to be used to send data and also applies filters to only receive data from the specified address.

source

pub async fn send<T: IoBuf>(&self, buf: T) -> BufResult<usize, T>

Sends data on the socket to the remote address to which it is connected.

source

pub async fn recv<T: IoBufMut>(&self, buf: T) -> BufResult<usize, T>

Receives a single datagram message on the socket from the remote address to which it is connected. On success, returns the number of bytes read.

source

pub fn from_std(socket: UdpSocket) -> Result<Self>

Creates new UdpSocket from a std::net::UdpSocket.

source

pub fn set_reuse_address(&self, reuse: bool) -> Result<()>

Set value for the SO_REUSEADDR option on this socket.

source

pub fn set_reuse_port(&self, reuse: bool) -> Result<()>

Set value for the SO_REUSEPORT option on this socket.

source

pub async fn readable(&self, relaxed: bool) -> Result<()>

Wait for read readiness. Note: Do not use it before every io. It is different from other runtimes!

Everytime call to this method may pay a syscall cost. In uring impl, it will push a PollAdd op; in epoll impl, it will use use inner readiness state; if !relaxed, it will call syscall poll after that.

If relaxed, on legacy driver it may return false positive result. If you want to do io by your own, you must maintain io readiness and wait for io ready with relaxed=false.

source

pub async fn writable(&self, relaxed: bool) -> Result<()>

Wait for write readiness. Note: Do not use it before every io. It is different from other runtimes!

Everytime call to this method may pay a syscall cost. In uring impl, it will push a PollAdd op; in epoll impl, it will use use inner readiness state; if !relaxed, it will call syscall poll after that.

If relaxed, on legacy driver it may return false positive result. If you want to do io by your own, you must maintain io readiness and wait for io ready with relaxed=false.

source§

impl UdpSocket

Cancelable related methods

source

pub async fn cancelable_recv_from<T: IoBufMut>( &self, buf: T, c: CancelHandle ) -> BufResult<(usize, SocketAddr), T>

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

source

pub async fn cancelable_send_to<T: IoBuf>( &self, buf: T, socket_addr: SocketAddr, c: CancelHandle ) -> BufResult<usize, T>

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

source

pub async fn cancelable_send<T: IoBuf>( &self, buf: T, c: CancelHandle ) -> BufResult<usize, T>

Sends data on the socket to the remote address to which it is connected.

source

pub async fn cancelable_recv<T: IoBufMut>( &self, buf: T, c: CancelHandle ) -> BufResult<usize, T>

Receives a single datagram message on the socket from the remote address to which it is connected. On success, returns the number of bytes read.

Trait Implementations§

source§

impl AsRawFd for UdpSocket

source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
source§

impl Debug for UdpSocket

source§

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

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

impl Split for UdpSocket

UdpSocket is safe to split to two parts

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.