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

§

type Error = <T as TcpClientStack>::Error

§

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§