41 "Connection no longer available",
42 "ws_session_wrapper::send"
47 return connection_->send_binary(std::move(data));
63 : server_id_(server_id)
64 , server_(std::make_shared<core::messaging_ws_server>(std::string(server_id)))
83 path_ = std::string(path);
99 server_->wait_for_stop();
113 "Server not initialized",
114 "ws_server_adapter::start"
119 return server_->start_server(port, path_);
128 "Server not initialized",
129 "ws_server_adapter::stop"
133 auto result = server_->stop();
137 std::lock_guard<std::mutex> lock(sessions_mutex_);
151 std::lock_guard<std::mutex> lock(callbacks_mutex_);
152 connection_callback_ = std::move(callback);
157 std::lock_guard<std::mutex> lock(callbacks_mutex_);
158 disconnection_callback_ = std::move(callback);
163 std::lock_guard<std::mutex> lock(callbacks_mutex_);
164 receive_callback_ = std::move(callback);
169 std::lock_guard<std::mutex> lock(callbacks_mutex_);
170 error_callback_ = std::move(callback);
185 server_->set_connection_callback(
186 [
this](std::shared_ptr<interfaces::i_websocket_session> ws_session)
189 auto ws_conn = std::dynamic_pointer_cast<core::ws_connection>(ws_session);
195 auto session = get_or_create_session(ws_conn);
199 std::lock_guard<std::mutex> lock(callbacks_mutex_);
200 callback_copy = connection_callback_;
205 callback_copy(session);
210 server_->set_disconnection_callback(
211 [
this](std::string_view session_id, uint16_t , std::string_view )
215 std::lock_guard<std::mutex> lock(sessions_mutex_);
216 sessions_.erase(std::string(session_id));
221 std::lock_guard<std::mutex> lock(callbacks_mutex_);
222 callback_copy = disconnection_callback_;
227 callback_copy(session_id);
232 server_->set_binary_callback(
233 [
this](std::string_view session_id,
const std::vector<uint8_t>& data)
237 std::lock_guard<std::mutex> lock(callbacks_mutex_);
238 callback_copy = receive_callback_;
243 callback_copy(session_id, data);
248 server_->set_error_callback(
249 [
this](std::string_view session_id, std::error_code ec)
253 std::lock_guard<std::mutex> lock(callbacks_mutex_);
254 callback_copy = error_callback_;
259 callback_copy(session_id, ec);
265 -> std::shared_ptr<interfaces::i_session>
269 std::lock_guard<std::mutex> lock(sessions_mutex_);
271 auto it = sessions_.find(session_id);
272 if (it != sessions_.end())
278 auto session = std::make_shared<ws_session_wrapper>(
connection);
279 sessions_[session_id] = session;
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.
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.
auto stop() -> VoidResult override
Stops the server and closes all connections.
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(std::string_view server_id)
Constructs an adapter with a unique server ID.
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 send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the client.
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)
constexpr int internal_error
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")