TcpListener

Trait TcpListener 

Source
pub trait TcpListener:
    TryFrom<TcpListener, Error = Error>
    + Fd
    + Unpin
    + Send
    + Sync
    + 'static {
    type Runtime: RuntimeLite;
    type Stream: TcpStream<Runtime = Self::Runtime>;
    type Incoming<'a>: Stream<Item = Result<Self::Stream>> + Send + Sync + Unpin + 'a;

    // Required methods
    fn bind<A: ToSocketAddrs<Self::Runtime>>(
        addr: A,
    ) -> impl Future<Output = Result<Self>> + Send
       where Self: Sized;
    fn accept(
        &self,
    ) -> impl Future<Output = Result<(Self::Stream, SocketAddr)>> + Send;
    fn incoming(&self) -> Self::Incoming<'_>;
    fn into_incoming(self) -> impl Stream<Item = Result<Self::Stream>> + Send;
    fn local_addr(&self) -> Result<SocketAddr>;
    fn set_ttl(&self, ttl: u32) -> Result<()>;
    fn ttl(&self) -> Result<u32>;

    // Provided method
    fn try_clone(&self) -> Result<Self> { ... }
}
Expand description

An abstraction layer for TCP listener.

Required Associated Types§

Source

type Runtime: RuntimeLite

The async runtime.

Source

type Stream: TcpStream<Runtime = Self::Runtime>

Stream of incoming connections.

Source

type Incoming<'a>: Stream<Item = Result<Self::Stream>> + Send + Sync + Unpin + 'a

A stream of incoming TCP connections.

This stream is infinite, i.e awaiting the next connection will never result in None. It is created by the TcpListener::incoming() method.

Required Methods§

Source

fn bind<A: ToSocketAddrs<Self::Runtime>>( addr: A, ) -> impl Future<Output = Result<Self>> + Send
where Self: Sized,

Creates a new TcpListener, which will be bound to the specified address.

The returned listener is ready for accepting connections.

Binding with a port number of 0 will request that the OS assigns a port to this listener. The port allocated can be queried via the local_addr method.

The address type can be any implementor of the ToSocketAddrs trait. If addr yields multiple addresses, bind will be attempted with each of the addresses until one succeeds and returns the listener. If none of the addresses succeed in creating a listener, the error returned from the last attempt (the last address) is returned.

This function sets the SO_REUSEADDR option on the socket.

Source

fn accept( &self, ) -> impl Future<Output = Result<(Self::Stream, SocketAddr)>> + Send

Accepts a new incoming connection from this listener.

This function will yield once a new TCP connection is established. When established, the corresponding TcpStream and the remote peer’s address will be returned.

Source

fn incoming(&self) -> Self::Incoming<'_>

Returns a stream of incoming connections.

Iterating over this stream is equivalent to calling accept() in a loop. The stream of connections is infinite, i.e awaiting the next connection will never result in None.

See also TcpListener::into_incoming.

Source

fn into_incoming(self) -> impl Stream<Item = Result<Self::Stream>> + Send

Turn this into a stream over the connections being received on this listener.

The returned stream is infinite and will also not yield the peer’s SocketAddr structure. Iterating over it is equivalent to calling TcpListener::accept in a loop.

See also TcpListener::incoming.

Source

fn local_addr(&self) -> Result<SocketAddr>

Returns the local address that this listener is bound to.

This can be useful, for example, when binding to port 0 to figure out which port was actually bound.

Source

fn set_ttl(&self, ttl: u32) -> Result<()>

Sets the time-to-live value for this socket.

Source

fn ttl(&self) -> Result<u32>

Gets the time-to-live value of this socket.

Provided Methods§

Source

fn try_clone(&self) -> Result<Self>

Creates a new independently owned handle to the underlying socket.

The returned UdpSocket is a reference to the same socket that this object references. Both handles will read and write the same port, and options set on one socket will be propagated to the other.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§