Mesh Transport is a peerβtoβpeer networking layer that enables agents to discover each other and exchange messages in an adβhoc, possibly partitioned, network.
-
Discovery
- Automatic discovery of peers on the same local network (via mDNS, UDP broadcast, or manual list).
- Support for static configuration of known peer addresses.
- Ability to filter peers by agent ID or role.
-
Connection Management
- Establish bidirectional connections (TCP, WebRTC, QUIC) with fallback.
- Handle connection loss and automatic reconnection with exponential backoff.
- Keepβalive heartbeats to detect dead peers.
-
Message Routing
- Unreliable broadcast (flooding) for smallβswarm scenarios.
- Reliable unicast for pointβtoβpoint commands.
- Optional multicast groups for topicβbased messaging.
-
Quality of Service
- Priority queues for critical messages (e.g., emergency stop).
- Configurable retransmission for reliable delivery.
- Bandwidth throttling per peer.
-
Security
- TLSβlike encryption (Noise protocol) for all links.
- Peer authentication via preβshared keys or certificate authority.
- Message integrity and replay protection.
- Latency: < 100 ms for localβnetwork messages.
- Throughput: Support at least 1000 messages/second per agent.
- Scalability: Up to 50 agents in a single mesh.
- Resource usage: < 5 MB RAM and < 1% CPU when idle.
βββββββββββββββββββββββββββββββββββββββββββ
β Mesh Transport β
βββββββββββββββ¬ββββββββββββββ¬ββββββββββββββ€
β Discovery β Connection β Routing β
β Module β Manager β Module β
βββββββββββββββΌββββββββββββββΌββββββββββββββ€
β Security Layer β
βββββββββββββββββββββββββββββββββββββββββββ€
β Transport Backend β
β (libp2p, smolβnet) β
βββββββββββββββββββββββββββββββββββββββββββ
- Implements
Discoverytrait. - Provides
discover_peers() -> Vec<PeerInfo>. - Emits
PeerDiscoveredandPeerLostevents.
- Maintains a
HashMap<PeerId, Connection>. - Creates outgoing connections and accepts incoming ones.
- Monitors health and triggers reconnection.
- Implements
RouteMessagetrait. broadcast(data: Vec<u8>)floods to all connected peers.send_to(peer: PeerId, data: Vec<u8>)delivers to a specific peer.
- Wraps each connection with an authenticated encrypted channel.
- Uses Noise protocol framework (XX handshake).
- Abstract trait
TransportBackendallowing pluggable implementations. - Default backend:
Libp2pBackend(usinglibp2pcrate). - Alternative backend:
SmolNetBackend(custom lightweight TCP/UDP).
pub struct PeerId([u8; 32]); // Cryptographic hash of public key
pub struct PeerInfo {
pub id: PeerId,
pub addresses: Vec<SocketAddr>,
pub metadata: HashMap<String, String>,
}
pub enum TransportEvent {
PeerDiscovered(PeerInfo),
PeerLost(PeerId),
MessageReceived {
from: PeerId,
payload: Vec<u8>,
timestamp: Instant,
},
ConnectionEstablished(PeerId),
ConnectionClosed(PeerId),
}
pub trait MeshTransport {
fn broadcast(&self, payload: Vec<u8>) -> Result<()>;
fn send_to(&self, peer: PeerId, payload: Vec<u8>) -> Result<()>;
fn events(&self) -> Box<dyn Stream<Item = TransportEvent> + Send>;
fn peers(&self) -> Vec<PeerInfo>;
}- Create
crates/mesh-transportwithlibp2pas a dependency. - Implement a simple discovery via mDNS (libp2pβmdns).
- Establish TCP connections between discovered peers.
- Send plainβtext βpingβpongβ messages.
- Unit test with two inβprocess nodes.
- Add Noise protocol encryption.
- Implement reliable message delivery with sequence numbers and ACKs.
- Add connection heartbeat and reconnection logic.
- Benchmark latency and throughput.
- Support for UDP (QUIC) for lower latency.
- Multicast groups for topicβbased subscriptions.
- Integration with resource monitor for adaptive QoS.
libp2p(with featurestcp,mdns,noise,yamux)tokiofor async runtimeserdefor configuration serializationtracingfor structured logging
- Unit tests: Mock network interfaces with
libp2pβswarmβtest. - Integration tests: Spawn multiple OSβlevel processes that communicate via loopback.
- Simulation tests: Use
tokioβtestand virtual time to simulate network partitions.
- Should we support WebRTC for browserβbased agents?
- Is mDNS sufficient for discovery, or do we need a custom beacon protocol?
- How to handle NAT traversal (STUN/TURN)?