Kurlyk
Loading...
Searching...
No Matches
kurlyk::SimpleWebSocketClientAdapter Class Referencefinal

A WebSocket client adapter that leverages the Simple WebSocket Server library for managing WebSocket connections. More...

#include <SimpleWebSocketClientAdapter.hpp>

Inheritance diagram for kurlyk::SimpleWebSocketClientAdapter:
kurlyk::BaseWebSocketClient kurlyk::IWebSocketClient kurlyk::IWebSocketSender

Public Types

using WsClient = SimpleWeb::SocketClient<SimpleWeb::WS>
using WssClient = SimpleWeb::SocketClient<SimpleWeb::WSS>

Public Member Functions

 SimpleWebSocketClientAdapter ()
 Constructs the WebSocket client and initializes the io_context.
virtual ~SimpleWebSocketClientAdapter ()=default
 Default destructor for cleanup.
 SimpleWebSocketClientAdapter (const SimpleWebSocketClientAdapter &)=delete
void operator= (const SimpleWebSocketClientAdapter &)=delete
std::string get_http_version () override final
 Retrieves the HTTP version used in the WebSocket connection.
Headers get_headers () override final
 Retrieves the headers associated with the WebSocket connection.
std::string get_remote_endpoint () override final
 Retrieves the remote endpoint information.
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.
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.
Public Member Functions inherited from kurlyk::BaseWebSocketClient
 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< WebSocketEventDatareceive_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 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.

Private Member Functions

bool init_websocket () override final
 Initializes and starts a WebSocket connection.
void deinit_websocket () override final
 Deinitializes the WebSocket connection.
void send_message (std::shared_ptr< WebSocketSendInfo > &send_info) override final
 Sends a WebSocket message.
void send_close (std::shared_ptr< WebSocketSendInfo > &send_info) override final
 Sends a WebSocket close request.
template<class ConnectionType>
void send_message (const ConnectionType &connection, const send_info_ptr_t &send_info)
 Helper to send a message on a specific connection type.
template<class ConnectionType>
void send_close (const ConnectionType &connection, const send_info_ptr_t &send_info)
 Helper to send a close request on a specific connection type.
template<class ClientType, class ConnectionType, class MessageType>
void init_client ()
 Initializes a WebSocket client based on the provided type and sets up callbacks.
template<class T>
std::shared_ptr< T > create_client (typename std::enable_if< std::is_same< T, WssClient >::value >::type *=0)
template<class T>
std::shared_ptr< T > create_client (typename std::enable_if< std::is_same< T, WsClient >::value >::type *=0)
template<class ConnectionType>
std::unique_ptr< WebSocketEventDatacreate_websocket_open_event (std::shared_ptr< ConnectionType > &connection)
template<class MessageType>
std::unique_ptr< WebSocketEventDatacreate_websocket_message_event (std::shared_ptr< MessageType > &message)
template<class T>
void init_connection (std::shared_ptr< T > &connection, typename std::enable_if< std::is_same< T, WssClient::Connection >::value >::type *=0)
template<class T>
void init_connection (std::shared_ptr< T > &connection, typename std::enable_if< std::is_same< T, WsClient::Connection >::value >::type *=0)
template<class ConnectionType>
std::string endpoint_to_string (const std::shared_ptr< ConnectionType > &connection) const
template<typename SrcMap, typename DstMap>
void copy_headers (const SrcMap &src, DstMap &dst)

Private Attributes

std::mutex m_client_mutex
std::shared_ptr< SimpleWeb::io_context > m_io_context
std::shared_ptr< WsClientm_ws_client
std::shared_ptr< WssClientm_wss_client
std::shared_ptr< WsClient::Connection > m_ws_connection
std::shared_ptr< WssClient::Connection > m_wss_connection

Additional Inherited Members

Protected Types inherited from kurlyk::BaseWebSocketClient
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 inherited from kurlyk::BaseWebSocketClient
std::unique_ptr< WebSocketEventDatacreate_websocket_event ()
 Creates a generic WebSocket event.
std::unique_ptr< WebSocketEventDatacreate_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< WebSocketEventDatacreate_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 inherited from kurlyk::BaseWebSocketClient
std::unique_ptr< WebSocketConfigm_config
 Current configuration for the WebSocket.
enum kurlyk::BaseWebSocketClient::FsmState m_fsm_state = FsmState::INIT

Detailed Description

A WebSocket client adapter that leverages the Simple WebSocket Server library for managing WebSocket connections.

This class implements core WebSocket functionalities, such as connecting, sending messages, handling events, and managing the connection lifecycle with Simple WebSocket Server's SocketClient and SocketClient<WS> types.

Definition at line 18 of file SimpleWebSocketClientAdapter.hpp.

Member Typedef Documentation

◆ WsClient

using kurlyk::SimpleWebSocketClientAdapter::WsClient = SimpleWeb::SocketClient<SimpleWeb::WS>

Definition at line 20 of file SimpleWebSocketClientAdapter.hpp.

◆ WssClient

Definition at line 21 of file SimpleWebSocketClientAdapter.hpp.

Constructor & Destructor Documentation

◆ SimpleWebSocketClientAdapter() [1/2]

kurlyk::SimpleWebSocketClientAdapter::SimpleWebSocketClientAdapter ( )
inline

Constructs the WebSocket client and initializes the io_context.

Definition at line 26 of file SimpleWebSocketClientAdapter.hpp.

◆ ~SimpleWebSocketClientAdapter()

virtual kurlyk::SimpleWebSocketClientAdapter::~SimpleWebSocketClientAdapter ( )
virtualdefault

Default destructor for cleanup.

◆ SimpleWebSocketClientAdapter() [2/2]

kurlyk::SimpleWebSocketClientAdapter::SimpleWebSocketClientAdapter ( const SimpleWebSocketClientAdapter & )
delete

Member Function Documentation

◆ copy_headers()

template<typename SrcMap, typename DstMap>
void kurlyk::SimpleWebSocketClientAdapter::copy_headers ( const SrcMap & src,
DstMap & dst )
inlineprivate

Definition at line 324 of file SimpleWebSocketClientAdapter.hpp.

◆ create_client() [1/2]

template<class T>
std::shared_ptr< T > kurlyk::SimpleWebSocketClientAdapter::create_client ( typename std::enable_if< std::is_same< T, WsClient >::value >::type * = 0)
inlineprivate

Definition at line 278 of file SimpleWebSocketClientAdapter.hpp.

◆ create_client() [2/2]

template<class T>
std::shared_ptr< T > kurlyk::SimpleWebSocketClientAdapter::create_client ( typename std::enable_if< std::is_same< T, WssClient >::value >::type * = 0)
inlineprivate

Definition at line 265 of file SimpleWebSocketClientAdapter.hpp.

◆ create_websocket_message_event()

template<class MessageType>
std::unique_ptr< WebSocketEventData > kurlyk::SimpleWebSocketClientAdapter::create_websocket_message_event ( std::shared_ptr< MessageType > & message)
inlineprivate

Definition at line 295 of file SimpleWebSocketClientAdapter.hpp.

◆ create_websocket_open_event()

template<class ConnectionType>
std::unique_ptr< WebSocketEventData > kurlyk::SimpleWebSocketClientAdapter::create_websocket_open_event ( std::shared_ptr< ConnectionType > & connection)
inlineprivate

Definition at line 286 of file SimpleWebSocketClientAdapter.hpp.

◆ deinit_websocket()

void kurlyk::SimpleWebSocketClientAdapter::deinit_websocket ( )
inlinefinaloverrideprivatevirtual

Deinitializes the WebSocket connection.

Implements kurlyk::BaseWebSocketClient.

Definition at line 126 of file SimpleWebSocketClientAdapter.hpp.

◆ endpoint_to_string()

template<class ConnectionType>
std::string kurlyk::SimpleWebSocketClientAdapter::endpoint_to_string ( const std::shared_ptr< ConnectionType > & connection) const
inlineprivate

Definition at line 318 of file SimpleWebSocketClientAdapter.hpp.

◆ get_headers()

Headers kurlyk::SimpleWebSocketClientAdapter::get_headers ( )
inlinefinaloverridevirtual

Retrieves the headers associated with the WebSocket connection.

Returns
A Headers object containing the HTTP headers.

Implements kurlyk::IWebSocketSender.

Definition at line 49 of file SimpleWebSocketClientAdapter.hpp.

◆ get_http_version()

std::string kurlyk::SimpleWebSocketClientAdapter::get_http_version ( )
inlinefinaloverridevirtual

Retrieves the HTTP version used in the WebSocket connection.

Returns
The HTTP version string.

Implements kurlyk::IWebSocketSender.

Definition at line 40 of file SimpleWebSocketClientAdapter.hpp.

◆ get_remote_endpoint()

std::string kurlyk::SimpleWebSocketClientAdapter::get_remote_endpoint ( )
inlinefinaloverridevirtual

Retrieves the remote endpoint information.

Returns
The remote endpoint as a string in the format "IP:Port".

Implements kurlyk::IWebSocketSender.

Definition at line 65 of file SimpleWebSocketClientAdapter.hpp.

◆ init_client()

template<class ClientType, class ConnectionType, class MessageType>
void kurlyk::SimpleWebSocketClientAdapter::init_client ( )
inlineprivate

Initializes a WebSocket client based on the provided type and sets up callbacks.

Definition at line 205 of file SimpleWebSocketClientAdapter.hpp.

◆ init_connection() [1/2]

template<class T>
void kurlyk::SimpleWebSocketClientAdapter::init_connection ( std::shared_ptr< T > & connection,
typename std::enable_if< std::is_same< T, WsClient::Connection >::value >::type * = 0 )
inlineprivate

Definition at line 311 of file SimpleWebSocketClientAdapter.hpp.

◆ init_connection() [2/2]

template<class T>
void kurlyk::SimpleWebSocketClientAdapter::init_connection ( std::shared_ptr< T > & connection,
typename std::enable_if< std::is_same< T, WssClient::Connection >::value >::type * = 0 )
inlineprivate

Definition at line 304 of file SimpleWebSocketClientAdapter.hpp.

◆ init_websocket()

bool kurlyk::SimpleWebSocketClientAdapter::init_websocket ( )
inlinefinaloverrideprivatevirtual

Initializes and starts a WebSocket connection.

Returns
True if initialization was successful, false otherwise.

Implements kurlyk::BaseWebSocketClient.

Definition at line 91 of file SimpleWebSocketClientAdapter.hpp.

◆ operator=()

void kurlyk::SimpleWebSocketClientAdapter::operator= ( const SimpleWebSocketClientAdapter & )
delete

◆ send_close() [1/3]

template<class ConnectionType>
void kurlyk::SimpleWebSocketClientAdapter::send_close ( const ConnectionType & connection,
const send_info_ptr_t & send_info )
inlineprivate

Helper to send a close request on a specific connection type.

Template Parameters
ConnectionTypeThe connection type (WsClient or WssClient).
Parameters
connectionThe connection to send the close request on.
send_infoInformation about the close request, including callback.

Definition at line 187 of file SimpleWebSocketClientAdapter.hpp.

◆ send_close() [2/3]

bool kurlyk::BaseWebSocketClient::send_close ( const int status = 1000,
const std::string & reason = std::string(),
std::function< void(const std::error_code &ec)> callback = nullptr )
inlinefinaloverride

Sends a close request through the WebSocket.

Parameters
statusThe status code to send with the close request.
reasonThe reason for closing the connection.
callbackThe callback to be invoked after sending.
Returns
True if the close request was successfully queued, false otherwise.

Definition at line 167 of file BaseWebSocketClient.hpp.

◆ send_close() [3/3]

void kurlyk::SimpleWebSocketClientAdapter::send_close ( std::shared_ptr< WebSocketSendInfo > & send_info)
inlinefinaloverrideprivatevirtual

Sends a WebSocket close request.

Parameters
send_infoInformation about the close request, including callback and status.

Implements kurlyk::BaseWebSocketClient.

Definition at line 150 of file SimpleWebSocketClientAdapter.hpp.

◆ send_message() [1/3]

template<class ConnectionType>
void kurlyk::SimpleWebSocketClientAdapter::send_message ( const ConnectionType & connection,
const send_info_ptr_t & send_info )
inlineprivate

Helper to send a message on a specific connection type.

Template Parameters
ConnectionTypeThe connection type (WsClient or WssClient).
Parameters
connectionThe connection to send the message on.
send_infoInformation about the message, including callback.

Definition at line 167 of file SimpleWebSocketClientAdapter.hpp.

◆ send_message() [2/3]

bool kurlyk::BaseWebSocketClient::send_message ( const std::string & message,
long rate_limit_id,
std::function< void(const std::error_code &ec)> callback = nullptr )
inlinefinaloverride

Sends a message through the WebSocket.

Parameters
messageThe message to send.
rate_limit_idThe rate limit type to apply.
callbackThe callback to be invoked after sending.
Returns
True if the message was successfully queued, false otherwise.

Definition at line 130 of file BaseWebSocketClient.hpp.

◆ send_message() [3/3]

void kurlyk::SimpleWebSocketClientAdapter::send_message ( std::shared_ptr< WebSocketSendInfo > & send_info)
inlinefinaloverrideprivatevirtual

Sends a WebSocket message.

Parameters
send_infoInformation about the message, including callback and rate limit.

Implements kurlyk::BaseWebSocketClient.

Definition at line 136 of file SimpleWebSocketClientAdapter.hpp.

Member Data Documentation

◆ m_client_mutex

std::mutex kurlyk::SimpleWebSocketClientAdapter::m_client_mutex
private

Definition at line 73 of file SimpleWebSocketClientAdapter.hpp.

◆ m_io_context

std::shared_ptr<SimpleWeb::io_context> kurlyk::SimpleWebSocketClientAdapter::m_io_context
private

Definition at line 74 of file SimpleWebSocketClientAdapter.hpp.

◆ m_ws_client

std::shared_ptr<WsClient> kurlyk::SimpleWebSocketClientAdapter::m_ws_client
private

Definition at line 75 of file SimpleWebSocketClientAdapter.hpp.

◆ m_ws_connection

std::shared_ptr<WsClient::Connection> kurlyk::SimpleWebSocketClientAdapter::m_ws_connection
private

Definition at line 77 of file SimpleWebSocketClientAdapter.hpp.

◆ m_wss_client

std::shared_ptr<WssClient> kurlyk::SimpleWebSocketClientAdapter::m_wss_client
private

Definition at line 76 of file SimpleWebSocketClientAdapter.hpp.

◆ m_wss_connection

std::shared_ptr<WssClient::Connection> kurlyk::SimpleWebSocketClientAdapter::m_wss_connection
private

Definition at line 78 of file SimpleWebSocketClientAdapter.hpp.


The documentation for this class was generated from the following file: