embedded_nal

Trait TcpClientStack

Source
pub trait TcpClientStack {
    type TcpSocket;
    type Error: TcpError;

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

This trait is implemented by TCP/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 write a portable HTTP client which can work with either implementation.

Required Associated Types§

Source

type TcpSocket

The type returned when we create a new TCP socket

Source

type Error: TcpError

The type returned when we have an error

Required Methods§

Source

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

Open a socket for usage as a TCP client.

The socket must be connected before it can be used.

Returns Ok(socket) if the socket was successfully created.

Source

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

Connect to the given remote host and port.

Returns Ok if the connection was successful. Otherwise, if the connection could not be completed immediately, this function should return nb::Error::WouldBlock.

Source

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

Write to the stream.

Returns the number of bytes written (which may be less than buffer.len()) or an error.

Source

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

Receive data from the stream.

Returns Ok(n), which means n bytes of data have been received and they have 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::TcpSocket) -> Result<(), Self::Error>

Close an existing TCP socket.

Implementations on Foreign Types§

Source§

impl<T: TcpClientStack> TcpClientStack for &mut T

Source§

type Error = <T as TcpClientStack>::Error

Source§

type TcpSocket = <T as TcpClientStack>::TcpSocket

Source§

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

Source§

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

Source§

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

Source§

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

Source§

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

Implementors§