Embassy
embassy-net

Crates

git

Versions

default

Flavors

Struct embassy_net::tcp::TcpSocket

source ·
pub struct TcpSocket<'a> { /* private fields */ }
Expand description

A TCP socket.

Implementations§

source§

impl<'a> TcpSocket<'a>

source

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.

source

pub fn recv_capacity(&self) -> usize

Return the maximum number of bytes inside the recv buffer.

source

pub fn send_capacity(&self) -> usize

Return the maximum number of bytes inside the transmit buffer.

source

pub async fn write_with<F, R>(&mut self, f: F) -> Result<R, Error>
where F: FnOnce(&mut [u8]) -> (usize, R),

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.

source

pub async fn read_with<F, R>(&mut self, f: F) -> Result<R, Error>
where F: FnOnce(&mut [u8]) -> (usize, R),

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.

source

pub fn split(&mut self) -> (TcpReader<'_>, TcpWriter<'_>)

Split the socket into reader and a writer halves.

source

pub async fn connect<T>( &mut self, remote_endpoint: T ) -> Result<(), ConnectError>
where T: Into<IpEndpoint>,

Connect to a remote host.

source

pub async fn accept<T>(&mut self, local_endpoint: T) -> Result<(), AcceptError>

Accept a connection from a remote host.

This function puts the socket in listening mode, and waits until a connection is received.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn set_hop_limit(&mut self, hop_limit: Option<u8>)

Set the hop limit field in the IP header of sent packets.

source

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.

source

pub fn remote_endpoint(&self) -> Option<IpEndpoint>

Get the remote endpoint of the socket.

Returns None if the socket is not connected.

source

pub fn state(&self) -> State

Get the state of the socket.

source

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.

source

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.

source

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.

source

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.

source

pub fn can_recv(&self) -> bool

Get whether the socket is ready to receive data, i.e. whether there is some pending data in the receive buffer.

Trait Implementations§

source§

impl<'a> Drop for TcpSocket<'a>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'d> ErrorType for TcpSocket<'d>

§

type Error = Error

Error type of all the IO operations on this type.
source§

impl<'d> Read for TcpSocket<'d>

source§

async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>

Read some bytes from this source into the specified buffer, returning how many bytes were read. Read more
source§

async fn read_exact( &mut self, buf: &mut [u8] ) -> Result<(), ReadExactError<Self::Error>>

Read the exact number of bytes required to fill buf. Read more
source§

impl<'d> ReadReady for TcpSocket<'d>

source§

fn read_ready(&mut self) -> Result<bool, Self::Error>

Get whether the reader is ready for immediately reading. Read more
source§

impl<'d> Write for TcpSocket<'d>

source§

async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>

Write a buffer into this writer, returning how many bytes were written. Read more
source§

async fn flush(&mut self) -> Result<(), Self::Error>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
source§

async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error>

Write an entire buffer into this writer. Read more
source§

impl<'d> WriteReady for TcpSocket<'d>

source§

fn write_ready(&mut self) -> Result<bool, Self::Error>

Get whether the writer is ready for immediately writing. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for TcpSocket<'a>

§

impl<'a> !RefUnwindSafe for TcpSocket<'a>

§

impl<'a> !Send for TcpSocket<'a>

§

impl<'a> !Sync for TcpSocket<'a>

§

impl<'a> Unpin for TcpSocket<'a>

§

impl<'a> !UnwindSafe for TcpSocket<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.