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

@@ -79,6 +79,9 @@ p2p.addPackagePath("p2p", "path/to/p2p-library/src");
## 📖 Quick Start
> ⚠️ **Important**: This library uses a poll-based architecture. You must call `p2p.update()`
> in your main loop for the library to process network I/O. See [Event Loop Architecture](docs/API.md#event-loop-architecture).
### Basic Server Example
```zig
@@ -89,38 +92,27 @@ pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
// Initialize P2P library
const config = p2p.Configuration{
.network = .{
.listen_address = try p2p.Address.parse("0.0.0.0:8080"),
.max_connections = 1000,
.connection_timeout = 30000,
},
.transport = .{
.tcp_enabled = true,
.udp_enabled = true,
},
};
var p2p_instance = try p2p.P2PLibrary.init(config);
defer p2p_instance.deinit();
// Start the service
try p2p_instance.start();
// Join the network
const bootstrap_nodes = &[_]p2p.Address{
try p2p.Address.parse("192.168.1.10:8080"),
try p2p.Address.parse("192.168.1.11:8080"),
};
try p2p_instance.join(bootstrap_nodes);
std.debug.print("P2P server started successfully\n", .{});
// Keep running
while (true) {
std.time.sleep(std.time.ns_per_min);
// Initialize P2P node
var node = try p2p.P2P.init(allocator, .{
.listen_port = 8080,
.max_peers = 100,
});
defer node.deinit();
try node.start();
std.debug.print("P2P node started on port 8080\n", .{});
// Main event loop - REQUIRED for the library to function
while (node.isRunning()) {
// Process network I/O (incoming messages, connections, etc.)
try node.update();
// Your application logic here
// ...
// Control loop timing (10ms = ~100 updates/sec)
std.Thread.sleep(10 * std.time.ns_per_ms);
}
}
```