25#ifndef NETWORK_USE_EXPERIMENTAL
26#define NETWORK_USE_EXPERIMENTAL
49 std::cout <<
"\nShutdown signal received..." << std::endl;
66 std::cout <<
"=== QUIC Server Example ===" << std::endl;
67 std::cout <<
"Starting server on port " <<
port_ << std::endl;
70 server_ = std::make_shared<core::messaging_quic_server>(
"quic_demo_server");
77 config.max_idle_timeout_ms = 30000;
78 config.max_connections = 100;
79 config.alpn_protocols = {
"h3",
"hq-interop"};
85 std::cerr <<
"Failed to start server: " << result.error().message
90 std::cout <<
"Server started successfully!" << std::endl;
91 std::cout <<
"Press Ctrl+C to stop the server..." << std::endl;
92 std::cout << std::endl;
97 std::this_thread::sleep_for(std::chrono::seconds(5));
106 std::cout <<
"\n=== Shutting Down ===" << std::endl;
110 <<
" clients..." << std::endl;
114 std::cout <<
"Stopping server..." << std::endl;
118 std::cout <<
"Server stopped." << std::endl;
127 [](std::shared_ptr<quic_session> session)
129 std::cout <<
"[Connect] New client: " << session->session_id()
130 <<
" from " << session->remote_endpoint().address()
134 std::string welcome =
"Welcome to QUIC server!";
135 auto result = session->send(
136 std::vector<uint8_t>(welcome.begin(), welcome.end()));
139 std::cerr <<
" Failed to send welcome: "
140 << result.error().message << std::endl;
146 [](std::shared_ptr<quic_session> session)
148 std::cout <<
"[Disconnect] Client left: "
149 << session->session_id() << std::endl;
154 [
this](std::shared_ptr<quic_session> session,
155 const std::vector<uint8_t>& data)
157 std::string msg(data.begin(), data.end());
158 std::cout <<
"[Receive] From " << session->session_id() <<
": "
162 std::string response =
"Echo: " + msg;
163 auto result = session->send(
164 std::vector<uint8_t>(response.begin(), response.end()));
167 std::cerr <<
" Failed to echo: " << result.error().message
177 [](std::shared_ptr<quic_session> session,
179 const std::vector<uint8_t>& data,
182 std::cout <<
"[Stream] From " << session->session_id()
183 <<
" on stream " << stream_id <<
": "
184 << data.size() <<
" bytes"
185 << (fin ?
" (FIN)" :
"") << std::endl;
190 [](std::error_code ec)
192 std::cerr <<
"[Error] Server error: " << ec.message()
198 const std::string& msg)
200 if (msg ==
"/status")
203 std::string status =
"Server status: " +
205 " clients connected";
207 std::vector<uint8_t>(status.begin(), status.end()));
209 else if (msg ==
"/broadcast")
214 else if (msg.substr(0, 5) ==
"/list")
217 std::string list =
"Connected clients:\n";
220 list +=
" - " + s->session_id() +
"\n";
222 (void)session->send(std::vector<uint8_t>(list.begin(), list.end()));
228 std::cout <<
"\n=== Broadcasting to All Clients ===" << std::endl;
230 std::string broadcast_msg =
"Broadcast: Hello everyone!";
232 std::vector<uint8_t>(broadcast_msg.begin(), broadcast_msg.end()));
237 <<
" clients" << std::endl;
241 std::cerr <<
"Broadcast failed: " << result.error().message
248 std::cout <<
"\n--- Server Status ---" << std::endl;
255 if (!sessions.empty())
257 std::cout <<
"Sessions:" << std::endl;
258 for (
const auto& session : sessions)
260 if (session && session->is_active())
262 auto stats = session->stats();
263 std::cout <<
" " << session->session_id()
264 <<
" - Bytes: sent=" << stats.bytes_sent
265 <<
", recv=" << stats.bytes_received << std::endl;
269 std::cout <<
"---------------------" << std::endl;
273 std::shared_ptr<core::messaging_quic_server>
server_;
281 std::cout <<
"\n=== Multicast Demo ===" << std::endl;
283 auto sessions = server->sessions();
284 if (sessions.size() < 2)
286 std::cout <<
"Need at least 2 clients for multicast demo" << std::endl;
291 std::vector<std::string> target_ids;
292 target_ids.push_back(sessions[0]->session_id());
293 target_ids.push_back(sessions[1]->session_id());
295 std::string multicast_msg =
"Multicast: Selected clients only!";
296 auto result = server->multicast(
298 std::vector<uint8_t>(multicast_msg.begin(), multicast_msg.end()));
302 std::cout <<
"Multicast sent to " << target_ids.size() <<
" clients"
307 std::cerr <<
"Multicast failed: " << result.error().message
317 std::cout <<
"\n=== Simple QUIC Server ===" << std::endl;
320 auto server = std::make_shared<core::messaging_quic_server>(
"simple_server");
323 server->set_receive_callback(
324 [](std::shared_ptr<quic_session> session,
325 const std::vector<uint8_t>& data)
328 std::vector<uint8_t> response(data);
329 (void)session->send(std::move(response));
333 config.cert_file =
"/path/to/server.crt";
334 config.key_file =
"/path/to/server.key";
336 auto result = server->start_server(4434,
config);
339 std::cout <<
"Simple server started on port 4434" << std::endl;
340 std::this_thread::sleep_for(std::chrono::seconds(5));
341 (void)server->stop_server();
345int main(
int argc,
char* argv[])
351 unsigned short port = 4433;
355 port =
static_cast<unsigned short>(std::stoi(argv[1]));
358 std::cout <<
"QUIC Server Example" << std::endl;
359 std::cout <<
"===================" << std::endl;
360 std::cout << std::endl;
364 bool success = demo.
run();
366 return success ? 0 : 1;
std::shared_ptr< core::messaging_quic_server > server_
QuicServerDemo(unsigned short port)
void handle_command(std::shared_ptr< quic_session > session, const std::string &msg)
auto is_running() const -> bool override
Checks if the server is currently running.
auto stop_server() -> VoidResult
Stops the server and releases all resources.
auto disconnect_all(uint64_t error_code=0) -> void
Disconnect all active sessions.
auto sessions() const -> std::vector< std::shared_ptr< session::quic_session > >
Get all active sessions.
auto broadcast(std::vector< uint8_t > &&data) -> VoidResult
Send data to all connected clients.
auto set_stream_receive_callback(stream_receive_callback_t callback) -> void
Sets the callback for stream data reception (legacy version).
auto set_receive_callback(interfaces::i_quic_server::receive_callback_t callback) -> void override
Sets the callback for received data on default stream (interface version).
auto set_connection_callback(interfaces::i_quic_server::connection_callback_t callback) -> void override
Sets the callback for new connections (interface version).
auto session_count() const -> size_t
Get the number of active sessions.
auto start_server(unsigned short port) -> VoidResult
Start the server with default configuration.
auto wait_for_stop() -> void override
Blocks until stop() is called.
auto set_disconnection_callback(interfaces::i_quic_server::disconnection_callback_t callback) -> void override
Sets the callback for disconnections (interface version).
auto set_error_callback(interfaces::i_quic_server::error_callback_t callback) -> void override
Sets the callback for errors (interface version).
Main namespace for all Network System components.
void simple_server_example()
Example with minimal setup.
void demo_multicast(std::shared_ptr< core::messaging_quic_server > server)
Example showing multicast functionality.
std::atomic< bool > g_running
QUIC-specific session with stream multiplexing.
Configuration options for QUIC server.