Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
legacy_client_example.cpp
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2025, kcenon
5All rights reserved.
6*****************************************************************************/
7
18// Note: These includes are for reference only
19// #include <kcenon/network/interfaces/i_client.h>
20// #include <kcenon/network/interfaces/i_tcp_client.h>
21
22#include <cstdint>
23#include <functional>
24#include <iostream>
25#include <memory>
26#include <string_view>
27#include <system_error>
28#include <vector>
29
30// This example shows the LEGACY patterns (deprecated)
31// Do NOT use this in new code
32
33namespace legacy_example {
34
45// Simulated legacy interface (for documentation purposes)
46class i_client {
47public:
48 virtual ~i_client() = default;
49
50 using receive_callback_t = std::function<void(const std::vector<uint8_t>&)>;
51 using connected_callback_t = std::function<void()>;
52 using disconnected_callback_t = std::function<void()>;
53 using error_callback_t = std::function<void(std::error_code)>;
54
55 virtual bool start(std::string_view host, uint16_t port) = 0;
56 virtual bool stop() = 0;
57 virtual bool send(std::vector<uint8_t>&& data) = 0;
58 virtual bool is_connected() const = 0;
59
60 virtual void set_receive_callback(receive_callback_t callback) = 0;
61 virtual void set_connected_callback(connected_callback_t callback) = 0;
63 virtual void set_error_callback(error_callback_t callback) = 0;
64};
65
74void setup_legacy_client(std::unique_ptr<i_client> client) {
75 // OLD: Set callbacks individually (verbose)
76 client->set_receive_callback([](const std::vector<uint8_t>& data) {
77 std::cout << "[Legacy] Received " << data.size() << " bytes\n";
78 });
79
80 client->set_connected_callback([]() {
81 std::cout << "[Legacy] Connected!\n";
82 });
83
84 client->set_disconnected_callback([]() {
85 std::cout << "[Legacy] Disconnected\n";
86 });
87
88 client->set_error_callback([](std::error_code ec) {
89 std::cerr << "[Legacy] Error: " << ec.message() << "\n";
90 });
91
92 // OLD: Start with separate host and port
93 if (!client->start("localhost", 8080)) {
94 std::cerr << "[Legacy] Failed to connect\n";
95 return;
96 }
97
98 // OLD: Send using std::vector<uint8_t>
99 std::vector<uint8_t> data = {'H', 'e', 'l', 'l', 'o'};
100 client->send(std::move(data));
101}
102
103} // namespace legacy_example
104
105int main() {
106 std::cout << "=== Legacy Client Example ===\n";
107 std::cout << "This file demonstrates the DEPRECATED legacy API.\n";
108 std::cout << "See unified_client_example.cpp for the new approach.\n\n";
109
110 std::cout << "Legacy issues:\n";
111 std::cout << " 1. Multiple protocol-specific interfaces to learn\n";
112 std::cout << " 2. Verbose individual callback setters\n";
113 std::cout << " 3. Protocol-coupled code\n";
114 std::cout << " 4. std::vector<uint8_t> instead of std::span<std::byte>\n";
115
116 return 0;
117}
std::function< void(std::error_code)> error_callback_t
virtual bool stop()=0
virtual void set_disconnected_callback(disconnected_callback_t callback)=0
virtual void set_connected_callback(connected_callback_t callback)=0
std::function< void()> disconnected_callback_t
virtual bool send(std::vector< uint8_t > &&data)=0
virtual ~i_client()=default
virtual bool is_connected() const =0
virtual void set_receive_callback(receive_callback_t callback)=0
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
virtual bool start(std::string_view host, uint16_t port)=0
virtual void set_error_callback(error_callback_t callback)=0
std::function< void()> connected_callback_t
void setup_legacy_client(std::unique_ptr< i_client > client)
Legacy client setup example.