Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
ws_connection_adapter.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
9
10#include <atomic>
11#include <memory>
12#include <mutex>
13#include <string>
14
16
38public:
43 explicit ws_connection_adapter(std::string_view connection_id);
44
48 ~ws_connection_adapter() override;
49
50 // Non-copyable, non-movable (contains mutex)
55
56 // =========================================================================
57 // i_transport interface implementation
58 // =========================================================================
59
60 [[nodiscard]] auto send(std::span<const std::byte> data) -> VoidResult override;
61 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
62 [[nodiscard]] auto is_connected() const noexcept -> bool override;
63 [[nodiscard]] auto id() const noexcept -> std::string_view override;
64 [[nodiscard]] auto remote_endpoint() const noexcept -> endpoint_info override;
65 [[nodiscard]] auto local_endpoint() const noexcept -> endpoint_info override;
66
67 // =========================================================================
68 // i_connection interface implementation
69 // =========================================================================
70
71 [[nodiscard]] auto connect(const endpoint_info& endpoint) -> VoidResult override;
72 [[nodiscard]] auto connect(std::string_view url) -> VoidResult override;
73 auto close() noexcept -> void override;
74 auto set_callbacks(connection_callbacks callbacks) -> void override;
75 auto set_options(connection_options options) -> void override;
76 auto set_timeout(std::chrono::milliseconds timeout) -> void override;
77 [[nodiscard]] auto is_connecting() const noexcept -> bool override;
78 auto wait_for_stop() -> void override;
79
80 // =========================================================================
81 // WebSocket-specific configuration
82 // =========================================================================
83
88 auto set_path(std::string_view path) -> void;
89
90private:
94 auto setup_internal_callbacks() -> void;
95
105 static auto parse_websocket_url(std::string_view url,
106 std::string& host,
107 uint16_t& port,
108 std::string& path,
109 bool& secure) -> bool;
110
111 std::string connection_id_;
112 std::shared_ptr<core::messaging_ws_client> client_;
113 std::string ws_path_ = "/";
114
117
118 mutable std::mutex endpoint_mutex_;
121
122 std::atomic<bool> is_connecting_{false};
124};
125
126} // namespace kcenon::network::unified::adapters
Adapter that wraps messaging_ws_client to implement i_connection.
static auto parse_websocket_url(std::string_view url, std::string &host, uint16_t &port, std::string &path, bool &secure) -> bool
Parses a WebSocket URL into components.
auto set_timeout(std::chrono::milliseconds timeout) -> void override
Sets the connection timeout.
auto send(std::span< const std::byte > data) -> VoidResult override
Sends raw data to the remote endpoint.
auto set_callbacks(connection_callbacks callbacks) -> void override
Sets all connection callbacks at once.
auto connect(const endpoint_info &endpoint) -> VoidResult override
Connects to a remote endpoint using host/port.
auto setup_internal_callbacks() -> void
Sets up internal callbacks to bridge to unified callbacks.
auto set_path(std::string_view path) -> void
Sets the WebSocket path for endpoint-based connections.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
ws_connection_adapter(const ws_connection_adapter &)=delete
auto close() noexcept -> void override
Closes the connection gracefully.
auto set_options(connection_options options) -> void override
Sets connection options.
auto remote_endpoint() const noexcept -> endpoint_info override
Gets the remote endpoint information.
ws_connection_adapter & operator=(ws_connection_adapter &&)=delete
ws_connection_adapter(std::string_view connection_id)
Constructs an adapter with a unique connection ID.
std::shared_ptr< core::messaging_ws_client > client_
auto local_endpoint() const noexcept -> endpoint_info override
Gets the local endpoint information.
auto is_connecting() const noexcept -> bool override
Checks if the connection is in the process of connecting.
~ws_connection_adapter() override
Destructor ensures proper cleanup.
ws_connection_adapter(ws_connection_adapter &&)=delete
ws_connection_adapter & operator=(const ws_connection_adapter &)=delete
auto is_connected() const noexcept -> bool override
Checks if the transport is currently connected.
Core interface for active network connections.
Core interface for active network connections.
std::mutex mutex
Callback functions for connection events.
Definition types.h:154
Configuration options for connections.
Definition types.h:198
Network endpoint information (host/port or URL)
Definition types.h:56