channel.rs

Overview

This file implements an in-memory, message-passing transport layer abstraction called ChannelTransport. It simulates network communication between nodes identified by SocketAddr (network socket addresses) using asynchronous channels instead of actual network sockets. The transport facilitates sending and receiving serialized ChitchatMessage instances, tracking statistics, and managing simulated network links (including their removal and restoration). The transport supports optional MTU (Maximum Transmission Unit) size enforcement and is designed to work asynchronously with Tokio's async runtime.

The main components are:

This file interacts closely with:


Entities and Their Details

Statistics

A simple struct to track total bytes and message counts sent via the channel transport.


ChannelTransportInner

An internal state container used within a mutex to manage:


ChannelTransport

The main transport struct providing asynchronous message passing between SocketAddrs. It holds an Arc<Mutex<ChannelTransportInner>> for shared mutable state and an optional mtu_opt to enforce message size limits.


InProcessSocket

Represents a socket bound to a local address within ChannelTransport.


Important Implementation Details


Interaction with Other System Components


Visual Diagram

classDiagram
class ChannelTransport {
+inner: Arc<Mutex<ChannelTransportInner>>
+mtu_opt: Option<usize>
+with_mtu()
+max_datagram_payload_size()
+open()
+statistics()
+add_link()
+remove_link()
+send()
+close()
}
class ChannelTransportInner {
+send_channels: HashMap<SocketAddr, Sender<(SocketAddr, ChitchatMessage)>>
+statistics: Statistics
+removed_links: HashMap<SocketAddr, HashSet<SocketAddr>>
}
class Statistics {
+num_bytes_total: u64
+num_messages_total: u64
+record_message_len()
}
class InProcessSocket {
+listen_addr: SocketAddr
+broker: ChannelTransport
+message_rx: Receiver<(SocketAddr, ChitchatMessage)>
+send()
+recv()
}
ChannelTransport "1" *-- "1" ChannelTransportInner : contains
ChannelTransportInner --> "many" Sender : holds
ChannelTransportInner --> Statistics : tracks
ChannelTransport o-- InProcessSocket : creates
InProcessSocket --> ChannelTransport : uses

This diagram illustrates the relationship between the core structs: ChannelTransport manages an inner state ChannelTransportInner, which tracks sending channels and statistics. It creates InProcessSocket instances that depend on the transport for sending and receiving messages.


Summary of Key Function Workflows


References to Related Topics