![]() |
Kurlyk
|
Base class implementing core functionality for WebSocket clients, managing events, states, and message sending. More...
#include <BaseWebSocketClient.hpp>
Classes | |
| struct | FSMEventData |
| Represents an event in the finite state machine (FSM) with optional associated data and callback. More... | |
Public Member Functions | |
| BaseWebSocketClient ()=default | |
| Default constructor. | |
| virtual | ~BaseWebSocketClient ()=default |
| Virtual destructor. | |
| std::function< void(std::unique_ptr< WebSocketEventData >)> & | event_handler () override final |
| Accessor for the event handler function. | |
| std::function< void()> & | notify_handler () override final |
| Accesses the notification handler for WebSocket events. | |
| void | set_config (std::unique_ptr< WebSocketConfig > config, std::function< void(bool)> callback) override final |
| Sets the configuration for the WebSocket client. | |
| void | connect (std::function< void(bool)> callback) override final |
| Initiates a connection to the WebSocket server. | |
| void | disconnect (std::function< void(bool)> callback) override final |
| Closes the connection to the WebSocket server. | |
| bool | is_connected () const override final |
| Checks if the WebSocket client is actively running. | |
| bool | is_running () const override final |
| Checks if the WebSocket client is actively running. | |
| std::list< std::unique_ptr< WebSocketEventData > > | receive_events () const override final |
| Retrieves all pending WebSocket events in a batch. | |
| std::unique_ptr< WebSocketEventData > | receive_event () const override final |
| Retrieves the next available WebSocket event, if any. | |
| SubmitResult | submit_message (const std::string &message, long rate_limit_id, std::function< void(const std::error_code &ec)> callback=nullptr) override final |
| Attempts to submit a message through the WebSocket. | |
| bool | send_message (const std::string &message, long rate_limit_id, std::function< void(const std::error_code &ec)> callback=nullptr) override final |
| Sends a message through the WebSocket. | |
| SubmitResult | submit_close (const int status=1000, const std::string &reason=std::string(), std::function< void(const std::error_code &ec)> callback=nullptr) override final |
| Attempts to submit a close request through the WebSocket. | |
| bool | send_close (const int status=1000, const std::string &reason=std::string(), std::function< void(const std::error_code &ec)> callback=nullptr) override final |
| Sends a close request through the WebSocket. | |
| void | process () override final |
| Processes internal operations such as event handling and state updates. | |
| void | shutdown () override final |
| Shuts down the WebSocket client, disconnecting and clearing all pending events. | |
| Public Member Functions inherited from kurlyk::IWebSocketClient | |
| IWebSocketClient ()=default | |
| Default constructor. | |
| virtual | ~IWebSocketClient ()=default |
| Virtual destructor for safe cleanup in derived classes. | |
| Public Member Functions inherited from kurlyk::IWebSocketSender | |
| IWebSocketSender ()=default | |
| Default constructor for IWebSocketSender. | |
| virtual | ~IWebSocketSender ()=default |
| Virtual destructor for safe cleanup through the sender abstraction. | |
| virtual std::string | get_http_version ()=0 |
| Retrieves the HTTP version used in the WebSocket connection. | |
| virtual Headers | get_headers ()=0 |
| Retrieves the headers associated with the WebSocket connection. | |
| virtual std::string | get_remote_endpoint ()=0 |
| Retrieves the remote endpoint information. | |
| virtual bool | send_message (const std::string &message, long rate_limit_id=0, std::function< void(const std::error_code &)> callback=nullptr)=0 |
| Sends a WebSocket message. | |
| virtual SubmitResult | submit_message (const std::string &message, long rate_limit_id=0, std::function< void(const std::error_code &)> callback=nullptr) |
| Attempts to submit a WebSocket message and reports the admission result. | |
| virtual bool | send_close (int status=1000, const std::string &reason=std::string(), std::function< void(const std::error_code &)> callback=nullptr)=0 |
| Sends a close request to the WebSocket server. | |
| virtual SubmitResult | submit_close (int status=1000, const std::string &reason=std::string(), std::function< void(const std::error_code &)> callback=nullptr) |
| Attempts to submit a close request and reports the admission result. | |
Protected Types | |
| enum class | FsmState { INIT , CONNECTING , WORKING , RECONNECTING , STOPPED } |
| Finite State Machine (FSM) states for the WebSocket connection. More... | |
| enum class | FsmEvent { RequestConnect , RequestDisconnect , ConnectionOpened , ConnectionClosed , ConnectionError , MessageReceived , UpdateConfig } |
| Represents events in the finite state machine. More... | |
Protected Member Functions | |
| virtual bool | init_websocket ()=0 |
| Initializes the WebSocket connection. Must be implemented in derived classes. | |
| virtual void | deinit_websocket ()=0 |
| Deinitializes the WebSocket connection. Must be implemented in derived classes. | |
| virtual void | send_message (std::shared_ptr< WebSocketSendInfo > &send_info)=0 |
| Sends a WebSocket message. | |
| virtual void | send_close (std::shared_ptr< WebSocketSendInfo > &send_info)=0 |
| Sends a close request. | |
| std::unique_ptr< WebSocketEventData > | create_websocket_event () |
| Creates a generic WebSocket event. | |
| std::unique_ptr< WebSocketEventData > | create_websocket_close_event (const std::string &reason="Normal Closure", int status_code=1000) |
| Creates a WebSocket close event with a specified reason and status code. | |
| std::unique_ptr< WebSocketEventData > | create_websocket_error_event (const std::error_code &error_code) |
| Creates a WebSocket error event with a specified error code. | |
| void | add_send_callback (const std::error_code &error_code, const std::function< void(const std::error_code &ec)> &callback) |
| Adds a send callback to the queue. | |
| void | add_fsm_event (FsmEvent event_type, std::unique_ptr< WebSocketEventData > event_data) |
| Adds an FSM event to the event queue and triggers the notify handler. | |
Protected Attributes | |
| std::unique_ptr< WebSocketConfig > | m_config |
| Current configuration for the WebSocket. | |
| enum kurlyk::BaseWebSocketClient::FsmState | m_fsm_state = FsmState::INIT |
Private Types | |
| using | event_data_ptr_t = std::unique_ptr<WebSocketEventData> |
| Alias for unique pointers to WebSocketEventData. | |
| using | send_info_ptr_t = std::shared_ptr<WebSocketSendInfo> |
| Alias for shared pointers to WebSocketSendInfo. | |
| using | send_callback_t = std::pair<std::error_code, std::function<void(const std::error_code& ec)>> |
| Alias for callback pairs with error codes. | |
Private Member Functions | |
| void | process_fsm_state () |
| Processes the current FSM state and transitions to the appropriate next state. | |
| void | process_state_init () |
| Handles the INIT state. Initializes connection or updates configuration. | |
| void | process_state_connecting () |
| Handles the CONNECTING state. Manages connection attempt, errors, or disconnection. | |
| void | process_state_working () |
| Handles the WORKING state. Processes incoming events and manages connection health. | |
| void | process_state_reconnecting () |
| Handles the RECONNECTING state. Attempts to reconnect based on configuration settings. | |
| void | process_state_stopped () |
| Processes the STOPPED state in the FSM. | |
| void | process_message_queue () |
| Processes the queue of messages to be sent over the WebSocket. | |
| void | process_send_callback_queue () |
| Processes the queue of send callbacks. | |
| void | handle_open_event (std::unique_ptr< WebSocketEventData > event) |
| Handles the event when the WebSocket connection is opened. | |
| void | handle_close_event (std::unique_ptr< WebSocketEventData > event=nullptr) |
| Handles the event when the WebSocket connection is closed. | |
| void | handle_error_event (std::unique_ptr< WebSocketEventData > event) |
| Handles WebSocket error events and queues them if no event handler is set. | |
| void | handle_error_event (const std::error_code &error_code) |
| Overloaded method to handle WebSocket error events using an error code. | |
| void | handle_message_event (std::unique_ptr< WebSocketEventData > event) |
| Handles incoming WebSocket message events and queues them if no event handler is set. | |
Private Attributes | |
| std::function< void(std::unique_ptr< WebSocketEventData >)> | m_on_event |
| Function to handle WebSocket events. Called when a new event is received. | |
| std::function< void()> | m_on_event_notify |
| Function to notify about new events in the FSM. | |
| utils::EventQueue< FSMEventData > | m_fsm_event_queue |
| Queue for FSM events, managing the event sequence for the FSM. | |
| long | m_reconnect_attempt = 0 |
| Counter for the number of reconnection attempts. | |
| std::atomic< bool > | m_is_running = ATOMIC_VAR_INIT(false) |
| Atomic flag indicating if the client is running. | |
| std::atomic< bool > | m_is_connected = ATOMIC_VAR_INIT(false) |
| Atomic flag indicating if the client is connected. | |
| std::atomic< std::size_t > | m_max_send_queue_size = ATOMIC_VAR_INIT(0) |
| Maximum number of queued outbound send operations, or zero if unbounded. | |
| WebSocketRateLimiter | m_rate_limiter |
| Rate limiter for controlling the frequency of message sending. | |
| std::chrono::steady_clock::time_point | m_close_time |
| Timestamp of the last WebSocket close event, used for reconnection timing. | |
| std::mutex | m_event_queue_mutex |
| Mutex for synchronizing access to the event queue. | |
| std::list< event_data_ptr_t > | m_event_queue |
| Queue holding pending WebSocket events. | |
| std::mutex | m_message_queue_mutex |
| Mutex for synchronizing access to the message queue. | |
| std::list< send_info_ptr_t > | m_message_queue |
| Queue holding messages to be sent over the WebSocket. | |
| std::mutex | m_send_callback_queue_mutex |
| Mutex for synchronizing access to the send callback queue. | |
| std::list< send_callback_t > | m_send_callback_queue |
| Queue holding send callbacks with their respective error codes. | |
Base class implementing core functionality for WebSocket clients, managing events, states, and message sending.
Definition at line 17 of file BaseWebSocketClient.hpp.
|
private |
Alias for unique pointers to WebSocketEventData.
Definition at line 385 of file BaseWebSocketClient.hpp.
|
private |
Alias for callback pairs with error codes.
Definition at line 393 of file BaseWebSocketClient.hpp.
|
private |
Alias for shared pointers to WebSocketSendInfo.
Definition at line 389 of file BaseWebSocketClient.hpp.
|
strongprotected |
Represents events in the finite state machine.
Definition at line 209 of file BaseWebSocketClient.hpp.
|
strongprotected |
Finite State Machine (FSM) states for the WebSocket connection.
| Enumerator | |
|---|---|
| INIT | Initialization state. |
| CONNECTING | Awaiting connection. |
| WORKING | Connection active and working. |
| RECONNECTING | Reconnection attempt. |
| STOPPED | Stopped state. |
Definition at line 200 of file BaseWebSocketClient.hpp.
|
default |
Default constructor.
|
virtualdefault |
Virtual destructor.
|
inlineprotected |
Adds an FSM event to the event queue and triggers the notify handler.
| event_type | The event type to be pushed. |
| event_data | The event data to be associated with the event. |
Definition at line 288 of file BaseWebSocketClient.hpp.
|
inlineprotected |
Adds a send callback to the queue.
| error_code | The error code returned after the send operation. |
| callback | The callback function to be called with the error code. |
Definition at line 278 of file BaseWebSocketClient.hpp.
|
inlinefinaloverridevirtual |
Initiates a connection to the WebSocket server.
| callback | Callback function to be executed upon connection completion, receiving a success status. |
Implements kurlyk::IWebSocketClient.
Definition at line 52 of file BaseWebSocketClient.hpp.
|
inlineprotected |
Creates a WebSocket close event with a specified reason and status code.
This method generates a WebSocket close event, setting the event type to "Close" and including an optional reason and status code.
| reason | The reason for the closure. Defaults to "Normal Closure." |
| status_code | The status code associated with the closure. Defaults to 1000 (normal closure). |
Definition at line 252 of file BaseWebSocketClient.hpp.
|
inlineprotected |
Creates a WebSocket error event with a specified error code.
This method generates a WebSocket error event, setting the event type to "Error" and associating it with the provided error code.
| error_code | The error code representing the nature of the error. |
Definition at line 267 of file BaseWebSocketClient.hpp.
|
inlineprotected |
Creates a generic WebSocket event.
Definition at line 236 of file BaseWebSocketClient.hpp.
|
protectedpure virtual |
Deinitializes the WebSocket connection. Must be implemented in derived classes.
Implemented in kurlyk::SimpleWebSocketClientAdapter.
|
inlinefinaloverridevirtual |
Closes the connection to the WebSocket server.
| callback | Callback function to be executed upon disconnection completion, receiving a success status. |
Implements kurlyk::IWebSocketClient.
Definition at line 58 of file BaseWebSocketClient.hpp.
|
inlinefinaloverridevirtual |
Accessor for the event handler function.
This method provides access to the event handler function used for handling WebSocket events. The returned reference allows getting or setting the function that will be called when a WebSocket event occurs.
Implements kurlyk::IWebSocketClient.
Definition at line 30 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Handles the event when the WebSocket connection is closed.
Sets the connection state to disconnected and triggers the event handler, if set. Otherwise, stores the event in the event queue. Generates a close event if none is provided.
| event | Unique pointer to the WebSocket close event data. Defaults to nullptr. |
Definition at line 797 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Overloaded method to handle WebSocket error events using an error code.
| error_code | The error code representing the WebSocket error. |
Definition at line 826 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Handles WebSocket error events and queues them if no event handler is set.
If an event handler exists, it directly processes the error event.
| event | Unique pointer to the WebSocket error event data. |
Definition at line 815 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Handles incoming WebSocket message events and queues them if no event handler is set.
If an event handler exists, it directly processes the message event.
| event | Unique pointer to the WebSocket message event data. |
Definition at line 833 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Handles the event when the WebSocket connection is opened.
Sets the connection state to connected and triggers the event handler, if set. Otherwise, stores the event in the event queue.
| event | Unique pointer to the WebSocket open event data. |
Definition at line 781 of file BaseWebSocketClient.hpp.
|
protectedpure virtual |
Initializes the WebSocket connection. Must be implemented in derived classes.
Implemented in kurlyk::SimpleWebSocketClientAdapter.
|
inlinefinaloverridevirtual |
Checks if the WebSocket client is actively running.
Implements kurlyk::IWebSocketSender.
Definition at line 64 of file BaseWebSocketClient.hpp.
|
inlinefinaloverridevirtual |
Checks if the WebSocket client is actively running.
Implements kurlyk::IWebSocketClient.
Definition at line 70 of file BaseWebSocketClient.hpp.
|
inlinefinaloverridevirtual |
Accesses the notification handler for WebSocket events.
The notification handler triggers the network worker to process WebSocket events by notifying it that an event has occurred, typically through NetworkWorker::get_instance().notify().
Implements kurlyk::IWebSocketClient.
Definition at line 39 of file BaseWebSocketClient.hpp.
|
inlinefinaloverridevirtual |
Processes internal operations such as event handling and state updates.
This function should be called periodically to ensure timely processing of internal state changes, events, and messages.
Implements kurlyk::IWebSocketClient.
Definition at line 179 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Processes the current FSM state and transitions to the appropriate next state.
Definition at line 397 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Processes the queue of messages to be sent over the WebSocket.
Filters the message queue according to the rate limiter. Messages allowed by the rate limiter are transferred to a temporary list and sent sequentially. Close requests are sent immediately.
Definition at line 735 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Processes the queue of send callbacks.
For each callback in the queue, it calls the callback function with the associated error code. This method is typically called after a message has been sent or an error has occurred.
Definition at line 766 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Handles the CONNECTING state. Manages connection attempt, errors, or disconnection.
Definition at line 457 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Handles the INIT state. Initializes connection or updates configuration.
Definition at line 418 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Handles the RECONNECTING state. Attempts to reconnect based on configuration settings.
Definition at line 607 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Processes the STOPPED state in the FSM.
Definition at line 681 of file BaseWebSocketClient.hpp.
|
inlineprivate |
Handles the WORKING state. Processes incoming events and manages connection health.
Definition at line 532 of file BaseWebSocketClient.hpp.
|
inlinefinaloverridevirtual |
Retrieves the next available WebSocket event, if any.
This method supports event-by-event processing by returning the next event in the queue.
Implements kurlyk::IWebSocketClient.
Definition at line 89 of file BaseWebSocketClient.hpp.
|
inlinefinaloverridevirtual |
Retrieves all pending WebSocket events in a batch.
This method enables efficient processing of multiple accumulated events at once.
Implements kurlyk::IWebSocketClient.
Definition at line 78 of file BaseWebSocketClient.hpp.
|
inlinefinaloverride |
Sends a close request through the WebSocket.
| status | The status code to send with the close request. |
| reason | The reason for closing the connection. |
| callback | The callback to be invoked after sending. |
Definition at line 167 of file BaseWebSocketClient.hpp.
|
protectedpure virtual |
Sends a close request.
| send_info | Reference to WebSocketSendInfo containing close details. |
Implemented in kurlyk::SimpleWebSocketClientAdapter.
|
inlinefinaloverride |
Sends a message through the WebSocket.
| message | The message to send. |
| rate_limit_id | The rate limit type to apply. |
| callback | The callback to be invoked after sending. |
Definition at line 130 of file BaseWebSocketClient.hpp.
|
protectedpure virtual |
Sends a WebSocket message.
| send_info | Reference to WebSocketSendInfo containing message details. |
Implemented in kurlyk::SimpleWebSocketClientAdapter.
|
inlinefinaloverridevirtual |
Sets the configuration for the WebSocket client.
| config | A unique pointer to the WebSocket configuration object. |
| callback | Callback function to be executed upon configuration completion. |
Implements kurlyk::IWebSocketClient.
Definition at line 46 of file BaseWebSocketClient.hpp.
|
inlinefinaloverridevirtual |
Shuts down the WebSocket client, disconnecting and clearing all pending events.
Initiates a disconnect event and processes any remaining events until the client stops running.
Implements kurlyk::IWebSocketClient.
Definition at line 187 of file BaseWebSocketClient.hpp.
|
inlinefinaloverride |
Attempts to submit a close request through the WebSocket.
| status | The status code to send with the close request. |
| reason | The reason for closing the connection. |
| callback | The callback to be invoked after sending. |
Definition at line 142 of file BaseWebSocketClient.hpp.
|
inlinefinaloverride |
Attempts to submit a message through the WebSocket.
| message | The message to send. |
| rate_limit_id | The rate limit type to apply. |
| callback | The callback to be invoked after sending. |
Definition at line 102 of file BaseWebSocketClient.hpp.
|
private |
Timestamp of the last WebSocket close event, used for reconnection timing.
Definition at line 382 of file BaseWebSocketClient.hpp.
|
protected |
Current configuration for the WebSocket.
Definition at line 197 of file BaseWebSocketClient.hpp.
|
mutableprivate |
Queue holding pending WebSocket events.
Definition at line 386 of file BaseWebSocketClient.hpp.
|
mutableprivate |
Mutex for synchronizing access to the event queue.
Definition at line 384 of file BaseWebSocketClient.hpp.
|
private |
Queue for FSM events, managing the event sequence for the FSM.
Definition at line 375 of file BaseWebSocketClient.hpp.
|
protected |
|
private |
Atomic flag indicating if the client is connected.
Definition at line 378 of file BaseWebSocketClient.hpp.
|
private |
Atomic flag indicating if the client is running.
Definition at line 377 of file BaseWebSocketClient.hpp.
|
private |
Maximum number of queued outbound send operations, or zero if unbounded.
Definition at line 379 of file BaseWebSocketClient.hpp.
|
private |
Queue holding messages to be sent over the WebSocket.
Definition at line 390 of file BaseWebSocketClient.hpp.
|
private |
Mutex for synchronizing access to the message queue.
Definition at line 388 of file BaseWebSocketClient.hpp.
|
private |
Function to handle WebSocket events. Called when a new event is received.
Definition at line 295 of file BaseWebSocketClient.hpp.
|
private |
Function to notify about new events in the FSM.
Definition at line 296 of file BaseWebSocketClient.hpp.
|
private |
Rate limiter for controlling the frequency of message sending.
Definition at line 381 of file BaseWebSocketClient.hpp.
|
private |
Counter for the number of reconnection attempts.
Definition at line 376 of file BaseWebSocketClient.hpp.
|
private |
Queue holding send callbacks with their respective error codes.
Definition at line 394 of file BaseWebSocketClient.hpp.
|
private |
Mutex for synchronizing access to the send callback queue.
Definition at line 392 of file BaseWebSocketClient.hpp.