Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
websocket_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
14
16{
17
18namespace
19{
21 std::atomic<uint64_t> g_client_id_counter{0};
22
24 std::atomic<uint64_t> g_server_id_counter{0};
25} // namespace
26
28{
29 const auto id = g_client_id_counter.fetch_add(1, std::memory_order_relaxed);
30 std::ostringstream oss;
31 oss << "ws_client_" << std::setfill('0') << std::setw(8) << id;
32 return oss.str();
33}
34
36{
37 const auto id = g_server_id_counter.fetch_add(1, std::memory_order_relaxed);
38 std::ostringstream oss;
39 oss << "ws_server_" << std::setfill('0') << std::setw(8) << id;
40 return oss.str();
41}
42
44{
45 if (config.ping_interval.count() <= 0)
46 {
47 return error_void(-1, "websocket_facade: ping_interval must be positive", "websocket_facade");
48 }
49
50 return ok();
51}
52
54{
55 if (config.port == 0 || config.port > 65535)
56 {
57 return error_void(-1, "websocket_facade: port must be between 1 and 65535", "websocket_facade");
58 }
59
60 if (config.path.empty() || config.path[0] != '/')
61 {
62 return error_void(-1, "websocket_facade: path must start with '/'", "websocket_facade");
63 }
64
65 return ok();
66}
67
70{
71 auto validation = validate_client_config(config);
72 if (validation.is_err())
73 {
75 validation.error().code, validation.error().message, "websocket_facade");
76 }
77
78 const auto client_id = config.client_id.empty() ? generate_client_id() : config.client_id;
79
80 // Create adapter with ping interval from config
81 // Note: Path must be set by caller via dynamic_cast if non-default path needed
82 return ok(std::shared_ptr<interfaces::i_protocol_client>(
83 std::make_shared<internal::adapters::ws_client_adapter>(
84 client_id, config.ping_interval)));
85}
86
89{
90 auto validation = validate_server_config(config);
91 if (validation.is_err())
92 {
94 validation.error().code, validation.error().message, "websocket_facade");
95 }
96
97 const auto server_id = config.server_id.empty() ? generate_server_id() : config.server_id;
98
99 // Create adapter and configure path
100 auto adapter = std::make_shared<internal::adapters::ws_server_adapter>(server_id);
101 adapter->set_path(config.path);
102
103 return ok(std::shared_ptr<interfaces::i_protocol_server>(adapter));
104}
105
106} // namespace kcenon::network::facade
static auto generate_server_id() -> std::string
Generates a unique server ID.
static auto validate_client_config(const client_config &config) -> VoidResult
Validates client configuration.
static auto validate_server_config(const server_config &config) -> VoidResult
Validates server configuration.
static auto generate_client_id() -> std::string
Generates a unique client ID.
auto create_server(const server_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_server > >
Creates a WebSocket server with the specified configuration.
auto create_client(const client_config &config) const -> Result< std::shared_ptr< interfaces::i_protocol_client > >
Creates a WebSocket client with the specified configuration.
tracing_config config
Definition exporters.cpp:29
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 WebSocket client.
Configuration for creating a WebSocket server.
Simplified facade for creating WebSocket clients and servers.