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<'_>
impl TcpListener<'_>
Sourcepub fn listen(
&mut self,
local: impl Into<ListenSocketAddr>,
) -> Result<(), ListenError>
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.
Sourcepub fn bind_to_iface(
&mut self,
iface: Option<IfaceHandle>,
) -> Result<(), ListenError>
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.
Sourcepub fn bound_iface(&self) -> Option<IfaceHandle>
pub fn bound_iface(&self) -> Option<IfaceHandle>
Return the interface the listener is bound to, or None.
See bind_to_iface.
Sourcepub fn close(&mut self)
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.
Sourcepub fn local_addr(&self) -> ListenSocketAddr
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.
Sourcepub fn register_accept_waker(&mut self, waker: &Waker)
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
accepthas changed.
Sourcepub fn can_accept(&self) -> bool
pub fn can_accept(&self) -> bool
Whether a connection attempt is waiting to be accepted.
Sourcepub fn accept(&mut self) -> Option<AcceptToken>
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.