Struct pavex::server::IncomingStream

source ·
pub struct IncomingStream { /* private fields */ }
Expand description

A stream of incoming connections.

IncomingStream::bind is the primary entrypoint for constructing a new IncomingStream.

Incoming connections will be usually passed to a Server instance to be handled. Check out Server::bind or Server::listen for more information.

Implementations§

source§

impl IncomingStream

source

pub async fn bind(addr: SocketAddr) -> Result<Self>

Create a new IncomingStream by binding to a socket address.
The socket will be configured to be non-blocking and reuse the address.

§Example
use std::net::SocketAddr;
use pavex::server::{IncomingStream, Server};

let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
let incoming = IncomingStream::bind(addr).await?;
§Custom configuration

If you want to customize the options set on the socket, you can build your own TcpListener using socket2::Socket and then convert it into an IncomingStream via TryFrom::try_from.
There’s only one option you can’t change: the socket will always be set to non-blocking mode.

use std::net::SocketAddr;
use socket2::Domain;
use pavex::server::{IncomingStream, Server};

let addr = SocketAddr::from(([127, 0, 0, 1], 8080));

let socket = socket2::Socket::new(
   Domain::for_address(addr),
   socket2::Type::STREAM,
   Some(socket2::Protocol::TCP),
)
.expect("Failed to create a socket");
socket.set_reuse_address(true)?;
socket.set_nonblocking(true)?;
socket.bind(&addr.into())?;
// We customize the backlog setting!
socket.listen(2048_i32)?;

let listener = std::net::TcpListener::from(socket);
let incoming: IncomingStream = listener.try_into()?;
source

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

Returns the address that this IncomingStream is bound to.

source

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

Accepts a new incoming connection from the underlying 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.

§Example
use pavex::server::IncomingStream;
use std::net::SocketAddr;

let address = SocketAddr::from(([127, 0, 0, 1], 8080));
let incoming = IncomingStream::bind(address).await?;

match incoming.accept().await {
    Ok((_socket, addr)) => println!("new client: {:?}", addr),
    Err(e) => println!("couldn't get client: {:?}", e),
}

Trait Implementations§

source§

impl From<TcpListener> for IncomingStream

source§

fn from(v: TcpListener) -> Self

Converts to this type from the input type.
source§

impl TryFrom<TcpListener> for IncomingStream

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(v: TcpListener) -> Result<Self>

Performs the conversion.

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more