embedded_nal

Trait UdpClientStack

Source
pub trait UdpClientStack {
    type UdpSocket;
    type Error: Debug;

    // Required methods
    fn socket(&mut self) -> Result<Self::UdpSocket, Self::Error>;
    fn connect(
        &mut self,
        socket: &mut Self::UdpSocket,
        remote: SocketAddr,
    ) -> Result<(), Self::Error>;
    fn send(
        &mut self,
        socket: &mut Self::UdpSocket,
        buffer: &[u8],
    ) -> Result<(), Self::Error>;
    fn receive(
        &mut self,
        socket: &mut Self::UdpSocket,
        buffer: &mut [u8],
    ) -> Result<(usize, SocketAddr), Self::Error>;
    fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error>;
}
Expand description

This trait is implemented by UDP/IP stacks. You could, for example, have an implementation which knows how to send AT commands to an ESP8266 WiFi module. You could have another implementation which knows how to driver the Rust Standard Library’s std::net module. Given this trait, you can how write a portable CoAP client which can work with either implementation.

Required Associated Types§

Source

type UdpSocket

The type returned when we create a new UDP socket

Source

type Error: Debug

The type returned when we have an error

Required Methods§

Source

fn socket(&mut self) -> Result<Self::UdpSocket, Self::Error>

Allocate a socket for further use.

Source

fn connect( &mut self, socket: &mut Self::UdpSocket, remote: SocketAddr, ) -> Result<(), Self::Error>

Connect a UDP socket with a peer using a dynamically selected port.

Selects a port number automatically and initializes for read/writing.

Source

fn send( &mut self, socket: &mut Self::UdpSocket, buffer: &[u8], ) -> Result<(), Self::Error>

Send a datagram to the remote host.

The remote host used is either the one specified in UdpStack::connect or the last one used in UdpServerStack::write_to.

Source

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

Read a datagram the remote host has sent to us.

Returns Ok((n, remote)), which means a datagram of size n has been received from remote and been placed in &buffer[0..n], or an error. If a packet has not been received when called, then nb::Error::WouldBlock should be returned.

Source

fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error>

Close an existing UDP socket.

Implementations on Foreign Types§

Source§

impl<T: UdpClientStack> UdpClientStack for &mut T

Source§

type Error = <T as UdpClientStack>::Error

Source§

type UdpSocket = <T as UdpClientStack>::UdpSocket

Source§

fn socket(&mut self) -> Result<Self::UdpSocket, Self::Error>

Source§

fn connect( &mut self, socket: &mut Self::UdpSocket, remote: SocketAddr, ) -> Result<(), Self::Error>

Source§

fn send( &mut self, socket: &mut Self::UdpSocket, buffer: &[u8], ) -> Result<(), Self::Error>

Source§

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

Source§

fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error>

Implementors§