Skip to main content

ServerBuilder

Struct ServerBuilder 

Source
pub struct ServerBuilder<W = NoWork, G = NoGate, S = NoopSink, T = NoTls> { /* private fields */ }
Expand description

Builds a Server. A builder without a gate or without sessions/handler has no build method: forgetting either is a compile error.

§Examples

use net_io::{AllowAll, DriverKind, Limits, ListenerSpec, ServerBuilder};
use std::time::{Duration, Instant};

let server = ServerBuilder::new()
    .listener(ListenerSpec::tcp("0.0.0.0:8080").name("public"))
    .listener(ListenerSpec::tcp("127.0.0.1:9090").name("admin"))
    .driver(DriverKind::Blocking)
    .workers(64)
    .limits(Limits::builder().idle_timeout(Duration::from_secs(5)).build()?)
    .handler(|conn| { conn.send_all(b"hello\n")?; Ok(()) })
    .gate(AllowAll)
    .build()?;              // listeners bound, workers started, door closed
// ... drop privileges here ...
server.open_door()?;
let report = server.shutdown(Instant::now() + Duration::from_secs(30));
println!("{report:?}");

Implementations§

Source§

impl ServerBuilder

Source

pub fn new() -> ServerBuilder

An empty builder.

Source§

impl<W, G, S, T> ServerBuilder<W, G, S, T>

Source

pub fn trusted_proxies(self, proxies: TrustedProxies) -> Self

The proxies whose PROXY headers are believed, for every listener that requires one and has no list of its own.

Source

pub fn listener(self, spec: ListenerSpec) -> Self

Adds a listener (at most 64).

Source

pub fn driver(self, kind: DriverKind) -> Self

The driver (default Auto).

Source

pub fn placement(self, placement: Placement) -> Self

Pins workers to CPUs (Unsupported on macOS). A worker that stays on one CPU keeps its cache; the cost is that the OS can no longer move it off a busy CPU.

Source

pub fn workers(self, n: u16) -> Self

Worker threads (default 128 on the blocking driver; 1 to 4,096).

Source

pub fn limits(self, limits: Limits) -> Self

The server’s limits; unset values take the driver’s defaults.

Source

pub fn sessions<F: SessionFactory>( self, factory: F, ) -> ServerBuilder<Sessions<F>, G, S, T>

Runs a sans-IO session per connection.

Source

pub fn handler<H>( self, handler: H, ) -> ServerBuilder<BlockingHandler<H>, G, S, T>
where H: Fn(&mut TcpConnection) -> Result<(), NetError> + Send + Sync + 'static,

Runs a blocking handler per connection (on the blocking driver).

Source

pub fn tls<T2: TlsAcceptor>(self, acceptor: T2) -> ServerBuilder<W, G, S, T2>

The TLS engine for listeners with ListenerSpec::tls(true) (feature tls).

Source

pub fn gate<G2: Gate>(self, gate: G2) -> ServerBuilder<W, G2, S, T>

The connection gate (required; AllowAll for none).

Source

pub fn events<S2: EventSink>(self, sink: S2) -> ServerBuilder<W, G, S2, T>

The event sink (default NoopSink).

Source

pub fn control<C: ControlLine>(self, line: Arc<C>) -> Self

The control line (default: none).

Source

pub fn wipe_buffers(self, on: bool) -> Self

Zero the used part of every buffer when it returns to a pool (default on).

Source

pub fn clock<K: Clock>(self, clock: Arc<K>) -> Self

The clock deadlines read (tests inject a ManualClock).

Source§

impl<G, S, T> ServerBuilder<NoWork, G, S, T>

Source

pub fn udp_sessions<F>(self, factory: F) -> ServerBuilder<UdpOnly, G, S, T>

Available on crate feature udp only.

Serves UDP listeners only; a TCP listener then fails build. One thread per listener, and no worker pool.

use std::net::SocketAddr;
use net_io::{AllowAll, ListenerInfo, ListenerSpec, ServerBuilder, UdpContext, UdpOptions, UdpSession};
struct Echo;
impl UdpSession for Echo {
    fn on_datagram(&mut self, cx: &mut UdpContext<'_>, from: SocketAddr, bytes: &[u8]) {
        let _ = cx.send_to(bytes, from);
    }
}
let server = ServerBuilder::new()
    .listener(ListenerSpec::udp("0.0.0.0:7000", UdpOptions::new()))
    .udp_sessions(|_: &ListenerInfo| Echo)
    .gate(AllowAll)
    .build()?;
server.open_door()?;
Source§

impl<W: Workload, G, S, T> ServerBuilder<W, G, S, T>

Source

pub fn udp_sessions<F>(self, factory: F) -> Self

Available on crate feature udp only.

Serves the UDP listeners beside this server’s TCP work.

Source§

impl<W, G, S, T> ServerBuilder<W, G, S, T>
where W: Workload, G: Gate, S: EventSink, T: TlsAcceptor,

Source

pub fn build(self) -> Result<Server, NetError>

Validates everything, binds the listeners, starts the workers, and returns the server with the door closed.

§Errors

InvalidConfig (field names the method), bind errors, Unsupported for a driver this version does not have.

Trait Implementations§

Source§

impl Default for ServerBuilder

Source§

fn default() -> ServerBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<W = NoWork, G = NoGate, S = NoopSink, T = NoTls> !RefUnwindSafe for ServerBuilder<W, G, S, T>

§

impl<W = NoWork, G = NoGate, S = NoopSink, T = NoTls> !UnwindSafe for ServerBuilder<W, G, S, T>

§

impl<W, G, S, T> Freeze for ServerBuilder<W, G, S, T>
where W: Freeze, G: Freeze, S: Freeze, T: Freeze,

§

impl<W, G, S, T> Send for ServerBuilder<W, G, S, T>
where W: Send, G: Send, S: Send, T: Send,

§

impl<W, G, S, T> Sync for ServerBuilder<W, G, S, T>
where W: Sync, G: Sync, S: Sync, T: Sync,

§

impl<W, G, S, T> Unpin for ServerBuilder<W, G, S, T>
where W: Unpin, G: Unpin, S: Unpin, T: Unpin,

§

impl<W, G, S, T> UnsafeUnpin for ServerBuilder<W, G, S, T>

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

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

fn try_from(value: U) -> Result<T, !>

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.