23#include <system_error>
30 class websocket_socket;
35 class ws_session_manager;
76 explicit ws_connection(std::shared_ptr<class ws_connection_impl> impl);
91 [[nodiscard]]
auto id() const -> std::string_view override;
99 [[nodiscard]] auto
is_connected() const ->
bool override;
108 [[nodiscard]] auto
send(std::vector<uint8_t>&& data) ->
VoidResult override;
115 auto
close() ->
void override;
142 auto
close(uint16_t code, std::string_view reason = "") ->
void override;
150 [[nodiscard]] auto
path() const -> std::string_view override;
162 std::shared_ptr<ws_connection_impl>
pimpl_;
209 :
public std::enable_shared_from_this<messaging_ws_server>
273 [[nodiscard]] auto
server_id() const -> const std::
string&;
310 [[nodiscard]] auto
is_running() const ->
bool override;
425 auto
on_close(const std::
string& conn_id, internal::ws_close_code code, const std::
string& reason) ->
void;
430 auto
on_error(const std::
string& conn_id, std::error_code ec) ->
void;
449 internal::ws_close_code code,
450 const std::
string& reason) ->
void;
458 const internal::ws_message& msg) ->
void;
491 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
work_guard_;
Thread-safe callback registration and invocation manager.
High-level WebSocket server with connection management.
utils::lifecycle_manager lifecycle_
auto do_accept() -> void
Starts accepting new connections.
auto set_binary_callback(interfaces::i_websocket_server::binary_callback_t callback) -> void override
Sets the callback for binary messages (interface version).
std::function< void(std::shared_ptr< ws_connection >)> connection_callback_t
Callback type for new connections.
~messaging_ws_server() noexcept override
Destructor. Automatically stops the server if still running.
auto broadcast_binary(const std::vector< uint8_t > &data) -> void
Broadcasts a binary message to all connected clients.
std::shared_ptr< ws_session_manager > session_mgr_
std::function< void(const std::string &, std::error_code)> error_callback_t
Callback type for errors.
std::unique_ptr< asio::io_context > io_context_
auto set_disconnection_callback(interfaces::i_websocket_server::disconnection_callback_t callback) -> void override
Sets the callback for disconnections (interface version).
auto get_all_connections() -> std::vector< std::string >
Gets all connection IDs.
std::mutex acceptor_mutex_
auto set_connection_callback(interfaces::i_websocket_server::connection_callback_t callback) -> void override
Sets the callback for new connections (interface version).
auto handle_new_connection(std::shared_ptr< asio::ip::tcp::socket > socket) -> void
Handles a new connection.
auto do_stop_impl() -> VoidResult
WebSocket-specific implementation of server stop.
auto wait_for_stop() -> void override
Blocks until stop() is called.
auto invoke_message_callback(std::shared_ptr< ws_connection > conn, const internal::ws_message &msg) -> void
Invokes the message callback.
auto invoke_error_callback(const std::string &conn_id, std::error_code ec) -> void
Invokes the error callback.
auto is_running() const -> bool override
Checks if the server is currently running.
auto set_error_callback(interfaces::i_websocket_server::error_callback_t callback) -> void override
Sets the callback for errors (interface version).
auto invoke_connection_callback(std::shared_ptr< ws_connection > conn) -> void
Invokes the connection callback.
auto stop_server() -> VoidResult
Stops the server and releases all resources.
auto do_start_impl(uint16_t port, std::string_view path) -> VoidResult
WebSocket-specific implementation of server start.
auto start(uint16_t port) -> VoidResult override
Starts the WebSocket server on the specified port.
auto start_server(const ws_server_config &config) -> VoidResult
Starts the server with full configuration.
auto connection_count() const -> size_t override
Gets the number of active WebSocket connections.
auto on_error(const std::string &conn_id, std::error_code ec) -> void
Handles errors.
auto server_id() const -> const std::string &
Returns the server identifier.
auto on_message(std::shared_ptr< ws_connection > conn, const internal::ws_message &msg) -> void
Handles received WebSocket messages.
std::function< void(std::shared_ptr< ws_connection >, const std::string &)> text_message_callback_t
Callback type for text messages.
auto invoke_disconnection_callback(const std::string &conn_id, internal::ws_close_code code, const std::string &reason) -> void
Invokes the disconnection callback.
messaging_ws_server(std::string_view server_id)
Constructs a WebSocket server.
auto set_text_callback(interfaces::i_websocket_server::text_callback_t callback) -> void override
Sets the callback for text messages (interface version).
std::unique_ptr< asio::ip::tcp::acceptor > acceptor_
std::function< void(std::shared_ptr< ws_connection >, const std::vector< uint8_t > &)> binary_message_callback_t
Callback type for binary messages.
std::shared_ptr< integration::thread_pool_interface > thread_pool_
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
auto get_connection(const std::string &connection_id) -> std::shared_ptr< ws_connection >
Gets a connection by ID.
std::future< void > io_context_future_
auto broadcast_text(const std::string &message) -> void
Broadcasts a text message to all connected clients.
std::function< void(const std::string &, internal::ws_close_code, const std::string &)> disconnection_callback_t
Callback type for disconnections.
auto on_close(const std::string &conn_id, internal::ws_close_code code, const std::string &reason) -> void
Handles connection close.
std::function< void(std::shared_ptr< ws_connection >, const internal::ws_message &)> message_callback_t
Callback type for WebSocket messages.
auto stop() -> VoidResult override
Stops the WebSocket server.
Represents a WebSocket connection to a client.
auto is_connected() const -> bool override
Checks if the session is currently connected.
std::shared_ptr< ws_connection_impl > pimpl_
ws_connection(std::shared_ptr< class ws_connection_impl > impl)
auto send_text(std::string &&message) -> VoidResult override
Sends a text message to the client.
auto send_binary(std::vector< uint8_t > &&data) -> VoidResult override
Sends a binary message to the client.
auto path() const -> std::string_view override
Gets the requested path from the handshake.
auto close() -> void override
Closes the session.
auto id() const -> std::string_view override
Gets the unique identifier for this session.
auto send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the client.
auto remote_endpoint() const -> std::string
Gets the remote endpoint address.
auto get_impl() const -> std::shared_ptr< ws_connection_impl >
Internal: Get the implementation pointer (for server use)
~ws_connection() override=default
Destructor.
Thread-safe WebSocket session lifecycle management.
Interface for WebSocket server components.
std::function< void(std::string_view, const std::vector< uint8_t > &)> binary_callback_t
Callback type for binary messages (session_id, data)
std::function< void(std::string_view, const std::string &)> text_callback_t
Callback type for text messages (session_id, message)
Interface for a WebSocket session on the server side.
Component lifecycle management (start, stop, restart).
ws_close_code
WebSocket close status codes (RFC 6455 Section 7.4).
ws_server_callback
Callback indices for messaging_ws_server.
Network-specific error and result type definitions.
Configuration for WebSocket server.
std::chrono::milliseconds ping_interval
Ping interval.
bool auto_pong
Auto-respond to pings.
size_t max_connections
Max concurrent connections.
uint16_t port
Server port.
size_t max_message_size
Max message size (10MB)
std::string path
WebSocket path.
Represents a complete WebSocket message.
Thread system integration interface for network_system.