Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
unified_messaging_example.cpp
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2024, 🍀☀🌕🌥 🌊
5All rights reserved.
6*****************************************************************************/
7
27
28#include <iostream>
29#include <string>
30
31using namespace kcenon::network;
32
33int main()
34{
35 std::cout << "=== Unified Messaging Interface Example ===" << std::endl;
36
37 // 1. Endpoint configuration
38 std::cout << "\n1. Endpoint configuration:" << std::endl;
39 unified::endpoint_info server_endpoint;
40 server_endpoint.host = "0.0.0.0";
41 server_endpoint.port = 9090;
42
43 unified::endpoint_info client_endpoint;
44 client_endpoint.host = "localhost";
45 client_endpoint.port = 9090;
46
47 std::cout << " Server: " << server_endpoint.host << ":" << server_endpoint.port << std::endl;
48 std::cout << " Client: " << client_endpoint.host << ":" << client_endpoint.port << std::endl;
49
50 // 2. Connection callbacks
51 std::cout << "\n2. Setting up connection callbacks:" << std::endl;
53
54 callbacks.on_connected = []()
55 { std::cout << " [Callback] Connected!" << std::endl; };
56
57 callbacks.on_data = [](std::span<const std::byte> data)
58 { std::cout << " [Callback] Received " << data.size() << " bytes" << std::endl; };
59
60 callbacks.on_disconnected = [](std::optional<std::string_view> reason)
61 {
62 std::cout << " [Callback] Disconnected";
63 if (reason)
64 {
65 std::cout << ": " << *reason;
66 }
67 std::cout << std::endl;
68 };
69
70 callbacks.on_error = [](std::error_code ec)
71 { std::cout << " [Callback] Error: " << ec.message() << std::endl; };
72
73 std::cout << " All 4 callbacks configured (on_connected, on_data, "
74 << "on_disconnected, on_error)" << std::endl;
75
76 // 3. Interface overview
77 std::cout << "\n3. Unified interface hierarchy:" << std::endl;
78 std::cout << " i_transport - send(), is_connected(), remote_endpoint()" << std::endl;
79 std::cout << " i_connection - connect(), close(), set_callbacks()" << std::endl;
80 std::cout << " i_listener - start(), stop(), set_accept_callback()" << std::endl;
81 std::cout << "\n Use with protocol tags:" << std::endl;
82 std::cout << " unified_messaging_client<tcp_protocol, no_tls>" << std::endl;
83 std::cout << " unified_messaging_server<tcp_protocol, tls_enabled>" << std::endl;
84
85 std::cout << "\nDone." << std::endl;
86 return 0;
87}
Main namespace for all Network System components.
Callback functions for connection events.
Definition types.h:154
std::function< void()> on_disconnected
Called when connection is closed.
Definition types.h:162
std::function< void(std::span< const std::byte >)> on_data
Called when data is received (raw bytes)
Definition types.h:159
std::function< void()> on_connected
Called when connection is established.
Definition types.h:156
std::function< void(std::error_code)> on_error
Called when an error occurs.
Definition types.h:165
Network endpoint information (host/port or URL)
Definition types.h:56
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
Convenience header for the unified network interface API.