connection.rs

Overview

This file provides core abstractions and functionality to manage network connections within a distributed system. It encapsulates connection metadata, roles, message delivery semantics, and supervises asynchronous sending and receiving of network messages over transport-layer connections. It interfaces with cryptographic identity verification and certificate handling, message encoding/decoding, and metrics reporting for network operations. The file primarily bridges the transport layer connections (NetConnection and NetTransport) with the higher-level publish-subscribe messaging system implemented via PubSub.


Enumerations

ConnectionRole

Defines the role of a network connection in the message exchange topology.

Variants:

Methods:

Usage example:

let role = ConnectionRole::Publisher;
if role.is_publisher() {
    // perform publisher-specific logic
}

Structures

ConnectionInfo

Holds metadata about a network connection, including identity, addressing, role, and cryptographic credentials.

Fields:

Methods:

Example usage:

let info = connection.info.clone();
if info.is_broadcast() {
    let mode = info.send_mode();
    // use mode for message sending logic
}

ConnectionWrapper<Connection: NetConnection>

Generic wrapper struct pairing a concrete network connection with its associated immutable connection metadata.

Fields:

Methods:

Parameters for new:

Example creation:

let wrapper = ConnectionWrapper::new(
    1,
    false,
    Some(remote_socket),
    remote_host_id_string,
    false,
    connection,
    ConnectionRole::Publisher,
)?;

Functions

connection_remote_host_id(connection: &impl NetConnection) -> String

Utility function returning the remote host identifier string from a given network connection.


connection_supervisor<Transport: NetTransport + 'static>

Asynchronous function supervising the lifecycle of a connection. It manages concurrent sending and receiving tasks, coordinates shutdown signals, and reports connection closure.

Parameters:

Behavior:

This function acts as the main event loop for handling a network connection, bridging transport layer logic with application-layer message handling.


trace_connection_task_result

Helper function to log results of sender or receiver tasks and update metrics if applicable.

Parameters:

Logs informational, error, or critical messages based on task outcome and increments error counters in metrics.


Message Structures

IncomingMessage

Represents a message received from the network, including metadata and timing information.

Fields:

Methods:

Parameter:

Example usage:

if let Some((msg, src_addr)) = incoming_message.finish::<MyMessageType>(&metrics) {
    // process the deserialized message
}

MessageDelivery

Enumeration describing how an outgoing message should be delivered.

Variants:


OutgoingMessage

Represents a message to be sent over the network, including delivery semantics and timing.

Fields:


Implementation Details and Algorithms


Interaction with Other Modules


Mermaid Diagram: Class Structure

classDiagram
class ConnectionRole {
+is_publisher()
+is_subscriber()
}
class ConnectionInfo {
+id: u64
+local_is_proxy: bool
+role: ConnectionRole
+remote_addr: SocketAddr
+remote_host_id: String
+remote_host_id_prefix: String
+remote_is_proxy: bool
+remote_cert_hash: CertHash
+remote_cert_pubkeys: Vec<VerifyingKey>
+remote_info()
+is_publisher()
+is_subscriber()
+is_broadcast()
+send_mode()
}
class ConnectionWrapper~Connection~ {
+info: Arc<ConnectionInfo>
+connection: Connection
+new()
+allow_sending()
}
class IncomingMessage {
+connection_info: Arc<ConnectionInfo>
+message: NetMessage
+duration_after_transfer: Instant
+finish()
}
class OutgoingMessage {
+delivery: MessageDelivery
+message: NetMessage
+duration_before_transfer: Instant
}
class MessageDelivery {
}
ConnectionWrapper --> ConnectionInfo : uses
IncomingMessage --> ConnectionInfo : references
OutgoingMessage --> MessageDelivery : uses
ConnectionInfo --> ConnectionRole : has a