transport_layer.rs

Overview

This file implements a network transport layer abstraction tailored for a gossip-based messaging system. It encapsulates the creation, management, and communication over network connections using a pluggable transport mechanism that conforms to the NetTransport trait. The transport layer supports asynchronous sending and receiving of serialized gossip messages (ChitchatMessage) over connections established with remote peers. It manages both incoming and outgoing connections, handles connection lifecycle events, and provides message queuing with backpressure and message expiration policies.

Key responsibilities include:

Types and Structures

TransportLayerTransport

A generic wrapper around a NetTransport implementation combined with a credential (NetCredential). This struct acts as the entry point to open sockets bound to a local address.

TransportLayerSocket<T>

Represents an open socket bound to a local address, managing connection state and message flow.

OutgoingConnection

Represents a single outgoing connection to a remote peer.

Asynchronous Functions

handle_socket_listener

async fn handle_socket_listener<Transport: NetTransport + 'static>(
    transport: Transport,
    listen_addr: SocketAddr,
    cred: NetCredential,
    incoming_tx: Sender<(SocketAddr, Instant, ChitchatMessage)>,
    mut stop_rx: tokio::sync::watch::Receiver<bool>,
) -> anyhow::Result<()>

handle_incoming_connection

async fn handle_incoming_connection<IncomingRequest: NetIncomingRequest + 'static>(
    connection_request: IncomingRequest,
    incoming_tx: Sender<(SocketAddr, Instant, ChitchatMessage)>,
) -> anyhow::Result<()>

handle_outgoing_connection

async fn handle_outgoing_connection<Connection: NetConnection + 'static>(
    messages_rx: Receiver<ChitchatMessage>,
    connection: Connection,
)

Important Implementation Details

Interaction with Other Components

Flow Diagram

flowchart TD
A[TransportLayerTransport] -->|open()| B[TransportLayerSocket]
B --> C{Outgoing Connections}
C -->|get_or_create_outgoing_connection| D[OutgoingConnection]
D -->|spawn| E[handle_outgoing_connection Task]
B --> F[incoming_message_rx Channel]
G[handle_socket_listener Task] -->|accept connections| H[handle_incoming_connection Task]
H -->|send messages| F
B -->|send()| D
B -->|recv()| F
F -->|recv()| B

Detailed Method Descriptions

TransportLayerTransport::new

TransportLayerTransport::open

TransportLayerSocket::new

TransportLayerSocket::send

TransportLayerSocket::recv

TransportLayerSocket::get_or_create_outgoing_connection

handle_socket_listener

handle_incoming_connection

handle_outgoing_connection


For details on the NetTransport, NetConnection, NetIncomingRequest traits and the ChitchatMessage type, see the [Transport Abstraction](/transport-abstraction) and [Message Serialization](/message-serialization) topics. For usage patterns of async channels and Tokio, see [Asynchronous Programming](/async-programming).

This file forms the core network interaction layer for the gossip protocol implementation by managing connection lifecycles and message delivery.

Diagram Legend