Struct embassy_net::tcp::TcpSocket
source · pub struct TcpSocket<'a> { /* private fields */ }
Expand description
A TCP socket.
Implementations§
source§impl<'a> TcpSocket<'a>
impl<'a> TcpSocket<'a>
sourcepub fn new<D: Driver>(
stack: &'a Stack<D>,
rx_buffer: &'a mut [u8],
tx_buffer: &'a mut [u8]
) -> Self
pub fn new<D: Driver>( stack: &'a Stack<D>, rx_buffer: &'a mut [u8], tx_buffer: &'a mut [u8] ) -> Self
Create a new TCP socket on the given stack, with the given buffers.
sourcepub fn recv_capacity(&self) -> usize
pub fn recv_capacity(&self) -> usize
Return the maximum number of bytes inside the recv buffer.
sourcepub fn send_capacity(&self) -> usize
pub fn send_capacity(&self) -> usize
Return the maximum number of bytes inside the transmit buffer.
sourcepub async fn write_with<F, R>(&mut self, f: F) -> Result<R, Error>
pub async fn write_with<F, R>(&mut self, f: F) -> Result<R, Error>
Call f
with the largest contiguous slice of octets in the transmit buffer,
and enqueue the amount of elements returned by f
.
If the socket is not ready to accept data, it waits until it is.
sourcepub async fn read_with<F, R>(&mut self, f: F) -> Result<R, Error>
pub async fn read_with<F, R>(&mut self, f: F) -> Result<R, Error>
Call f
with the largest contiguous slice of octets in the receive buffer,
and dequeue the amount of elements returned by f
.
If no data is available, it waits until there is at least one byte available.
sourcepub fn split(&mut self) -> (TcpReader<'_>, TcpWriter<'_>)
pub fn split(&mut self) -> (TcpReader<'_>, TcpWriter<'_>)
Split the socket into reader and a writer halves.
sourcepub async fn connect<T>(
&mut self,
remote_endpoint: T
) -> Result<(), ConnectError>where
T: Into<IpEndpoint>,
pub async fn connect<T>(
&mut self,
remote_endpoint: T
) -> Result<(), ConnectError>where
T: Into<IpEndpoint>,
Connect to a remote host.
sourcepub async fn accept<T>(&mut self, local_endpoint: T) -> Result<(), AcceptError>where
T: Into<IpListenEndpoint>,
pub async fn accept<T>(&mut self, local_endpoint: T) -> Result<(), AcceptError>where
T: Into<IpListenEndpoint>,
Accept a connection from a remote host.
This function puts the socket in listening mode, and waits until a connection is received.
sourcepub async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
Read data from the socket.
Returns how many bytes were read, or an error. If no data is available, it waits until there is at least one byte available.
sourcepub async fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
pub async fn write(&mut self, buf: &[u8]) -> Result<usize, Error>
Write data to the socket.
Returns how many bytes were written, or an error. If the socket is not ready to accept data, it waits until it is.
sourcepub async fn flush(&mut self) -> Result<(), Error>
pub async fn flush(&mut self) -> Result<(), Error>
Flushes the written data to the socket.
This waits until all data has been sent, and ACKed by the remote host. For a connection
closed with abort()
it will wait for the TCP RST packet to be sent.
sourcepub fn set_timeout(&mut self, duration: Option<Duration>)
pub fn set_timeout(&mut self, duration: Option<Duration>)
Set the timeout for the socket.
If the timeout is set, the socket will be closed if no data is received for the specified duration.
sourcepub fn set_keep_alive(&mut self, interval: Option<Duration>)
pub fn set_keep_alive(&mut self, interval: Option<Duration>)
Set the keep-alive interval for the socket.
If the keep-alive interval is set, the socket will send keep-alive packets after the specified duration of inactivity.
If not set, the socket will not send keep-alive packets.
sourcepub fn set_hop_limit(&mut self, hop_limit: Option<u8>)
pub fn set_hop_limit(&mut self, hop_limit: Option<u8>)
Set the hop limit field in the IP header of sent packets.
sourcepub fn local_endpoint(&self) -> Option<IpEndpoint>
pub fn local_endpoint(&self) -> Option<IpEndpoint>
Get the local endpoint of the socket.
Returns None
if the socket is not bound (listening) or not connected.
sourcepub fn remote_endpoint(&self) -> Option<IpEndpoint>
pub fn remote_endpoint(&self) -> Option<IpEndpoint>
Get the remote endpoint of the socket.
Returns None
if the socket is not connected.
sourcepub fn close(&mut self)
pub fn close(&mut self)
Close the write half of the socket.
This closes only the write half of the socket. The read half side remains open, the socket can still receive data.
Data that has been written to the socket and not yet sent (or not yet ACKed) will still still sent. The last segment of the pending to send data is sent with the FIN flag set.
sourcepub fn abort(&mut self)
pub fn abort(&mut self)
Forcibly close the socket.
This instantly closes both the read and write halves of the socket. Any pending data that has not been sent will be lost.
Note that the TCP RST packet is not sent immediately - if the TcpSocket
is dropped too soon
the remote host may not know the connection has been closed.
abort()
callers should wait for a flush()
call to complete before
dropping or reusing the socket.
sourcepub fn may_send(&self) -> bool
pub fn may_send(&self) -> bool
Get whether the socket is ready to send data, i.e. whether there is space in the send buffer.
sourcepub fn may_recv(&self) -> bool
pub fn may_recv(&self) -> bool
return whether the receive half of the full-duplex connection is open. This function returns true if it’s possible to receive data from the remote endpoint. It will return true while there is data in the receive buffer, and if there isn’t, as long as the remote endpoint has not closed the connection.
Trait Implementations§
source§impl<'d> Read for TcpSocket<'d>
impl<'d> Read for TcpSocket<'d>
source§async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>
source§async fn read_exact(
&mut self,
buf: &mut [u8]
) -> Result<(), ReadExactError<Self::Error>>
async fn read_exact( &mut self, buf: &mut [u8] ) -> Result<(), ReadExactError<Self::Error>>
buf
. Read more