Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
tcp_facade.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
6
7#include <atomic>
8#include <iomanip>
9#include <sstream>
10#include <stdexcept>
11
15// SSL implementations will be added after they implement the protocol interfaces
16// #include "internal/core/secure_messaging_client.h"
17// #include "internal/core/secure_messaging_server.h"
18
20{
21
22namespace
23{
25 std::atomic<uint64_t> g_client_id_counter{0};
26
28 std::atomic<uint64_t> g_server_id_counter{0};
29} // namespace
30
31auto tcp_facade::generate_client_id() -> std::string
32{
33 const auto id = g_client_id_counter.fetch_add(1, std::memory_order_relaxed);
34 std::ostringstream oss;
35 oss << "tcp_client_" << std::setfill('0') << std::setw(8) << id;
36 return oss.str();
37}
38
39auto tcp_facade::generate_server_id() -> std::string
40{
41 const auto id = g_server_id_counter.fetch_add(1, std::memory_order_relaxed);
42 std::ostringstream oss;
43 oss << "tcp_server_" << std::setfill('0') << std::setw(8) << id;
44 return oss.str();
45}
46
48{
49 if (config.host.empty())
50 {
51 return error_void(-1, "tcp_facade: host cannot be empty", "tcp_facade");
52 }
53
54 if (config.port == 0 || config.port > 65535)
55 {
56 return error_void(-1, "tcp_facade: port must be between 1 and 65535", "tcp_facade");
57 }
58
59 if (config.timeout.count() <= 0)
60 {
61 return error_void(-1, "tcp_facade: timeout must be positive", "tcp_facade");
62 }
63
64 return ok();
65}
66
68{
69 if (config.port == 0 || config.port > 65535)
70 {
71 return error_void(-1, "tcp_facade: port must be between 1 and 65535", "tcp_facade");
72 }
73
74 if (config.use_ssl)
75 {
76 if (!config.cert_path.has_value() || config.cert_path->empty())
77 {
78 return error_void(-1, "tcp_facade: cert_path required when use_ssl=true", "tcp_facade");
79 }
80
81 if (!config.key_path.has_value() || config.key_path->empty())
82 {
83 return error_void(-1, "tcp_facade: key_path required when use_ssl=true", "tcp_facade");
84 }
85 }
86
87 return ok();
88}
89
92{
93 // Validate configuration
94 auto validation = validate_client_config(config);
95 if (validation.is_err())
96 {
98 validation.error().code, validation.error().message, "tcp_facade");
99 }
100
101 // Generate client ID if not provided
102 const std::string client_id = config.client_id.empty() ? generate_client_id() : config.client_id;
103
104 // SSL support not yet implemented
105 if (config.use_ssl)
106 {
108 -6, "tcp_facade: SSL/TLS support not yet implemented", "tcp_facade");
109 }
110
111 auto client = std::make_shared<core::messaging_client>(client_id);
112
113 // Start the client and connect
114 auto result = client->start(config.host, config.port);
115 if (result.is_err())
116 {
118 -600, "tcp_facade: failed to start client: " + result.error().message, "tcp_facade");
119 }
120
121 return ok(std::shared_ptr<interfaces::i_protocol_client>(client));
122}
123
126{
127 // Validate configuration
128 auto validation = validate_server_config(config);
129 if (validation.is_err())
130 {
132 validation.error().code, validation.error().message, "tcp_facade");
133 }
134
135 // Generate server ID if not provided
136 const std::string server_id = config.server_id.empty() ? generate_server_id() : config.server_id;
137
138 // SSL support not yet implemented
139 if (config.use_ssl)
140 {
142 -6, "tcp_facade: SSL/TLS support not yet implemented", "tcp_facade");
143 }
144
145 // Create adapter wrapping messaging_server
146 auto server = std::make_shared<internal::adapters::tcp_server_adapter>(server_id);
147
148 return ok(std::shared_ptr<interfaces::i_protocol_server>(server));
149}
150
153{
154 if (config.host.empty())
155 {
157 -1, "tcp_facade: host cannot be empty", "tcp_facade");
158 }
159
160 if (config.port == 0 || config.port > 65535)
161 {
163 -1, "tcp_facade: port must be between 1 and 65535", "tcp_facade");
164 }
165
166 if (config.pool_size == 0)
167 {
169 -1, "tcp_facade: pool_size must be greater than 0", "tcp_facade");
170 }
171
172 return ok(std::make_shared<core::connection_pool>(
173 config.host,
174 config.port,
175 config.pool_size,
176 config.acquire_timeout));
177}
178
179} // namespace kcenon::network::facade
static auto validate_server_config(const server_config &config) -> VoidResult
Validates server configuration.
auto create_server(const server_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_server > >
Creates a TCP server with the specified configuration.
auto create_connection_pool(const pool_config &config) const -> Result< std::shared_ptr< core::connection_pool > >
Creates a TCP connection pool with the specified configuration.
auto create_client(const client_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_client > >
Creates a TCP client with the specified configuration.
static auto generate_server_id() -> std::string
Generates a unique server ID.
static auto generate_client_id() -> std::string
Generates a unique client ID.
static auto validate_client_config(const client_config &config) -> VoidResult
Validates client configuration.
tracing_config config
Definition exporters.cpp:29
TCP client implementation.
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")
VoidResult ok()
Configuration for creating a TCP client.
Definition tcp_facade.h:102
Configuration for creating a TCP connection pool.
Definition tcp_facade.h:195
Configuration for creating a TCP server.
Definition tcp_facade.h:130
Simplified facade for creating TCP clients and servers.