TcpListener

Struct TcpListener 

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

A TCP socket listener.

This works much like the TcpListener in the standard library, but this doesn’t block when calling accept and instead returns a WouldBlock error.

§Deregistering

TcpListener will deregister itself when dropped, iff it is not cloned (via try_clone).

§Examples

use std::time::Duration;

use mio_st::event::{Events, EventedId};
use mio_st::net::TcpListener;
use mio_st::poll::{Poller, PollOption};

let address = "127.0.0.1:8001".parse()?;
let mut listener = TcpListener::bind(address)?;

let mut poller = Poller::new()?;
let mut events = Events::new();

// Register the socket with `Poller`
poller.register(&mut listener, EventedId(0), TcpListener::INTERESTS, PollOption::Edge)?;

poller.poll(&mut events, Some(Duration::from_millis(100)))?;

// There may be a socket ready to be accepted.

Implementations§

Source§

impl TcpListener

Source

pub const INTERESTS: Interests = Interests::READABLE

The interests to use when registering to receive acceptable connections events.

Source

pub fn bind(address: SocketAddr) -> Result<TcpListener>

Convenience method to bind a new TCP listener to the specified address to receive new connections.

This also sets the SO_REUSEPORT and SO_REUSEADDR options on the socket.

Source

pub fn try_clone(&self) -> Result<TcpListener>

Create a independently owned handle to the underlying socket.

The returned TcpListener is a reference to the same socket as self. Both handles can be used to accept incoming connections and options set on one listener will affect the other.

§Notes

On Linux when a TcpListener is cloned it must deregistered. If its not deregistered explicitly and one listener is closed (dropped) and onother is still open the poller will still receive events.

Source

pub fn accept(&mut self) -> Result<(TcpStream, SocketAddr)>

Accepts a new TcpStream.

This may return an WouldBlock error, this means a stream may be ready at a later point and one should wait for a notification before calling accept again.

If an accepted stream is returned, the remote address of the peer is returned along with it.

Source

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

Returns the local socket address of this listener.

Source

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

Sets the value for the IP_TTL option on this socket.

Source

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

Gets the value of the IP_TTL option for this socket.

Source

pub fn take_error(&mut self) -> Result<Option<Error>>

Get the value of the SO_ERROR option on this socket.

This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.

Trait Implementations§

Source§

impl AsRawFd for TcpListener

Available on Unix only.
Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Debug for TcpListener

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Evented for TcpListener

Source§

fn register( &mut self, poller: &mut Poller, id: EventedId, interests: Interests, opt: PollOption, ) -> Result<()>

Register self with the given Poller instance. Read more
Source§

fn reregister( &mut self, poller: &mut Poller, id: EventedId, interests: Interests, opt: PollOption, ) -> Result<()>

Reregister self with the given Poller instance. Read more
Source§

fn deregister(&mut self, poller: &mut Poller) -> Result<()>

Deregister self from the given Poller instance Read more
Source§

impl FromRawFd for TcpListener

Available on Unix only.
Source§

unsafe fn from_raw_fd(fd: RawFd) -> TcpListener

The caller must ensure that the listener is in non-blocking mode when using this function.

Source§

impl IntoRawFd for TcpListener

Available on Unix only.
Source§

fn into_raw_fd(self) -> RawFd

Consumes this object, returning the raw underlying 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.