embassy-net

Crates

git

Versions

default

Flavors

Skip to main content

TcpListener

Struct TcpListener 

Source
pub struct TcpListener<'d> { /* private fields */ }
Expand description

A TCP listener.

A listener listens on a local endpoint, and queues the incoming connection attempts. accept takes the next queued attempt as an AcceptToken. Pass it to TcpSocket::accept to set up a socket for it, possibly in another task.

Connection attempts (SYN packets) are not answered (with a SYN|ACK packet) until you accept them. The queue depth is set by the tcp-listener-backlog-N feature of xarxa.

Implementations§

Source§

impl<'d> TcpListener<'d>

Source

pub fn new(stack: Stack<'d>) -> Result<Self, Full>

Create a new TCP listener on the given stack.

The listener starts closed. Call listen to start listening.

Errors:

  • Full if the stack has no room for another TCP listener. The limit is set by the tcp-listener-count-N feature of xarxa.
Source

pub fn bind_to_iface( &mut self, iface: Option<IfaceHandle>, ) -> Result<(), ListenError>

Bind the listener to an interface, or unbind it with None.

A listener bound to an interface only accepts connection attempts arriving on it, and the accepted sockets are bound to it too.

The listener must not be listening. The binding is kept across close.

Two listeners with otherwise identical endpoints may coexist if they are bound to different interfaces. A listener bound to the arrival interface wins over an unbound one with an equal endpoint.

Returns Err(ListenError::InvalidState) if the listener is listening.

Source

pub fn bound_iface(&self) -> Option<IfaceHandle>

Return the interface the listener is bound to, or None.

See bind_to_iface.

Source

pub fn listen<T>(&mut self, local_endpoint: T) -> Result<(), ListenError>

Start listening on the given endpoint.

The endpoint’s address, if any, scopes the listen: only connections to that address are accepted. Listening on the same port with a different scope is allowed, and the most specific listener gets the connection.

Listening on the endpoint the listener is already listening on is a no-op.

Source

pub fn close(&mut self)

Stop listening, dropping all queued connection attempts.

The dropped connection attempts are not reset. The clients’ retransmissions are answered with a RST once the listener is gone.

Source

pub fn is_open(&self) -> bool

Get whether the listener is listening.

Source

pub fn local_endpoint(&self) -> IpListenEndpoint

Get the endpoint the listener is listening on.

A zero port means the listener is closed.

Source

pub fn can_accept(&self) -> bool

Get whether a connection attempt is waiting to be accepted.

Source

pub fn wait_accept_ready(&self) -> impl Future<Output = ()> + '_

Wait until a connection attempt is waiting to be accepted.

This is the equivalent of accept, without accepting the connection.

Source

pub async fn accept(&mut self) -> Result<AcceptToken, AcceptError>

Accept a queued connection attempt, waiting if none is queued yet.

Returns an AcceptToken. Pass it to TcpSocket::accept to set up a socket for the incoming connection, possibly in another task.

Dropping the token forgets the attempt. The client retransmits its SYN, which queues the attempt on the listener again.

Errors:

  • InvalidState if the listener is not listening.

Trait Implementations§

Source§

impl Drop for TcpListener<'_>

Available on crate feature tcp-listener only.
Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'d> !RefUnwindSafe for TcpListener<'d>

§

impl<'d> !Send for TcpListener<'d>

§

impl<'d> !Sync for TcpListener<'d>

§

impl<'d> !UnwindSafe for TcpListener<'d>

§

impl<'d> Freeze for TcpListener<'d>

§

impl<'d> Unpin for TcpListener<'d>

§

impl<'d> UnsafeUnpin for TcpListener<'d>

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>,

Source§

type Error = !

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>,

Source§

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.