Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
unified_client_example.cpp File Reference

Example showing the new unified client interface usage. More...

#include <kcenon/network/protocol/protocol.h>
#include <kcenon/network/unified/unified.h>
#include <cstddef>
#include <iostream>
#include <span>
#include <vector>
Include dependency graph for unified_client_example.cpp:

Go to the source code of this file.

Functions

void handle_connection (std::unique_ptr< unified::i_connection > conn)
 Protocol-agnostic connection handler.
 
void tcp_example ()
 TCP client example using unified API.
 
void udp_example ()
 UDP client example using unified API.
 
void websocket_example ()
 WebSocket client example using unified API.
 
std::unique_ptr< unified::i_connection > create_connection (const std::string &protocol, const unified::endpoint_info &endpoint)
 Example of protocol selection at runtime.
 
int main ()
 

Detailed Description

Example showing the new unified client interface usage.

This file demonstrates the NEW, RECOMMENDED way of creating network clients using the unified interface API.

Benefits:

  • Single interface (i_connection) for all protocols
  • Protocol selection via factory functions
  • Cleaner callback setup
  • Protocol-agnostic business logic
See also
legacy_client_example.cpp for comparison with old approach

Definition in file unified_client_example.cpp.

Function Documentation

◆ create_connection()

std::unique_ptr< unified::i_connection > create_connection ( const std::string & protocol,
const unified::endpoint_info & endpoint )

Example of protocol selection at runtime.

Definition at line 113 of file unified_client_example.cpp.

115 {
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}
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
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

References kcenon::network::protocol::tcp::connect(), kcenon::network::protocol::udp::connect(), kcenon::network::protocol::websocket::connect(), kcenon::network::unified::endpoint_info::host, and kcenon::network::unified::endpoint_info::port.

Here is the call graph for this function:

◆ handle_connection()

void handle_connection ( std::unique_ptr< unified::i_connection > conn)

Protocol-agnostic connection handler.

This function works with ANY protocol - TCP, UDP, WebSocket, QUIC. The unified interface enables truly protocol-agnostic code.

Definition at line 40 of file unified_client_example.cpp.

40 {
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}
virtual auto set_callbacks(connection_callbacks callbacks) -> void=0
Sets all connection callbacks at once.
virtual auto connect(const endpoint_info &endpoint) -> VoidResult=0
Connects to a remote endpoint using host/port.
virtual auto send(std::span< const std::byte > data) -> VoidResult=0
Sends raw data to the remote endpoint.

Referenced by tcp_example(), udp_example(), and websocket_example().

Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 131 of file unified_client_example.cpp.

131 {
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}

◆ tcp_example()

void tcp_example ( )

TCP client example using unified API.

Definition at line 78 of file unified_client_example.cpp.

78 {
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}
void handle_connection(std::unique_ptr< unified::i_connection > conn)
Protocol-agnostic connection handler.

References kcenon::network::protocol::tcp::connect(), and handle_connection().

Here is the call graph for this function:

◆ udp_example()

void udp_example ( )

UDP client example using unified API.

Definition at line 89 of file unified_client_example.cpp.

89 {
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}

References kcenon::network::protocol::udp::connect(), and handle_connection().

Here is the call graph for this function:

◆ websocket_example()

void websocket_example ( )

WebSocket client example using unified API.

Definition at line 100 of file unified_client_example.cpp.

100 {
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}

References kcenon::network::protocol::websocket::connect(), and handle_connection().

Here is the call graph for this function: