pub struct Server { /* private fields */ }Expand description
A running server. Dropping it stops it (with a drain), so keep it and call
Server::wait_for_stop to run until a stop is requested.
Implementations§
Source§impl Server
impl Server
Sourcepub fn open_door(&self) -> Result<(), NetError>
pub fn open_door(&self) -> Result<(), NetError>
Starts accepting on every listener; a second call is a no-op.
§Errors
ShuttingDown while draining or after shutdown; ForkedChild.
Sourcepub fn close_door(&self)
pub fn close_door(&self)
Stops accepting within one loop iteration; listening sockets stay open. Idempotent.
Sourcepub fn drain(&self, deadline: Instant) -> DrainReport
pub fn drain(&self, deadline: Instant) -> DrainReport
Closes the door, tells sessions to finish, closes idle connections at once,
waits for the rest until deadline, then kills them with DrainDeadline.
Sourcepub fn shutdown(self, deadline: Instant) -> DrainReport
pub fn shutdown(self, deadline: Instant) -> DrainReport
Drains, then closes the listening sockets and joins every thread.
Sourcepub fn wait_for_stop(self) -> DrainReport
pub fn wait_for_stop(self) -> DrainReport
Blocks until a stop is requested (ServerHandle::request_stop, or the
control line moves to Draining or Stopping), then shuts down.
Sourcepub fn adopt(
&self,
conn: TcpConnection,
spec: AdoptSpec,
) -> Result<ConnectionId, NetError>
pub fn adopt( &self, conn: TcpConnection, spec: AdoptSpec, ) -> Result<ConnectionId, NetError>
Registers a dialed connection exactly like an accepted one on the
listener named in spec: a new id, the caps, Gate::admit with
AdmitStage::Adopt, that listener’s limits, the server’s handler or
sessions, its kill handle, and its events. For back-channels and outbound
connections a server should run and revoke like its own. A gate the
connection had from set_gate is closed (SessionClosed) first.
§Errors
Refused { cause } (caps, the gate, Busy; the connection was reset),
InvalidConfig { field: "listener" } (no listener with that name),
InvalidConfig { field: "connection" } (a TLS connection, or bytes read
ahead), ShuttingDown, ForkedChild, Closed.
use net_io::{AdoptSpec, AllowAll, ListenerSpec, ServerBuilder};
let server = ServerBuilder::new()
.listener(ListenerSpec::tcp("127.0.0.1:7000").name("main"))
.handler(|conn| { conn.send_all(b"hello, upstream\n")?; Ok(()) })
.gate(AllowAll)
.build()?;
let upstream = net_io::connect("10.0.0.9:9000")?;
let id = server.adopt(upstream, AdoptSpec::new("main"))?;Sourcepub fn release_listeners(&self) -> Result<Vec<ListenerHandle>, NetError>
pub fn release_listeners(&self) -> Result<Vec<ListenerHandle>, NetError>
Owned duplicates of every listening socket, for a hand-off to the next
process (send_listeners, Unix). This server keeps
accepting meanwhile, so the new process can start before this one stops
(decision D-042); a later shutdown closes only this server’s copies, and
a Unix socket file stays in place for the new process.
§Errors
Os when a socket cannot be duplicated, ForkedChild.
Sourcepub fn add_listener(
&mut self,
spec: ListenerSpec,
) -> Result<ListenerId, NetError>
pub fn add_listener( &mut self, spec: ListenerSpec, ) -> Result<ListenerId, NetError>
Binds one more listener on the running server and starts its accept
thread. Its door is closed until the next Server::open_door (which
opens every listener’s door); the same checks as build apply. The id is
new: ids of removed listeners are never reused.
§Errors
As build for the listener; InvalidConfig { field: "listener" } past 64
listeners, ShuttingDown, ForkedChild.
use net_io::{AllowAll, ListenerSpec, ServerBuilder};
let mut server = ServerBuilder::new()
.listener(ListenerSpec::tcp("127.0.0.1:8080"))
.handler(|conn| { conn.send_all(b"hi\n")?; Ok(()) })
.gate(AllowAll)
.build()?;
server.open_door()?;
let added = server.add_listener(ListenerSpec::tcp("127.0.0.1:8081").name("extra"))?;
server.open_door()?; // opens the new listener's doorSourcepub fn remove_listener(
&mut self,
id: ListenerId,
deadline: Instant,
) -> Result<DrainReport, NetError>
pub fn remove_listener( &mut self, id: ListenerId, deadline: Instant, ) -> Result<DrainReport, NetError>
Closes one listener’s socket at once (its connections keep running),
then drains them: idle ones close now, the rest may finish until
deadline and are then killed (DrainDeadline). Other listeners and
their connections are not touched.
§Errors
InvalidConfig { field: "listener", reason: Missing } for an unknown id,
ForkedChild.
Sourcepub fn handle(&self) -> ServerHandle
pub fn handle(&self) -> ServerHandle
A cloneable handle for other threads.
Sourcepub fn listeners(&self) -> &[ListenerInfo]
pub fn listeners(&self) -> &[ListenerInfo]
Facts about each listener.