TcpListener

Struct TcpListener 

Source
pub struct TcpListener { /* private fields */ }
Expand description

A TcpListener represents a io_uring based TCP listener socket to accept incoming connections on, this listener is capable of sharing a single socket address across multiple listeners, via the SO_REUSEPORT socket option.

There are two main ways of consuming the TcpListener. You can use the TcpListener::accept call which will return a single use Accept future, which when polled will return a single connection that is ready to use. Or you can use the TcpListener::incoming call which will return a [Stream] object in the form of an Incoming future which you can iterate over.

§Examples

let mut listener = TcpListener::new("[::]", 9092).expect("Failed to listen on specified address.");

let mut incoming = listener.incoming();
while let Some(connection) = incoming.next().await {
    let connection = connection.expect("Failed to accept connection.");
    // Do something with the connection.
}

Implementations§

Source§

impl TcpListener

Source

pub fn new(host: impl AsRef<str>, port: u16) -> Result<TcpListener>

Create a new TcpListener and listen on the specified address and port combination, using the default outstanding connections setting.

Source

pub fn with_outstanding( host: impl AsRef<str>, port: u16, outstanding: i32, ) -> Result<TcpListener>

Create a new TcpListener like TcpListener::new, however allow overriding the outstanding connection queue size.

Source

pub fn local_addr(&self) -> SocketAddr

Retrieve this sockets local SocketAddr, or panics if there is either no local address or some other std::io::Error is encountered.

For a safe alternative use TcpListener::try_local_addr.

Source

pub fn try_local_addr(&self) -> Result<SocketAddr>

Retrieve this sockets local SocketAddr or returns an error if there is either no local address for this socket or some other std::io::Error is encountered.

Source

pub fn accept(&mut self) -> Accept<'_, TcpListener>

Accept a single connection asynchronously, this will return an Accept future that when polled to completion will either return a valid [TcpStream] that is ready to use or an std::io::Error describing any errors that might have occured.

Source

pub fn incoming(&mut self) -> Incoming<'_, TcpListener>

Accept a stream of incoming connections, this will return an Incoming future [Stream] that when iterated on will return valid [TcpStream] objects or std::io::Error objects describing issues enountered.

Note that its best to call this outside of a loop body or conditional, as the future is meant to be reused.

Trait Implementations§

Source§

impl AsRawFd for TcpListener

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more

Auto Trait Implementations§

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 = Infallible

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.