Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Examples

The repository examples cover TCP, UDP, typed chains, JSON, TLS, lifecycle hooks, HTTP, and WebSocket.

TCP Echo

  • examples/tcp_echo_server.rs
  • examples/tcp_echo_client.rs

Run:

cargo run --example tcp_echo_server
cargo run --example tcp_echo_client

The server uses LineCodec and #[handler(Echo)]. The client sends two lines with write_and_flush.

Typed TCP Chain

  • examples/tcp_typed_chain.rs
  • examples/tcp_typed_chain_client.rs

Run:

cargo run --example tcp_typed_chain
cargo run --example tcp_typed_chain_client

Server pipeline:

#![allow(unused)]
fn main() {
pipeline()
    .codec(LineCodec::new())
    .inbound(Trim)
    .inbound(ParseRequest)
    .handler(Router)
    .outbound(RenderResponse)
}

This shows a full String -> Request -> Response -> String type chain.

JSON Over Line

  • examples/tcp_json_line_echo.rs

Run the server:

cargo run --example tcp_json_line_echo --features json

Run the client:

cargo run --example tcp_json_line_echo --features json -- client

This example separates framing from JSON. LineCodec handles line boundaries; JsonDecode<T> / JsonEncode<T> handle typed JSON.

TLS Echo

  • examples/tcp_tls_echo.rs

Run:

cargo run --example tcp_tls_echo --features tls

This example generates a localhost test certificate, attaches TLS with .tls(...), and keeps LineCodec as the application plaintext codec. The same TLS context APIs also support required or optional mTLS, ALPN advertisement, SNI-specific certificate identities, and TlsInfo metadata through TCP handler/stage contexts.

Lifecycle

  • examples/tcp_lifecycle.rs

Run:

cargo run --example tcp_lifecycle

This example implements Life and prints TCP server started/stopped and connection opened/closed events.

UDP Echo

  • examples/udp_echo_server.rs
  • examples/udp_echo_client.rs

Run:

cargo run --example udp_echo_server
cargo run --example udp_echo_client

It uses Utf8DatagramCodec and datagram_pipeline().

Typed UDP Chain

  • examples/udp_typed_chain.rs
  • examples/udp_typed_chain_client.rs

Run:

cargo run --example udp_typed_chain
cargo run --example udp_typed_chain_client

This demonstrates typed inbound/outbound transformations in a UDP datagram pipeline.

HTTP And WebSocket

  • examples/http_server.rs
  • examples/websocket_server.rs
  • examples/http_websocket_server.rs

Run:

cargo run --example http_server
cargo run --example websocket_server --features websocket
cargo run --example http_websocket_server --features websocket

http_server uses HttpCodec. websocket_server uses WebSocketCodec. http_websocket_server uses HttpWsCodec and HttpWsRouter to handle HTTP requests and WebSocket upgrades on the same port.