xarxa

Crates

git

Versions

default

Flavors

Skip to main content

TcpListener

Struct TcpListener 

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

A TCP listener borrowed from a Stack, returned by Stack::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.

Connection attempts (SYN packets) are not answered (with a SYN|ACK packet) until a socket accepts them.

Implementations§

Source§

impl TcpListener<'_>

Source

pub fn listen( &mut self, local: impl Into<ListenSocketAddr>, ) -> Result<(), ListenError>

Start listening on the given local address.

Returns:

  • Err(ListenError::Unaddressable) if the port is zero.
  • Err(ListenError::InvalidState) if the listener is already listening (unless it is listening on this same address, which is a no-op).
  • Err(ListenError::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 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.

Returns Err(ListenError::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 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 register_accept_waker(&mut self, waker: &Waker)

Register a waker for accept.

The waker is woken on state changes that might affect the return value of accept calls, such as a SYN being queued, or the listener closing.

Notes:

  • Only one waker can be registered at a time. If another waker was previously registered, it is overwritten and will no longer be woken.
  • The Waker is woken only once. Once woken, you must register it again before it may be woken again.
  • “Spurious wakes” are allowed: a wake doesn’t guarantee the result of accept has changed.
Source

pub fn can_accept(&self) -> bool

Whether a connection attempt is waiting to be accepted.

Source

pub fn accept(&mut self) -> Option<AcceptToken>

Accept a queued connection attempt.

Returns an AcceptToken, or None if none is queued.

Pass it to TcpSocket::accept to set up a socket for the incoming connection.

Auto Trait Implementations§

§

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

§

impl<'a> Freeze for TcpListener<'a>

§

impl<'a> RefUnwindSafe for TcpListener<'a>

§

impl<'a> Send for TcpListener<'a>

§

impl<'a> Sync for TcpListener<'a>

§

impl<'a> Unpin for TcpListener<'a>

§

impl<'a> UnsafeUnpin for TcpListener<'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>,

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.