server.rs

Overview

This file implements a lightweight server module named LiteServer designed for network communication using Microsoft QUIC (MSQUIC) transport. It handles incoming network requests, manages connections, and broadcasts messages efficiently to multiple clients. The server utilizes asynchronous Rust features with Tokio for concurrency, providing a scalable and responsive network service. It is primarily responsible for:

The server is built around the MSQUIC transport abstraction and integrates with other components such as MsQuicNetIncomingRequest, MsQuicTransport, and message receivers to facilitate bidirectional communication.


Detailed Components

Struct: LiteServer

Purpose

Represents a lightweight network server bound to a specific socket address.

Fields

Methods

new(bind: SocketAddr) -> Self

Constructs a new LiteServer instance bound to the given socket address.

Parameters:

Returns:

Example:

let server = LiteServer::new("127.0.0.1:8080".parse().unwrap());

start<TBPResolver, A>(self, raw_block_receiver: InstrumentedReceiver<(A, Vec<u8>)>, bp_resolver: TBPResolver) -> anyhow::Result<()>

Starts the server asynchronously, launching all necessary background tasks:

Type Parameters:

Parameters:

Returns:

Usage:

server.start(raw_block_receiver, |addr| Some(addr.to_string())).await?;

Implementation Details:


Async Functions

listener_handler(bind: SocketAddr, incoming_request_tx: UnboundedSender<MsQuicNetIncomingRequest>) -> anyhow::Result<()>

Handles the listening socket lifecycle:

Parameters:

Returns:

Behavior:


incoming_requests_handler(incoming_request_rx: UnboundedReceiver<MsQuicNetIncomingRequest>, outgoing_message_tx: broadcast::Sender<Arc<Vec<u8>>>) -> anyhow::Result<()>

Processes incoming requests received from the listener:

Parameters:

Returns:


connection_supervisor(incoming_request: MsQuicNetIncomingRequest, outgoing_message_rx: broadcast::Receiver<Arc<Vec<u8>>>)

Supervises an individual connection lifecycle:

Parameters:


connection_handler(incoming_request: MsQuicNetIncomingRequest, outgoing_message_rx: broadcast::Receiver<Arc<Vec<u8>>>) -> anyhow::Result<()>

Manages the communication for an individual client connection:

Parameters:

Returns:


message_multiplexor_handler<TBKAddrResolver, A>(incoming_message_rx: InstrumentedReceiver<(A, Vec<u8>)>, outgoing_message_tx: broadcast::Sender<Arc<Vec<u8>>>, bp_resolver: TBKAddrResolver) -> anyhow::Result<()>

Serializes and forwards raw incoming messages to the broadcast channel:

Type Parameters:

Parameters:

Returns:


Important Implementation Details and Algorithms


Interactions with Other Parts of the System


Visual Diagram

classDiagram
class LiteServer {
+bind: SocketAddr
+new()
+start()
}
LiteServer o-- "3" Task : spawns
class listener_handler {
<<async function>>
}
class incoming_requests_handler {
<<async function>>
}
class message_multiplexor_handler {
<<blocking function>>
}
LiteServer --> listener_handler : listener_task
LiteServer --> incoming_requests_handler : incoming_requests_task
LiteServer --> message_multiplexor_handler : multiplexer_task
listener_handler --> MsQuicTransport
listener_handler --> NetCredential
incoming_requests_handler --> connection_supervisor : spawns
connection_supervisor --> connection_handler : calls async
connection_handler --> MsQuicNetIncomingRequest
connection_handler --> broadcast::Receiver
message_multiplexor_handler --> InstrumentedReceiver
message_multiplexor_handler --> broadcast::Sender

This diagram illustrates the main structural components of the file and the key asynchronous tasks spawned by LiteServer. It also shows the interaction between connection handlers and the message multiplexor.