Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
quic_server_example.cpp File Reference
#include "internal/experimental/quic_server.h"
#include <kcenon/network/detail/session/quic_session.h>
#include <iostream>
#include <string>
#include <memory>
#include <thread>
#include <chrono>
#include <vector>
#include <atomic>
#include <signal.h>
Include dependency graph for quic_server_example.cpp:

Go to the source code of this file.

Classes

class  QuicServerDemo
 Simple QUIC server demo. More...
 

Macros

#define NETWORK_USE_EXPERIMENTAL
 

Functions

void signal_handler (int)
 
void demo_multicast (std::shared_ptr< core::messaging_quic_server > server)
 Example showing multicast functionality.
 
void simple_server_example ()
 Example with minimal setup.
 
int main (int argc, char *argv[])
 

Variables

std::atomic< bool > g_running {true}
 

Macro Definition Documentation

◆ NETWORK_USE_EXPERIMENTAL

#define NETWORK_USE_EXPERIMENTAL

BSD 3-Clause License Copyright (c) 2024, Network System Project

QUIC Server Example

This example demonstrates how to create a QUIC server that accepts client connections and handles data transmission.

Note: This sample uses messaging_quic_server directly instead of quic_facade because it needs QUIC-specific features like broadcasting, multicasting, and session management. The quic_facade returns i_protocol_server which provides a unified interface but doesn't expose QUIC-specific methods.

Key features demonstrated:

  • Starting and stopping the server
  • Handling client connections and disconnections
  • Receiving data from clients
  • Broadcasting to all connected clients
  • Multicasting to specific clients
  • Session management

Definition at line 26 of file quic_server_example.cpp.

Function Documentation

◆ demo_multicast()

void demo_multicast ( std::shared_ptr< core::messaging_quic_server > server)

Example showing multicast functionality.

Definition at line 279 of file quic_server_example.cpp.

280{
281 std::cout << "\n=== Multicast Demo ===" << std::endl;
282
283 auto sessions = server->sessions();
284 if (sessions.size() < 2)
285 {
286 std::cout << "Need at least 2 clients for multicast demo" << std::endl;
287 return;
288 }
289
290 // Get first two session IDs
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());
294
295 std::string multicast_msg = "Multicast: Selected clients only!";
296 auto result = server->multicast(
297 target_ids,
298 std::vector<uint8_t>(multicast_msg.begin(), multicast_msg.end()));
299
300 if (result.is_ok())
301 {
302 std::cout << "Multicast sent to " << target_ids.size() << " clients"
303 << std::endl;
304 }
305 else
306 {
307 std::cerr << "Multicast failed: " << result.error().message
308 << std::endl;
309 }
310}
@ server
Server-side handling of a request.

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 345 of file quic_server_example.cpp.

346{
347 // Set up signal handler for graceful shutdown
348 signal(SIGINT, signal_handler);
349 signal(SIGTERM, signal_handler);
350
351 unsigned short port = 4433;
352
353 if (argc >= 2)
354 {
355 port = static_cast<unsigned short>(std::stoi(argv[1]));
356 }
357
358 std::cout << "QUIC Server Example" << std::endl;
359 std::cout << "===================" << std::endl;
360 std::cout << std::endl;
361
362 // Run the main demo
363 QuicServerDemo demo(port);
364 bool success = demo.run();
365
366 return success ? 0 : 1;
367}
Simple QUIC server demo.
void signal_handler(int)

References QuicServerDemo::run(), and signal_handler().

Here is the call graph for this function:

◆ signal_handler()

void signal_handler ( int )
Examples
tcp_echo_server.cpp.

Definition at line 47 of file quic_server_example.cpp.

48{
49 std::cout << "\nShutdown signal received..." << std::endl;
50 g_running.store(false);
51}
std::atomic< bool > g_running

References g_running.

Referenced by main(), and main().

Here is the caller graph for this function:

◆ simple_server_example()

void simple_server_example ( )

Example with minimal setup.

Definition at line 315 of file quic_server_example.cpp.

316{
317 std::cout << "\n=== Simple QUIC Server ===" << std::endl;
318
319 // Create server directly for QUIC-specific features
320 auto server = std::make_shared<core::messaging_quic_server>("simple_server");
321
322 // Minimal setup - just echo received messages
323 server->set_receive_callback(
324 [](std::shared_ptr<quic_session> session,
325 const std::vector<uint8_t>& data)
326 {
327 // Echo back
328 std::vector<uint8_t> response(data);
329 (void)session->send(std::move(response));
330 });
331
333 config.cert_file = "/path/to/server.crt";
334 config.key_file = "/path/to/server.key";
335
336 auto result = server->start_server(4434, config);
337 if (result.is_ok())
338 {
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();
342 }
343}
tracing_config config
Definition exporters.cpp:29
Configuration options for QUIC server.
Definition quic_server.h:70

References config.

Variable Documentation

◆ g_running

std::atomic<bool> g_running {true}
Examples
tcp_echo_server.cpp.

Definition at line 45 of file quic_server_example.cpp.

45{true};

Referenced by main(), QuicServerDemo::run(), and signal_handler().