Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
ws_client_adapter.h
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
5#pragma once
6
8
9#include <atomic>
10#include <chrono>
11#include <memory>
12#include <mutex>
13#include <string>
14
15namespace kcenon::network::core {
16 class messaging_ws_client;
17}
18
20
52public:
58 explicit ws_client_adapter(
59 std::string_view client_id,
60 std::chrono::milliseconds ping_interval = std::chrono::seconds(30));
61
65 ~ws_client_adapter() override;
66
67 // Non-copyable, non-movable
72
73 // =========================================================================
74 // Path Configuration
75 // =========================================================================
76
84 auto set_path(std::string_view path) -> void;
85
86 // =========================================================================
87 // i_network_component interface implementation
88 // =========================================================================
89
90 [[nodiscard]] auto is_running() const -> bool override;
91 auto wait_for_stop() -> void override;
92
93 // =========================================================================
94 // i_protocol_client interface implementation
95 // =========================================================================
96
97 [[nodiscard]] auto start(std::string_view host, uint16_t port) -> VoidResult override;
98 [[nodiscard]] auto stop() -> VoidResult override;
99 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
100 [[nodiscard]] auto is_connected() const -> bool override;
101
102 auto set_observer(std::shared_ptr<interfaces::connection_observer> observer) -> void override;
103
104 auto set_receive_callback(receive_callback_t callback) -> void override;
105 auto set_connected_callback(connected_callback_t callback) -> void override;
106 auto set_disconnected_callback(disconnected_callback_t callback) -> void override;
107 auto set_error_callback(error_callback_t callback) -> void override;
108
109private:
113 auto setup_internal_callbacks() -> void;
114
115 std::string client_id_;
116 std::string path_{"/"}; // Default WebSocket path
117 std::chrono::milliseconds ping_interval_;
118 std::shared_ptr<core::messaging_ws_client> client_;
119
120 mutable std::mutex callbacks_mutex_;
121 std::shared_ptr<interfaces::connection_observer> observer_;
126};
127
128} // namespace kcenon::network::internal::adapters
Unified interface for all protocol client implementations.
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
std::function< void()> connected_callback_t
Callback type for connection established.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
std::function< void()> disconnected_callback_t
Callback type for disconnection.
Adapter that wraps messaging_ws_client to implement i_protocol_client.
auto stop() -> VoidResult override
Stops the client and closes the connection.
auto set_path(std::string_view path) -> void
Sets the WebSocket path for connection.
ws_client_adapter & operator=(const ws_client_adapter &)=delete
auto set_disconnected_callback(disconnected_callback_t callback) -> void override
Sets the callback for disconnection.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
std::shared_ptr< core::messaging_ws_client > client_
auto setup_internal_callbacks() -> void
Sets up internal callbacks to bridge WebSocket callbacks to i_protocol_client callbacks.
ws_client_adapter & operator=(ws_client_adapter &&)=delete
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
~ws_client_adapter() override
Destructor ensures proper cleanup.
auto start(std::string_view host, uint16_t port) -> VoidResult override
Starts the client and connects to the specified server.
auto is_running() const -> bool override
Checks if the component is currently running.
auto is_connected() const -> bool override
Checks if the client is connected to the server.
std::shared_ptr< interfaces::connection_observer > observer_
ws_client_adapter(const ws_client_adapter &)=delete
auto set_observer(std::shared_ptr< interfaces::connection_observer > observer) -> void override
Sets the connection observer for unified event handling.
auto set_connected_callback(connected_callback_t callback) -> void override
Sets the callback for connection established.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
ws_client_adapter(std::string_view client_id, std::chrono::milliseconds ping_interval=std::chrono::seconds(30))
Constructs an adapter with a unique client ID.
Protocol-specific client interface extending i_client.