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§
Required Methods§
Sourcefn socket(&mut self) -> Result<Self::TcpSocket, Self::Error>
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.
Sourcefn connect(
&mut self,
socket: &mut Self::TcpSocket,
remote: SocketAddr,
) -> Result<(), Self::Error>
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
.
Sourcefn send(
&mut self,
socket: &mut Self::TcpSocket,
buffer: &[u8],
) -> Result<usize, Self::Error>
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.
Sourcefn receive(
&mut self,
socket: &mut Self::TcpSocket,
buffer: &mut [u8],
) -> Result<usize, Self::Error>
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.