This directory contains examples demonstrating the migration from legacy protocol-specific interfaces to the new unified interface API.
Files
Quick Comparison
Client Code
Legacy (Don't use in new code):
i_tcp_client* client;
i_udp_client* udp_client;
i_websocket_client* ws_client;
client->set_receive_callback(...);
client->set_connected_callback(...);
client->set_disconnected_callback(...);
client->set_error_callback(...);
client->start("localhost", 8080);
Unified (Recommended):
auto tcp = protocol::tcp::connect({"localhost", 8080});
auto udp = protocol::udp::connect({"localhost", 5555});
auto ws = protocol::websocket::connect("ws://localhost:8080/ws");
conn->set_callbacks({
.on_connected = ...,
.on_data = ...,
.on_disconnected = ...,
.on_error = ...
});
conn->connect({"remote.host", 9000});
Server Code
Legacy:
i_tcp_server* server;
server->set_connection_callback(...);
server->set_receive_callback(...);
server->start(8080);
Unified:
auto listener = protocol::tcp::listen(8080);
listener->set_callbacks({
.on_accept = ...,
.on_data = ...,
.on_disconnect = ...
});
listener->start(8080);
Key Benefits
- Fewer interfaces to learn: 3 core interfaces vs 12+ protocol-specific
- Protocol-agnostic code: Write once, use with any protocol
- Cleaner callbacks: Single
set_callbacks() with aggregate initialization
- Modern types:
std::span<std::byte> instead of std::vector<uint8_t>
- Factory pattern: Protocol selection at creation time
Building Examples
These examples are for documentation purposes. To compile:
# From project root
cmake -B build
cmake --build build --target migration_examples
See Also
- Migration Guide
- Unified API Reference