Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
ws_server_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 <memory>
10#include <mutex>
11#include <string>
12#include <unordered_map>
13
14namespace kcenon::network::core {
15 class messaging_ws_server;
16 class ws_connection;
17}
18
20
33public:
34 explicit ws_session_wrapper(std::shared_ptr<core::ws_connection> connection);
35
36 [[nodiscard]] auto id() const -> std::string_view override;
37 [[nodiscard]] auto is_connected() const -> bool override;
38 [[nodiscard]] auto send(std::vector<uint8_t>&& data) -> VoidResult override;
39 auto close() -> void override;
40
41private:
42 std::shared_ptr<core::ws_connection> connection_;
43 std::string id_cache_;
44};
45
77public:
82 explicit ws_server_adapter(std::string_view server_id);
83
87 ~ws_server_adapter() override;
88
89 // Non-copyable, non-movable
94
95 // =========================================================================
96 // Path Configuration
97 // =========================================================================
98
106 auto set_path(std::string_view path) -> void;
107
108 // =========================================================================
109 // i_network_component interface implementation
110 // =========================================================================
111
112 [[nodiscard]] auto is_running() const -> bool override;
113 auto wait_for_stop() -> void override;
114
115 // =========================================================================
116 // i_protocol_server interface implementation
117 // =========================================================================
118
119 [[nodiscard]] auto start(uint16_t port) -> VoidResult override;
120 [[nodiscard]] auto stop() -> VoidResult override;
121 [[nodiscard]] auto connection_count() const -> size_t override;
122
123 auto set_connection_callback(connection_callback_t callback) -> void override;
124 auto set_disconnection_callback(disconnection_callback_t callback) -> void override;
125 auto set_receive_callback(receive_callback_t callback) -> void override;
126 auto set_error_callback(error_callback_t callback) -> void override;
127
128private:
132 auto setup_internal_callbacks() -> void;
133
137 auto get_or_create_session(std::shared_ptr<core::ws_connection> connection)
138 -> std::shared_ptr<interfaces::i_session>;
139
140 std::string server_id_;
141 std::string path_{"/"}; // Default WebSocket path
142 std::shared_ptr<core::messaging_ws_server> server_;
143
144 mutable std::mutex callbacks_mutex_;
149
150 mutable std::mutex sessions_mutex_;
151 std::unordered_map<std::string, std::shared_ptr<ws_session_wrapper>> sessions_;
152};
153
154} // namespace kcenon::network::internal::adapters
Unified interface for all protocol server implementations.
std::function< void(std::string_view)> disconnection_callback_t
Callback type for disconnections (session_id)
std::function< void(std::string_view, const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data (session_id, data)
std::function< void(std::string_view, std::error_code)> error_callback_t
Callback type for errors (session_id, error)
std::function< void(std::shared_ptr< i_session >)> connection_callback_t
Callback type for new connections.
Interface for a single client session on the server side.
Definition i_session.h:49
Adapter that wraps messaging_ws_server to implement i_protocol_server.
std::shared_ptr< core::messaging_ws_server > server_
auto get_or_create_session(std::shared_ptr< core::ws_connection > connection) -> std::shared_ptr< interfaces::i_session >
Gets or creates a session wrapper for the given connection.
~ws_server_adapter() override
Destructor ensures proper cleanup.
auto set_connection_callback(connection_callback_t callback) -> void override
Sets the callback for new connections.
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
auto is_running() const -> bool override
Checks if the component is currently running.
auto set_path(std::string_view path) -> void
Sets the WebSocket path for accepting connections.
auto set_disconnection_callback(disconnection_callback_t callback) -> void override
Sets the callback for disconnections.
auto connection_count() const -> size_t override
Gets the number of active client connections.
auto setup_internal_callbacks() -> void
Sets up internal callbacks to bridge WebSocket callbacks to i_protocol_server callbacks.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
std::unordered_map< std::string, std::shared_ptr< ws_session_wrapper > > sessions_
auto stop() -> VoidResult override
Stops the server and closes all connections.
ws_server_adapter & operator=(const ws_server_adapter &)=delete
auto start(uint16_t port) -> VoidResult override
Starts the server and begins listening for connections.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
ws_server_adapter & operator=(ws_server_adapter &&)=delete
ws_server_adapter(const ws_server_adapter &)=delete
ws_server_adapter(std::string_view server_id)
Constructs an adapter with a unique server ID.
Session wrapper for WebSocket connections implementing i_session.
auto close() -> void override
Closes the session.
auto is_connected() const -> bool override
Checks if the session is currently connected.
std::shared_ptr< core::ws_connection > connection_
auto id() const -> std::string_view override
Gets the unique identifier for this session.
ws_session_wrapper(std::shared_ptr< core::ws_connection > connection)
QUIC connection state machine (RFC 9000 Section 5)
Definition connection.h:158
Protocol-specific server interface extending i_server.