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

Example showing the new unified server (listener) interface usage. More...

#include <kcenon/network/protocol/protocol.h>
#include <kcenon/network/unified/unified.h>
#include <cstddef>
#include <iostream>
#include <map>
#include <memory>
#include <span>
#include <string>
Include dependency graph for unified_server_example.cpp:

Go to the source code of this file.

Classes

class  ConnectionManager
 Advanced example: Managing accepted connections. More...
 

Functions

void setup_server (std::unique_ptr< unified::i_listener > listener)
 Protocol-agnostic server setup.
 
void tcp_server_example ()
 TCP server example using unified API.
 
void udp_server_example ()
 UDP server example using unified API.
 
void websocket_server_example ()
 WebSocket server example using unified API.
 
std::unique_ptr< unified::i_listener > create_listener (const std::string &protocol, uint16_t port)
 Example of protocol selection at runtime.
 
int main ()
 

Detailed Description

Example showing the new unified server (listener) interface usage.

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

Benefits:

  • Single interface (i_listener) for all protocols
  • Protocol selection via factory functions
  • Unified callback structure
  • Accepted connections use same i_connection interface

Definition in file unified_server_example.cpp.

Function Documentation

◆ create_listener()

std::unique_ptr< unified::i_listener > create_listener ( const std::string & protocol,
uint16_t port )

Example of protocol selection at runtime.

Definition at line 160 of file unified_server_example.cpp.

162 {
163
164 if (protocol == "tcp") {
165 return protocol::tcp::listen(port);
166 } else if (protocol == "udp") {
167 return protocol::udp::listen(port);
168 } else if (protocol == "websocket") {
169 return protocol::websocket::listen(port, "/");
170 }
171
172 throw std::invalid_argument("Unknown protocol: " + protocol);
173}
auto listen(const unified::endpoint_info &bind_address, std::string_view id="") -> std::unique_ptr< unified::i_listener >
Creates and starts a TCP listener in one call.
Definition tcp.cpp:62
auto listen(const unified::endpoint_info &bind_address, std::string_view id="") -> std::unique_ptr< unified::i_listener >
Creates and starts a UDP listener in one call.
Definition udp.cpp:62
auto listen(const unified::endpoint_info &bind_address, std::string_view path="/", std::string_view id="") -> std::unique_ptr< unified::i_listener >
Creates and starts a WebSocket listener in one call.
Definition websocket.cpp:65

References kcenon::network::protocol::tcp::listen(), kcenon::network::protocol::udp::listen(), and kcenon::network::protocol::websocket::listen().

Here is the call graph for this function:

◆ main()

int main ( )

Definition at line 175 of file unified_server_example.cpp.

175 {
176 std::cout << "=== Unified Server Example ===\n";
177 std::cout << "This file demonstrates the NEW unified API for servers.\n\n";
178
179 std::cout << "Benefits of unified server API:\n";
180 std::cout << " 1. Single i_listener interface for all protocols\n";
181 std::cout << " 2. Accepted connections are i_connection (same as clients)\n";
182 std::cout << " 3. Protocol selection via factory functions\n";
183 std::cout << " 4. Cleaner callback setup with aggregate initialization\n";
184 std::cout << " 5. Easy connection management with ownership\n";
185
186 std::cout << "\nCode patterns shown:\n";
187 std::cout << " - protocol::tcp::listen(port)\n";
188 std::cout << " - protocol::udp::listen(port)\n";
189 std::cout << " - protocol::websocket::listen(port, path)\n";
190 std::cout << " - listener->set_callbacks({...})\n";
191 std::cout << " - listener->set_accept_callback(callback)\n";
192 std::cout << " - listener->start(port)\n";
193 std::cout << " - listener->broadcast(data)\n";
194
195 return 0;
196}

◆ setup_server()

void setup_server ( std::unique_ptr< unified::i_listener > listener)

Protocol-agnostic server setup.

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

Definition at line 40 of file unified_server_example.cpp.

40 {
41 // NEW: Set all callbacks at once using aggregate initialization
42 listener->set_callbacks({
43 .on_accept = [](std::string_view conn_id) {
44 std::cout << "[Server] New connection: " << conn_id << "\n";
45 },
46 .on_data = [](std::string_view conn_id, std::span<const std::byte> data) {
47 std::cout << "[Server] Received " << data.size()
48 << " bytes from " << conn_id << "\n";
49
50 // Process data...
51 },
52 .on_disconnect = [](std::string_view conn_id) {
53 std::cout << "[Server] Disconnected: " << conn_id << "\n";
54 },
55 .on_error = [](std::string_view conn_id, std::error_code ec) {
56 std::cerr << "[Server] Error on " << conn_id
57 << ": " << ec.message() << "\n";
58 }
59 });
60
61 // NEW: Start listening
62 if (auto result = listener->start(8080); !result) {
63 std::cerr << "[Server] Failed to start\n";
64 return;
65 }
66
67 std::cout << "[Server] Listening on port 8080\n";
68}
virtual auto set_callbacks(listener_callbacks callbacks) -> void=0
Sets all listener callbacks at once.
virtual auto start(const endpoint_info &bind_address) -> VoidResult=0
Starts listening for incoming connections.

Referenced by tcp_server_example(), udp_server_example(), and websocket_server_example().

Here is the caller graph for this function:

◆ tcp_server_example()

void tcp_server_example ( )

TCP server example using unified API.

Definition at line 73 of file unified_server_example.cpp.

73 {
74 std::cout << "\n=== TCP Server (Unified API) ===\n";
75
76 // NEW: Create listener via protocol factory
77 auto listener = protocol::tcp::listen(8080);
78 setup_server(std::move(listener));
79}
void setup_server(std::unique_ptr< unified::i_listener > listener)
Protocol-agnostic server setup.

References kcenon::network::protocol::tcp::listen(), and setup_server().

Here is the call graph for this function:

◆ udp_server_example()

void udp_server_example ( )

UDP server example using unified API.

Definition at line 84 of file unified_server_example.cpp.

84 {
85 std::cout << "\n=== UDP Server (Unified API) ===\n";
86
87 // Same interface, different protocol factory
88 auto listener = protocol::udp::listen(5555);
89 setup_server(std::move(listener));
90}

References kcenon::network::protocol::udp::listen(), and setup_server().

Here is the call graph for this function:

◆ websocket_server_example()

void websocket_server_example ( )

WebSocket server example using unified API.

Definition at line 95 of file unified_server_example.cpp.

95 {
96 std::cout << "\n=== WebSocket Server (Unified API) ===\n";
97
98 // WebSocket listener
99 auto listener = protocol::websocket::listen(8080, "/ws");
100 setup_server(std::move(listener));
101}

References kcenon::network::protocol::websocket::listen(), and setup_server().

Here is the call graph for this function: