pub struct TcpSocket<'a> { /* private fields */ }
Expand description
A TCP socket.
Implementations§
Source§impl<'a> TcpSocket<'a>
impl<'a> TcpSocket<'a>
Sourcepub fn new(
stack: Stack<'a>,
rx_buffer: &'a mut [u8],
tx_buffer: &'a mut [u8],
) -> Self
pub fn new( stack: Stack<'a>, 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 fn send_queue(&self) -> usize
pub fn send_queue(&self) -> usize
Return the amount of octets queued in the transmit buffer.
Sourcepub fn recv_queue(&self) -> usize
pub fn recv_queue(&self) -> usize
Return the amount of octets queued in the receive buffer. This value can be larger than
the slice read by the next recv
or peek
call because it includes all queued octets,
and not only the octets that may be returned as a contiguous slice.
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 wait_read_ready(&self)
pub async fn wait_read_ready(&self)
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.
A return value of Ok(0) means that the socket was closed and is longer able to receive any data.
Sourcepub async fn wait_write_ready(&self)
pub async fn wait_write_ready(&self)
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.
§Note:
Set a keep alive interval ([set_keep_alive
] to prevent timeouts when
the remote could still respond.
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.
By setting a timeout
larger then the keep alive you
can detect a remote endpoint that no longer answers.
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
Return whether the transmit half of the full-duplex connection is open.
This function returns true if it’s possible to send data and have it arrive to the remote endpoint. However, it does not make any guarantees about the state of the transmit buffer, and even if it returns true, write may not be able to enqueue any octets.
In terms of the TCP state machine, the socket must be in the ESTABLISHED
or
CLOSE-WAIT
state.
Sourcepub fn can_send(&self) -> bool
pub fn can_send(&self) -> bool
Check whether the transmit half of the full-duplex connection is open (see may_send), and the transmit buffer is not full.
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