Trait UdpSocket

Source
pub trait UdpSocket {
    // Required methods
    async fn bind(&mut self, address: SocketAddr) -> Result<(), Error>;
    fn local_addr(&self) -> Result<SocketAddr, Error>;
    async fn recv_from(
        &self,
        buffer: &mut [u8],
    ) -> Result<(usize, SocketAddr), Error>;
    async fn send_to(
        &self,
        buffer: &[u8],
        address: SocketAddr,
    ) -> Result<usize, Error>;
    fn close(&mut self);
}
Expand description

UDP socket for sending and receiving datagrams.

UDP is connectionless - each send/receive operation can target different addresses.

§Example

use veecle_osal_api::net::udp::UdpSocket;
use core::net::SocketAddr;

async fn udp_echo<S>(mut socket: S)
where
    S: UdpSocket
{
    let addr: SocketAddr = "0.0.0.0:8080".parse().unwrap();
    socket.bind(addr).await.unwrap();

    let mut buffer = [0u8; 1500];
    loop {
        let (size, peer) = socket.recv_from(&mut buffer).await.unwrap();
        socket.send_to(&buffer[..size], peer).await.unwrap();
    }
}

Required Methods§

Source

async fn bind(&mut self, address: SocketAddr) -> Result<(), Error>

Binds the socket to a local address.

If the specified port is 0, the port is assigned automatically and can be queried with Self::local_addr.

Source

fn local_addr(&self) -> Result<SocketAddr, Error>

Returns the local address this socket is bound to.

Source

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

Receives a datagram.

Returns the number of bytes received and the sender’s address. If the datagram is larger than the buffer, excess bytes may be discarded.

Source

async fn send_to( &self, buffer: &[u8], address: SocketAddr, ) -> Result<usize, Error>

Sends a datagram to the specified address.

Returns the number of bytes sent.

Source

fn close(&mut self)

Closes the UDP socket.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl UdpSocket for veecle_os::osal::std::net::udp::UdpSocket

§

impl<'a> UdpSocket for veecle_os::osal::embassy::net::udp::UdpSocket<'a>