TcpStack

Trait TcpStack 

Source
pub trait TcpStack {
    type TcpSocket;
    type Error: Into<TcpError> + Debug;

    // Required methods
    fn open(&self, mode: Mode) -> Result<Self::TcpSocket, Self::Error>;
    fn connect(
        &self,
        socket: Self::TcpSocket,
        remote: HostSocketAddr,
    ) -> Result<Self::TcpSocket, Self::Error>;
    fn is_connected(
        &self,
        socket: &Self::TcpSocket,
    ) -> Result<bool, Self::Error>;
    fn write(
        &self,
        socket: &mut Self::TcpSocket,
        buffer: &[u8],
    ) -> Result<usize, Self::Error>;
    fn read(
        &self,
        socket: &mut Self::TcpSocket,
        buffer: &mut [u8],
    ) -> Result<usize, Self::Error>;
    fn close(&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 implemenation which knows how to driver the Rust Standard Library’s std::net module. Given this trait, you can how 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: Into<TcpError> + Debug

The type returned when we have an error

Required Methods§

Source

fn open(&self, mode: Mode) -> Result<Self::TcpSocket, Self::Error>

Open a new TCP socket. The socket starts in the unconnected state.

Source

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

Connect to the given remote host and port.

Source

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

Check if this socket is connected

Source

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

Write to the stream. Returns the number of bytes written is returned (which may be less than buffer.len()), or an error.

Source

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

Read 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.

Source

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

Close an existing TCP socket.

Implementors§