pub struct SharedWebSocketServer { /* private fields */ }Expand description
Cheap, cloneable handle to a WebSocketServer’s per-connection
configuration, produced by WebSocketServer::into_shared.
into_shared builds the connection configuration (router, hooks,
capacities) exactly once; cloning a SharedWebSocketServer is an
Arc clone. The handle is Send + Sync + 'static, so it drops
straight into a per-connection
tokio::spawn(async move { shared.serve_connection(ws).await }) —
the shape an embedder needs to share one TCP port between REPE
WebSocket upgrades and its own HTTP routes.
Implementations§
Sourcepub async fn accept(
&self,
stream: TcpStream,
path: &str,
) -> Result<WebSocketStream<TcpStream>, RepeError>
pub async fn accept( &self, stream: TcpStream, path: &str, ) -> Result<WebSocketStream<TcpStream>, RepeError>
Perform the REPE WebSocket handshake using this server’s configured
WebSocketLimits.
Prefer this over the associated WebSocketServer::accept in an
embedder-owned accept loop. That one takes no self, so it cannot see a
builder-set with_limits and accepts
with the defaults instead: the limits would appear to be configured and
silently not be.
Sourcepub async fn accept_with_handshake(
&self,
stream: TcpStream,
path: &str,
) -> Result<(WebSocketStream<TcpStream>, HandshakeContext), RepeError>
pub async fn accept_with_handshake( &self, stream: TcpStream, path: &str, ) -> Result<(WebSocketStream<TcpStream>, HandshakeContext), RepeError>
accept, also returning the upgrade’s
HandshakeContext.
Sourcepub fn limits(&self) -> WebSocketLimits
pub fn limits(&self) -> WebSocketLimits
This server’s size limits, for an embedder that upgrades the stream through some path of its own and needs to configure it to match.
Sourcepub async fn adopt_upgraded<S>(&self, io: S) -> WebSocketStream<S>
pub async fn adopt_upgraded<S>(&self, io: S) -> WebSocketStream<S>
Wrap the raw IO of a connection another HTTP stack already
upgraded into a server-role WebSocketStream carrying this
server’s configured WebSocketLimits,
ready for serve_connection.
This is the seam for an embedder whose HTTP routes live in a
framework (axum, hyper, tower) rather than in a hand-written
accept loop. Such an embedder already owns a Router with its own
paths on it and wants the REPE endpoint to be one more route,
answering the 101 through the framework’s own upgrade machinery.
The peek-then-fork recipe (is_websocket_upgrade +
accept) cannot serve that shape, because it
requires repe to own the TcpStream before any HTTP is parsed.
Here the framework keeps the socket, performs the handshake, and
hands back the upgraded byte stream — which is what io is.
The caller is responsible for the handshake itself: validating
Upgrade/Connection/Sec-WebSocket-Version and answering 101
with the derive_accept_key of the client’s Sec-WebSocket-Key.
Frameworks that expose the upgrade as an extractor do that part;
derive_accept_key is provided for those that hand back the raw
request. Path validation is the caller’s too — the framework
already routed the request here, which is the counterpart to the
path argument WebSocketServer::accept takes.
Nothing is read from io before the first REPE frame, so a stream
whose handshake produced garbage surfaces as a protocol error on
that connection alone. The other failure shape is quieter: if the
client is still waiting for a 101 that never came, neither side
speaks, and repe applies no read timeout or idle deadline — the
framework’s own upgrade timeout is what bounds that.
To name the returned type in your own signatures, the transport
crate is re-exported as repe::tokio_tungstenite; call sites that
pass the value straight to
serve_connection never need to.
This call is where the connection’s inbound thresholds are fixed —
they cannot be changed afterwards — which is precisely why it
exists rather than leaving embedders to call the transport’s
from_raw_socket themselves and silently substitute its defaults
for the limits the server was built with. The outbound guard
travels with the server config and applies on every path.
Sourcepub async fn adopt_upgraded_partially_read<S>(
&self,
io: S,
buffered: Vec<u8>,
) -> WebSocketStream<S>
pub async fn adopt_upgraded_partially_read<S>( &self, io: S, buffered: Vec<u8>, ) -> WebSocketStream<S>
adopt_upgraded for a framework that hands
back the bytes it read past the upgrade request separately from the
stream, as (io, buffered).
A client may legally pipeline its first frame with the upgrade
request, so those bytes can be real REPE traffic. hyper hides this
— its Upgraded replays what it buffered — but a framework that
returns the two halves separately needs somewhere to put them, and
dropping them loses a frame. Pass them here and they are decoded
ahead of anything read from io.
Prefer adopt_upgraded when the stream
already replays its own leftovers; passing buffered bytes that
io will also yield would double-decode them.
Sourcepub async fn serve_connection<S>(
&self,
ws: WebSocketStream<S>,
) -> Result<(), RepeError>
pub async fn serve_connection<S>( &self, ws: WebSocketStream<S>, ) -> Result<(), RepeError>
Run one connection’s reader/writer loop using this server’s
router, hooks, and capacities. Pair with
WebSocketServer::accept to serve a stream the embedder
accepted and upgraded itself.
Generic over the underlying byte stream, so the connection need
not have come from a TcpStream this crate accepted. A stream
some other HTTP stack upgraded — an axum/hyper route that
answered the 101 itself — is served by wrapping its upgraded IO
with adopt_upgraded and passing the
result here.
Connect/disconnect hooks (and any PeerRegistry attached via
WebSocketServer::with_peer_registry) fire for connections
served this way, exactly as under WebSocketServer::serve.
The connection’s off-reader handlers see their cancellation signal
(CallContext::is_cancelled /
CallContext::cancelled) fire on
disconnect. To also fire it on an embedder-driven shutdown, use
serve_connection_with_cancel.
Sourcepub async fn serve_connection_with_handshake<S>(
&self,
ws: WebSocketStream<S>,
handshake: HandshakeContext,
) -> Result<(), RepeError>
pub async fn serve_connection_with_handshake<S>( &self, ws: WebSocketStream<S>, handshake: HandshakeContext, ) -> Result<(), RepeError>
Like serve_connection, but threads the
HandshakeContext from WebSocketServer::accept_with_handshake
through so
on_peer_connect_with_handshake
hooks fire for this connection. For a one-port co-hosting embedder
that keys peers off the upgrade request.
Sourcepub async fn serve_connection_with_cancel<S>(
&self,
ws: WebSocketStream<S>,
shutdown: &ShutdownToken,
) -> Result<(), RepeError>
pub async fn serve_connection_with_cancel<S>( &self, ws: WebSocketStream<S>, shutdown: &ShutdownToken, ) -> Result<(), RepeError>
Like serve_connection, but ties this
connection’s cancellation signal to a shared ShutdownToken as
well as to disconnect.
For a one-port co-hosting embedder that owns its accept loop:
create one ShutdownToken, serve each connection through this
method, and call ShutdownToken::cancel on shutdown to wake
every connection’s in-flight off-reader handlers at once while you
drain your own task set. The connection’s token is a child of the
shared one, so cancelling the shared token cancels every
connection, and a single connection’s disconnect cancels only its
own child.
Sourcepub async fn serve_connection_with_cancel_and_handshake<S>(
&self,
ws: WebSocketStream<S>,
handshake: HandshakeContext,
shutdown: &ShutdownToken,
) -> Result<(), RepeError>
pub async fn serve_connection_with_cancel_and_handshake<S>( &self, ws: WebSocketStream<S>, handshake: HandshakeContext, shutdown: &ShutdownToken, ) -> Result<(), RepeError>
serve_connection_with_cancel
plus the captured HandshakeContext — the union of co-hosting,
embedder-driven drain, and handshake-keyed peers. Cancelling the
shared ShutdownToken winds the connection’s in-flight off-reader
handlers down, and
on_peer_connect_with_handshake
hooks fire from the captured handshake.
Trait Implementations§
Source§fn clone(&self) -> SharedWebSocketServer
fn clone(&self) -> SharedWebSocketServer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more