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.

Use a TcpListener to accept incoming TCP connections.

A listener can be bound to a port and optionally an address. It receives all incoming connection attempts and queues them. Calling accept pops an attempt from the queue 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 a socket accepts 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 listener. Only possible without the alloc feature, where 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 that arrive on that interface.

When accepting a connection, the sockets inherit the binding.

The listener must be closed. The binding is kept across close.

Two listeners on the same address can coexist if they are bound to different interfaces, or if one is not bound. In the latter case, the bound listener “wins” for incoming connections coming from the bound interface.

§Errors
  • InvalidState: if the listener is open.
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( &mut self, local: impl Into<ListenSocketAddr>, ) -> Result<(), ListenError>

Start listening on the given local address.

§Errors
  • Unaddressable: if the port is zero.
  • InvalidState: if the listener is already listening (unless it is listening on this same address, which is a no-op).
  • InUse: if another listener is bound to an identical address. Listeners on the same port with different specificity (one wildcard, one per-version, one per-address) may coexist, and so may listeners on identical addresses bound to different interfaces.
Source

pub fn close(&mut self)

Stop listening, dropping all queued SYNs.

The dropped SYNs are not reset. The clients’ retransmissions are answered with an RST once the listener is gone.

Source

pub fn is_open(&self) -> bool

Whether the listener is listening.

Source

pub fn local_addr(&self) -> ListenSocketAddr

Return the listened address. The address is the filter the listen scoped the listener to. A zero port means the listener is closed.

Source

pub fn can_accept(&self) -> bool

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.