bidirectional ip reflection for listener address discovery

This commit is contained in:
2026-02-09 16:02:23 -03:00
parent 59e9e420be
commit 7b47ef155c
31 changed files with 3192 additions and 269 deletions

View File

@@ -4,6 +4,7 @@ Complete API reference for the P2P file sharing library. This documentation cove
## Table of Contents
- [Event Loop Architecture](#event-loop-architecture) ⚠️ **Read First**
- [Core Module](#core-module)
- [Network Module](#network-module)
- [Protocol Module](#protocol-module)
@@ -11,6 +12,91 @@ Complete API reference for the P2P file sharing library. This documentation cove
- [Error Types](#error-types)
- [Data Structures](#data-structures)
---
## Event Loop Architecture
> ⚠️ **CRITICAL**: This library uses a poll-based architecture. You MUST call `p2p.update()` periodically for the library to function.
### Why Poll-Based?
This design choice enables:
- **WASM compatibility** - WebAssembly has no threading support
- **Single-threaded simplicity** - No mutex/synchronization complexity
- **Application control** - You decide when I/O happens (useful for games, simulations)
- **Consistency with rust-libp2p** - Same proven architecture pattern
### Basic Usage Pattern
```zig
const p2p_mod = @import("p2p");
pub fn main() !void {
var p2p = try p2p_mod.P2P.init(allocator, config);
defer p2p.deinit();
try p2p.start();
// Main application loop
while (running) {
// REQUIRED: Process network I/O
try p2p.update();
// Your application logic here
handleEvents();
// Control loop timing
std.Thread.sleep(10 * std.time.ns_per_ms);
}
}
```
### Common Mistake
```zig
// ❌ WRONG - Will never receive responses!
fn waitForPeer(p2p: *P2P, target: [32]u8) !?Publication {
const deadline = now() + timeout;
while (now() < deadline) {
if (try findPeer(target)) |pub| return pub;
std.Thread.sleep(100 * std.time.ns_per_ms);
// BUG: Network messages are never processed!
}
return null;
}
// ✅ CORRECT - Call update() in the loop
fn waitForPeer(p2p: *P2P, target: [32]u8) !?Publication {
const deadline = now() + timeout;
while (now() < deadline) {
try p2p.update(); // Process incoming messages
if (try findPeer(target)) |pub| return pub;
std.Thread.sleep(100 * std.time.ns_per_ms);
}
return null;
}
```
### What `update()` Does
Each call to `p2p.update()` performs:
1. Accept incoming connections
2. Process received network data
3. Dispatch messages to handlers
4. Process file transfer operations
5. Send queued outgoing messages
### High-Level APIs
High-level APIs like MWP handle `update()` internally:
```zig
// MWP.connect() handles update() for you
const result = try mwp.connect(target_pubkey, .{ .timeout_ms = 30_000 });
```
---
## Module Import Guide
The library uses a module-based import system. Import modules as follows: