Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
unified_server_example.cpp
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2025, kcenon
5All rights reserved.
6*****************************************************************************/
7
24
25#include <cstddef>
26#include <iostream>
27#include <map>
28#include <memory>
29#include <span>
30#include <string>
31
32using namespace kcenon::network;
33
40void setup_server(std::unique_ptr<unified::i_listener> listener) {
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}
69
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}
80
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}
91
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}
102
110public:
111 void start(std::unique_ptr<unified::i_listener> listener) {
112 listener_ = std::move(listener);
113
114 // Use accept callback to get connection ownership
116 [this](std::unique_ptr<unified::i_connection> conn) {
117 handle_new_connection(std::move(conn));
118 });
119
120 listener_->start(8080);
121 }
122
123 void broadcast(std::span<const std::byte> data) {
124 for (auto& [id, conn] : connections_) {
125 conn->send(data);
126 }
127 }
128
129private:
130 void handle_new_connection(std::unique_ptr<unified::i_connection> conn) {
131 // Get connection ID from endpoint info
132 auto endpoint = conn->remote_endpoint();
133 std::string id = std::string(endpoint.host) + ":" +
134 std::to_string(endpoint.port);
135
136 std::cout << "Managing connection: " << id << "\n";
137
138 // Set up connection-specific callbacks
139 conn->set_callbacks({
140 .on_data = [id](std::span<const std::byte> data) {
141 std::cout << "Data from " << id << "\n";
142 },
143 .on_disconnected = [this, id]() {
144 connections_.erase(id);
145 std::cout << "Removed connection: " << id << "\n";
146 }
147 });
148
149 // Store connection
150 connections_[id] = std::move(conn);
151 }
152
153 std::unique_ptr<unified::i_listener> listener_;
154 std::map<std::string, std::unique_ptr<unified::i_connection>> connections_;
155};
156
160std::unique_ptr<unified::i_listener> create_listener(
161 const std::string& protocol,
162 uint16_t port) {
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}
174
175int main() {
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}
Advanced example: Managing accepted connections.
void start(std::unique_ptr< unified::i_listener > listener)
void handle_new_connection(std::unique_ptr< unified::i_connection > conn)
void broadcast(std::span< const std::byte > data)
std::map< std::string, std::unique_ptr< unified::i_connection > > connections_
std::unique_ptr< unified::i_listener > listener_
virtual auto set_accept_callback(accept_callback_t callback) -> void=0
Sets the accept callback for new connections.
virtual auto start(const endpoint_info &bind_address) -> VoidResult=0
Starts listening for incoming connections.
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
Main namespace for all Network System components.
Convenience header for all protocol factory functions.
Convenience header for the unified network interface API.
std::unique_ptr< unified::i_listener > create_listener(const std::string &protocol, uint16_t port)
Example of protocol selection at runtime.
void setup_server(std::unique_ptr< unified::i_listener > listener)
Protocol-agnostic server setup.
void udp_server_example()
UDP server example using unified API.
void tcp_server_example()
TCP server example using unified API.
void websocket_server_example()
WebSocket server example using unified API.