Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
unified_client_example.cpp
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2025, kcenon
5All rights reserved.
6*****************************************************************************/
7
26
27#include <cstddef>
28#include <iostream>
29#include <span>
30#include <vector>
31
32using namespace kcenon::network;
33
40void handle_connection(std::unique_ptr<unified::i_connection> conn) {
41 // NEW: Set all callbacks at once using aggregate initialization
42 conn->set_callbacks({
43 .on_connected = []() {
44 std::cout << "[Unified] Connected!\n";
45 },
46 .on_data = [](std::span<const std::byte> data) {
47 std::cout << "[Unified] Received " << data.size() << " bytes\n";
48
49 // Easy to work with modern C++ types
50 std::vector<std::byte> buffer(data.begin(), data.end());
51 // Process buffer...
52 },
53 .on_disconnected = []() {
54 std::cout << "[Unified] Disconnected\n";
55 },
56 .on_error = [](std::error_code ec) {
57 std::cerr << "[Unified] Error: " << ec.message() << "\n";
58 }
59 });
60
61 // NEW: Connect using endpoint_info
62 if (auto result = conn->connect({"localhost", 8080}); !result) {
63 std::cerr << "[Unified] Failed to connect\n";
64 return;
65 }
66
67 // NEW: Send using std::span<const std::byte>
68 std::vector<std::byte> data = {
69 std::byte{'H'}, std::byte{'e'}, std::byte{'l'},
70 std::byte{'l'}, std::byte{'o'}
71 };
72 conn->send(data);
73}
74
79 std::cout << "\n=== TCP Client (Unified API) ===\n";
80
81 // NEW: Create connection via protocol factory
82 auto conn = protocol::tcp::connect({"localhost", 8080});
83 handle_connection(std::move(conn));
84}
85
90 std::cout << "\n=== UDP Client (Unified API) ===\n";
91
92 // Same interface, different protocol factory
93 auto conn = protocol::udp::connect({"localhost", 5555});
94 handle_connection(std::move(conn));
95}
96
101 std::cout << "\n=== WebSocket Client (Unified API) ===\n";
102
103 // WebSocket uses URL-based creation
104 auto conn = protocol::websocket::connect("ws://localhost:8080/ws");
105
106 // Can also use the same handle_connection function!
107 handle_connection(std::move(conn));
108}
109
113std::unique_ptr<unified::i_connection> create_connection(
114 const std::string& protocol,
115 const unified::endpoint_info& endpoint) {
116
117 if (protocol == "tcp") {
118 return protocol::tcp::connect(endpoint);
119 } else if (protocol == "udp") {
120 return protocol::udp::connect(endpoint);
121 } else if (protocol == "websocket") {
122 // Convert endpoint to URL for WebSocket
123 std::string url = "ws://" + std::string(endpoint.host) + ":" +
124 std::to_string(endpoint.port);
126 }
127
128 throw std::invalid_argument("Unknown protocol: " + protocol);
129}
130
131int main() {
132 std::cout << "=== Unified Client Example ===\n";
133 std::cout << "This file demonstrates the NEW unified API.\n\n";
134
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";
141
142 // Note: These examples create connections but won't actually connect
143 // without a running server. They demonstrate the API patterns.
144
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";
152
153 return 0;
154}
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.
Definition tcp.cpp:39
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.
Definition udp.cpp:39
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.
Definition websocket.cpp:39
Main namespace for all Network System components.
Convenience header for all protocol factory functions.
Network endpoint information (host/port or URL)
Definition types.h:56
std::string host
Hostname, IP address, or full URL.
Definition types.h:57
uint16_t port
Port number (0 if embedded in URL)
Definition types.h:58
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.