1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// =============================================================================
// WSE Rust WebSocket server: tokio + tungstenite + PyO3.
//
// Key optimizations:
// - send_event(): full outbound pipeline in Rust (envelope+serialize+compress+send)
// - Drain mode: Python polls Rust for batched inbound events (1 GIL per batch)
// - Ping/pong handled entirely in Rust (zero Python round-trips)
// - Write coalescing: feed() + batch try_recv() + single flush()
// - TCP_NODELAY on accept
// =============================================================================
//! The WebSocket server.
//!
//! # Module map
//!
//! Engine, plain Rust (the part a native `wse-rs` library would keep):
//!
//! - `frames`: WebSocket frame types, pre-framed encoding, the per-connection
//! broadcast queue and its vectored writer.
//! - `state`: what every task shares: connection handles, queue groups, inbound
//! events, server commands and `SharedState`.
//! - `messages`: protocol messages the server builds itself (server_hello,
//! server_ready, errors, presence events) and the category helpers.
//! - `handshake`: WebSocket upgrade helpers: the auth rejection, the
//! pre-handshake cap and its guard.
//! - `e2e`: end-to-end encryption (ECDH P-256 key exchange, AES-GCM-256).
//! - `connection`: one client connection, from the upgrade to the cleanup.
//! - `fanout`: topic glob matching, the topic index, delivery on every core
//! (one lane per core, each with its own task), recovery stamping.
//! - `signing`: signed messages: client keys, the server's signing key, the
//! checks a client's signed message passes.
//! - `commands`: the command processor that executes `ServerCommand`s.
//! - `handler`: what the application hears of its clients (`Handler`).
//!
//! Python layer, PyO3:
//!
//! - `py`: the `RustWSEServer` class. `py/mod.rs` holds the class, its
//! constructor parameters and the single `#[pymethods]` block, so every
//! Python-visible signature, default and docstring is in one place. Apart
//! from the constructor and the short accessors, each method body is an
//! `*_impl` helper in a topic file: `py/lifecycle.rs`, `messaging.rs`,
//! `inbound.rs`, `subscriptions.rs`, `presence.rs`, `cluster.rs` and
//! `metrics.rs`; `convert.rs` converts between Python objects and JSON,
//! `callbacks.rs` fires the callback-mode callbacks, `handler.rs` is the
//! `Handler` the Python server runs its connections with.
//!
//! The crate-visible items are re-exported below, so `server::X` paths stay as
//! they were. Sibling modules import them through these re-exports and import
//! module-internal helpers (`pub(super)`) from their own module.
//!
//! # Engine vs Python layer
//!
//! The engine has no PyO3: what the application hears of its connections
//! goes through `handler::Handler`, called on each connection's task in
//! order (connect, messages, signed messages, disconnect). `py::handler`
//! implements it for Python: drain mode queues the events for
//! `drain_inbound()`, callback mode runs the callbacks one after another for
//! each connection on the blocking pool.
//!
//! Engine code that still lives in the Python layer:
//!
//! - `py::lifecycle::start_impl` builds the runtime: the accept loop with
//! cluster protocol detection, the recovery cleanup, the presence TTL sweep
//! and the ping / zombie / slow-consumer task. `start_impl` and `stop_impl`
//! take the lifecycle lock through the Python-aware `crate::pylock::lock`.
//! - `py::messaging::send_event_impl` holds the outbound pipeline (dedup, rate
//! limit, envelope, msgpack / zlib / E2E) next to the `PyDict` conversion.
//! - `py::subscriptions` and `py::presence` hold subscribe / unsubscribe, queue
//! groups, cluster interest refcounts, recovery replay and presence tracking.
//! - `RustWSEServer::build` (`py/mod.rs`) validates the JWT keys and builds the
//! `SharedState` configuration, reporting errors as Python exceptions.
pub
pub
pub
pub
pub
pub
pub
pub
pub
pub use RustWSEServer;
pub use ;
pub use ;
pub use ;
pub use E2E_KEY_INFO;
pub use ;
pub use ;
pub use Server;
pub use ;
pub use normalize_origin;
pub use format_presence_msg;
pub use Health;
pub use ;
pub use ;
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------