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§
Sourceasync fn bind(&mut self, address: SocketAddr) -> Result<(), Error>
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
.
Sourcefn local_addr(&self) -> Result<SocketAddr, Error>
fn local_addr(&self) -> Result<SocketAddr, Error>
Returns the local address this socket is bound to.
Sourceasync fn recv_from(
&self,
buffer: &mut [u8],
) -> Result<(usize, SocketAddr), Error>
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.
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.