43 .on_connected = []() {
44 std::cout <<
"[Unified] Connected!\n";
46 .on_data = [](std::span<const std::byte> data) {
47 std::cout <<
"[Unified] Received " << data.size() <<
" bytes\n";
50 std::vector<std::byte> buffer(data.begin(), data.end());
53 .on_disconnected = []() {
54 std::cout <<
"[Unified] Disconnected\n";
56 .on_error = [](std::error_code ec) {
57 std::cerr <<
"[Unified] Error: " << ec.message() <<
"\n";
62 if (
auto result = conn->connect({
"localhost", 8080}); !result) {
63 std::cerr <<
"[Unified] Failed to connect\n";
68 std::vector<std::byte> data = {
69 std::byte{
'H'}, std::byte{
'e'}, std::byte{
'l'},
70 std::byte{
'l'}, std::byte{
'o'}
79 std::cout <<
"\n=== TCP Client (Unified API) ===\n";
90 std::cout <<
"\n=== UDP Client (Unified API) ===\n";
101 std::cout <<
"\n=== WebSocket Client (Unified API) ===\n";
114 const std::string& protocol,
117 if (protocol ==
"tcp") {
119 }
else if (protocol ==
"udp") {
121 }
else if (protocol ==
"websocket") {
123 std::string url =
"ws://" + std::string(endpoint.
host) +
":" +
124 std::to_string(endpoint.
port);
128 throw std::invalid_argument(
"Unknown protocol: " + protocol);
132 std::cout <<
"=== Unified Client Example ===\n";
133 std::cout <<
"This file demonstrates the NEW unified API.\n\n";
135 std::cout <<
"Benefits of unified API:\n";
136 std::cout <<
" 1. Single i_connection interface for all protocols\n";
137 std::cout <<
" 2. Protocol selection via factory functions\n";
138 std::cout <<
" 3. Cleaner callback setup with aggregate initialization\n";
139 std::cout <<
" 4. std::span<std::byte> for modern, efficient data handling\n";
140 std::cout <<
" 5. Protocol-agnostic business logic\n";
145 std::cout <<
"\nCode patterns shown:\n";
146 std::cout <<
" - protocol::tcp::connect(endpoint)\n";
147 std::cout <<
" - protocol::udp::connect(endpoint)\n";
148 std::cout <<
" - protocol::websocket::connect(url)\n";
149 std::cout <<
" - conn->set_callbacks({...})\n";
150 std::cout <<
" - conn->connect(endpoint)\n";
151 std::cout <<
" - conn->send(data)\n";
auto connect(const unified::endpoint_info &endpoint, std::string_view id="") -> std::unique_ptr< unified::i_connection >
Creates and connects a TCP connection in one call.
auto connect(const unified::endpoint_info &endpoint, std::string_view id="") -> std::unique_ptr< unified::i_connection >
Creates and starts a UDP connection in one call.
auto connect(std::string_view url, std::string_view id="") -> std::unique_ptr< unified::i_connection >
Creates and starts a WebSocket connection in one call.
Main namespace for all Network System components.
Convenience header for all protocol factory functions.
Network endpoint information (host/port or URL)
std::string host
Hostname, IP address, or full URL.
uint16_t port
Port number (0 if embedded in URL)
Convenience header for the unified network interface API.
std::unique_ptr< unified::i_connection > create_connection(const std::string &protocol, const unified::endpoint_info &endpoint)
Example of protocol selection at runtime.
void udp_example()
UDP client example using unified API.
void tcp_example()
TCP client example using unified API.
void websocket_example()
WebSocket client example using unified API.
void handle_connection(std::unique_ptr< unified::i_connection > conn)
Protocol-agnostic connection handler.