Peer-to-peer engine for Vix.cpp
Durable. Offline-first. Deterministic.
vix/p2p is the networking layer of Vix.cpp.
It provides everything needed to build a real-world distributed system:
- peer discovery (LAN + registry)
- secure handshake
- message framing
- transport abstraction
- WAL replication
- offline-first sync
Designed for unreliable networks. Built for the real world.
- Local-first → nodes work without network
- Eventually consistent → sync happens later
- Deterministic → same inputs → same state
- Transport-agnostic → TCP, QUIC, future-ready
- Failure-tolerant → restart-safe, replay-safe
Discovery → finds peers (UDP / registry)
Transport → sends frames (TCP / QUIC)
Framing → splits byte streams into messages
Envelope → protocol-level message container
Dispatch → routes message types
EdgeSync → WAL replication + outbox
Router → optional multi-hop routing
Frame encode(payload);
FrameDecodeResult decode(buffer);Example:
LengthPrefixVarint → compact framing using varint length
Every message is wrapped in an envelope:
magic | version | type | msg_id | flags | [crypto] | payload
Features:
- versioning
- encryption (AEAD)
- message tracking
- compatibility checks
Supported message types:
-
Hello
-
HelloAck
-
HelloFinish
-
Ping
-
Pong
-
WalPush
-
WalAck
-
OutboxPull
AnyMessage decode_payload_or_throw(type, payload);Maps raw payload → typed message (std::variant)
- WalPush → send WAL batch
- WalAck → confirm apply
- OutboxPull → request pending ops
Core of offline-first replication.
vix run examples/p2p/01_envelope_and_framing_basic.cppvix run examples/p2p/02_hello_handshake_messages.cppvix run examples/p2p/03_discovery_announce_json.cppvix run examples/p2p/04_router_memory_basic.cppvix run examples/p2p/05_pack_secure_envelope.cppvix run examples/p2p/06_dispatch_decode_basic.cppvix run examples/p2p/07_wal_push_and_ack.cpp--= compiler flags--run= runtime arguments (argv)
Terminal 1:
vix run examples/p2p/08_runtime_manual_connect.cpp --run serverTerminal 2:
vix run examples/p2p/08_runtime_manual_connect.cpp --run client 127.0.0.1 9101Terminal 1:
vix run examples/p2p/09_udp_discovery_basic.cpp --run node-a 9201Terminal 2:
vix run examples/p2p/09_udp_discovery_basic.cpp --run node-b 9202Start a registry returning:
{
"peers": [
{ "host": "127.0.0.1", "tcp_port": 9301, "node_id": "node-x" }
]
}Run:
vix run examples/p2p/10_bootstrap_http_basic.cpp --run http://127.0.0.1:8080/peersInstead of writing code, you can directly run a node:
vix p2p --id A --listen 9001Connect:
vix p2p --id B --listen 9002 --connect 127.0.0.1:9001This uses the same engine internally.
encode(message)
→ envelope
→ framing
→ transport
→ network
→ decode
→ dispatch
A → Hello
B → HelloAck
A → HelloFinish
After that:
- session key established
- encryption enabled
- peer becomes "Connected"
local write → WAL
WAL → WalPush
peer → WalAck
retry until convergence
Real systems fail:
- network drops
- partial writes
- restarts
- duplicates
Traditional systems:
- lose data
- become inconsistent
Vix P2P ensures:
No data is lost. Ever. Sync happens later. Safely.
vix/p2p is not just networking.
It is:
- a sync protocol
- a replication engine
- a failure recovery system
- a foundation for distributed apps
MIT License Copyright (c) 2025 Gaspard Kirira