How denoland/fastwebsockets works
Main components
a. Network I/O layer
• Tokio’s TcpStream or HTTP‐upgraded streams (hyper::upgrade::Upgraded).
• External Autobahn TestSuite server (for compliance testing).
• LLVM’s libFuzzer (for fuzz testing).
b. Handshake module (src/handshake.rs)
• Client‐side handshake generator/validator.
• Server‐side handshake (feature “upgrade”).
c. WebSocket core API (src/lib.rs, WebSocket<Stream>)
• Frame reading/writing (read_frame(), write_frame()).
• Configurables: set_writev, set_auto_close, set_auto_pong.
d. Frame processing sub‐modules
• frame.rs: frame structure (FIN, opcode, payload).
• mask.rs / unmask.rs: masking logic.
• fragment.rs: fragmentation definitions.
• FragmentCollector (in examples & docs) for coalescing frames.
e. HTTP upgrade integration (src/upgrade.rs)
• Adapter to/from Hyper & Axum routes.
f. Examples & Benchmarks
• Standalone example servers/clients (examples/*.rs).
• Bench scripts (benches/, autobahn/).
g. Testing & Fuzzing
• Unit & integration tests (tests/*.rs).
• UI tests (tests/ui).
• Fuzz targets (fuzz/fuzz_targets).
Relationships & data flow
• Raw TCP or HTTP-upgraded I/O → Handshake module → WebSocket core → Frame parser → (optional) FragmentCollector → Application logic → Frame writer → Network I/O.
• HTTP upgrade path: Hyper/Axum request → upgrade::upgrade() → produces a WebSocket future.
• Fuzzing harnesses feed arbitrary byte streams into parse_frame, unmask, parse_frame_fragment.
• Benchmarks measure performance of read_frame/write_frame under load.
• Autobahn TestSuite exercises end-to-end handshake + frame exchange.
Architectural patterns & design principles
– Modular decomposition: each protocol step in its own Rust module.
– Zero-copy or minimal‐copy data handling (mask/unmask, writev).
– Asynchronous I/O via Tokio & Hyper executors.
– Feature‐flagged extensions (e.g., “upgrade”, “with_axum”).
– Extensibility: raw frame parser vs high-level message collector.
– Strong testing: unit tests, fuzzing, compliance suite.