162 lines
5.3 KiB
Zig
162 lines
5.3 KiB
Zig
//! Example client demonstrating P2P core networking usage
|
|
//!
|
|
//! This example shows how to use the core networking module to create
|
|
//! a simple TCP client that connects to a server and exchanges messages.
|
|
|
|
const std = @import("std");
|
|
const net = std.net;
|
|
const os = std.os;
|
|
|
|
const core = @import("core");
|
|
const Error = core.Error;
|
|
const Address = core.Address;
|
|
const Socket = core.Socket;
|
|
const Connection = core.Connection;
|
|
const ConnectionConfig = core.connection.ConnectionConfig;
|
|
const Buffer = core.buffer;
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
std.debug.print("=== P2P Core Networking Client Example ===\n", .{});
|
|
|
|
// Create connection configuration
|
|
const config = ConnectionConfig{
|
|
.connection_type = .tcp,
|
|
.buffer_size = 4096,
|
|
.timeout_ms = 5000,
|
|
.keep_alive = true,
|
|
.no_delay = true,
|
|
.max_retry_attempts = 3,
|
|
.health_check_interval = 30000,
|
|
.enable_metrics = true,
|
|
};
|
|
|
|
// Create TCP connection
|
|
var connection = try Connection.init(.tcp, config, allocator);
|
|
defer connection.deinit();
|
|
|
|
std.debug.print("Created connection (ID: {})\n", .{connection.getId()});
|
|
|
|
// Connect to server
|
|
const server_address = try Address.parse("127.0.0.1:8080");
|
|
std.debug.print("Connecting to {s}...\n", .{blk: {
|
|
var buf: [128]u8 = undefined;
|
|
break :blk server_address.toStringBuf(&buf);
|
|
}});
|
|
|
|
try connection.connect(server_address);
|
|
std.debug.print("Connected successfully!\n", .{});
|
|
|
|
// Get connection information
|
|
if (connection.getLocalAddress()) |local_addr| {
|
|
std.debug.print("Local address: {s}\n", .{blk: {
|
|
var buf: [128]u8 = undefined;
|
|
break :blk local_addr.toStringBuf(&buf);
|
|
}});
|
|
}
|
|
if (connection.getRemoteAddress()) |remote_addr| {
|
|
std.debug.print("Remote address: {s}\n", .{blk: {
|
|
var buf: [128]u8 = undefined;
|
|
break :blk remote_addr.toStringBuf(&buf);
|
|
}});
|
|
}
|
|
|
|
// Send and receive messages
|
|
try sendReceiveLoop(&connection, allocator);
|
|
|
|
// Health check
|
|
std.debug.print("\nPerforming health check...\n", .{});
|
|
const health = connection.checkHealth();
|
|
std.debug.print("Health status: {s}\n", .{
|
|
@tagName(health.status)
|
|
});
|
|
if (health.err) |err| {
|
|
std.debug.print("Health error: {s}\n", .{@errorName(err)});
|
|
}
|
|
|
|
// Show statistics
|
|
const stats = connection.getStats();
|
|
std.debug.print("\nConnection Statistics:\n", .{});
|
|
std.debug.print(" Bytes sent: {}\n", .{stats.getBytesSent()});
|
|
std.debug.print(" Bytes received: {}\n", .{stats.getBytesReceived()});
|
|
std.debug.print(" Messages sent: {}\n", .{stats.getMessagesSent()});
|
|
std.debug.print(" Messages received: {}\n", .{stats.getMessagesReceived()});
|
|
std.debug.print(" Uptime: {} seconds\n", .{connection.getUptime()});
|
|
std.debug.print(" Errors: {}\n", .{stats.getErrors()});
|
|
|
|
// Graceful shutdown
|
|
std.debug.print("\nPerforming graceful shutdown...\n", .{});
|
|
try connection.gracefulShutdown(1000);
|
|
std.debug.print("Connection closed gracefully.\n", .{});
|
|
}
|
|
|
|
fn sendReceiveLoop(connection: *Connection, _: std.mem.Allocator) !void {
|
|
std.debug.print("\n=== Starting message exchange ===\n", .{});
|
|
|
|
// Create buffer for sending
|
|
var send_buffer = try connection.createSendBuffer();
|
|
defer send_buffer.release();
|
|
|
|
// Send multiple messages
|
|
const messages = [_][]const u8{
|
|
"Hello, Server! This is the first message.",
|
|
"This is the second message from the client.",
|
|
"Third message - testing multiple sends.",
|
|
"Final message from client.",
|
|
};
|
|
|
|
for (messages, 0..) |message, i| {
|
|
std.debug.print("Sending message {}: {s}\n", .{i + 1, message});
|
|
|
|
// Prepare buffer
|
|
try send_buffer.write(message);
|
|
|
|
// Send message
|
|
try connection.sendBuffer(&send_buffer);
|
|
|
|
// Reset buffer for next message
|
|
send_buffer.reset();
|
|
|
|
// Small delay between messages
|
|
std.Thread.sleep(100_000_000); // 100ms
|
|
}
|
|
|
|
std.debug.print("All messages sent successfully!\n", .{});
|
|
|
|
// Receive responses (if any)
|
|
std.debug.print("\nWaiting for responses...\n", .{});
|
|
|
|
var receive_buffer: [1024]u8 = undefined;
|
|
var total_received: usize = 0;
|
|
var message_count: usize = 0;
|
|
|
|
// Try to receive up to 10 messages or until timeout
|
|
while (message_count < 10) {
|
|
const received = connection.receive(&receive_buffer) catch |err| {
|
|
if (err == Error.ConnectionTimeout) {
|
|
std.debug.print("No more data received (timeout)\n", .{});
|
|
break;
|
|
}
|
|
return err;
|
|
};
|
|
|
|
if (received == 0) {
|
|
break;
|
|
}
|
|
|
|
total_received += received;
|
|
message_count += 1;
|
|
|
|
std.debug.print("Received message {} ({} bytes): {s}\n", .{
|
|
message_count, received, receive_buffer[0..received]
|
|
});
|
|
|
|
// Small delay between receives
|
|
std.Thread.sleep(50_000_000); // 50ms
|
|
}
|
|
|
|
std.debug.print("Received {} messages, {} total bytes\n", .{message_count, total_received});
|
|
} |