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>
impl<'d> TcpListener<'d>
Sourcepub fn new(stack: Stack<'d>) -> Result<Self, Full>
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:
Fullif the stack has no room for another TCP listener. The limit is set by thetcp-listener-count-Nfeature ofxarxa.
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 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.
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 listen<T>(&mut self, local_endpoint: T) -> Result<(), ListenError>where
T: Into<IpListenEndpoint>,
pub fn listen<T>(&mut self, local_endpoint: T) -> Result<(), ListenError>where
T: Into<IpListenEndpoint>,
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.
Sourcepub fn close(&mut self)
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.
Sourcepub fn local_endpoint(&self) -> IpListenEndpoint
pub fn local_endpoint(&self) -> IpListenEndpoint
Get the endpoint the listener is listening on.
A zero port means the listener is closed.
Sourcepub fn can_accept(&self) -> bool
pub fn can_accept(&self) -> bool
Get whether a connection attempt is waiting to be accepted.
Sourcepub fn wait_accept_ready(&self) -> impl Future<Output = ()> + '_
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.
Sourcepub async fn accept(&mut self) -> Result<AcceptToken, AcceptError>
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:
InvalidStateif the listener is not listening.
Trait Implementations§
Source§impl Drop for TcpListener<'_>
Available on crate feature tcp-listener only.
impl Drop for TcpListener<'_>
tcp-listener only.