pub struct Server { /* private fields */ }Expand description
The listening server.
bind spawns a background thread that owns the control TcpListener
and accepts connections for the life of the process; dropping Server
does not stop that thread or close the control port — there is
currently no shutdown handshake for the control listener itself. Only
per-session data listeners are torn down early, and only through
Server::reap. This is a deliberate, narrower scope than “dropping it
stops accepting” would suggest; widening it is future work, not a claim
this code already makes good on.
Implementations§
Source§impl Server
impl Server
Sourcepub fn bind(port: u16) -> Result<Self>
pub fn bind(port: u16) -> Result<Self>
Binds the control port. Pass 0 to let the OS choose (tests do).
§Errors
Returns the OS error when the address cannot be bound.
Sourcepub fn control_port(&self) -> u16
pub fn control_port(&self) -> u16
The port clients send HELLO to.
Sourcepub fn events(&self) -> &Receiver<ServerEvent>
pub fn events(&self) -> &Receiver<ServerEvent>
Events for the UI to drain.
Sourcepub fn live_count(&self) -> usize
pub fn live_count(&self) -> usize
Sourcepub fn reap(&mut self, ttl: Duration)
pub fn reap(&mut self, ttl: Duration)
Drops sessions that have been detached longer than ttl, sending a
ServerEvent::Closed for each one on the same channel as every
other lifecycle event — the coherent way for a caller draining
Server::events to learn what to close, whether it comes from a
live connection or from reaping.
Each reaped session’s data-port listener thread is told to stop and
its listener is dropped, releasing the port: a session’s TCP port
does not outlive the session. Simply dropping a TcpListener while
another thread is blocked in accept() does not reliably wake that
thread (the behaviour is platform-dependent), so a shutdown flag is
set first and then a throwaway self-connect forces one more
iteration of the accept loop, which observes the flag and exits.
A connected session, or one that has reconnected since it last
detached, never expires: idle_since is None in both cases.
§Panics
If the internal session-map mutex is poisoned.
Sourcepub fn close_session(&mut self, id: SessionId)
pub fn close_session(&mut self, id: SessionId)
Tears a session down immediately, regardless of its idle state:
used when the UI window is closed by the user rather than by the
idle TTL. Reuses reap’s shutdown mechanism (shutdown flag plus a
self-connect to wake the blocked accept loop) so the session’s data
port and accept thread are actually released, not just forgotten —
see defect 2 in .superpowers/sdd/lifecycle-fixes-report.md for why
closing a window must tear the server-side session down rather than
leaving it live forever with no window to show it.
No event is sent: the caller (the UI) already knows it is closing this session and is not waiting to be told.
§Panics
If the internal session-map mutex is poisoned.