2#ifndef _KURLYK_SIMPLE_WEB_SOCKET_CLIENT_ADAPTER_HPP_INCLUDED
3#define _KURLYK_SIMPLE_WEB_SOCKET_CLIENT_ADAPTER_HPP_INCLUDED
20 using WsClient = SimpleWeb::SocketClient<SimpleWeb::WS>;
21 using WssClient = SimpleWeb::SocketClient<SimpleWeb::WSS>;
80# ifdef BOOST_ASIO_VERSION
84 std::error_code convert_boost_to_std(
const boost::system::error_code& boost_ec) {
85 return std::error_code(boost_ec.value(), std::generic_category());
93 const size_t MIN_URL_SIZE = 6;
96 m_config->url.size() < MIN_URL_SIZE) {
101 if (protocol !=
"wss" && protocol !=
"ws") {
107 if (protocol ==
"wss") {
110 WssClient::Connection,
111 WssClient::InMessage>();
115 WsClient::Connection,
116 WsClient::InMessage>();
136 void send_message(std::shared_ptr<WebSocketSendInfo>& send_info)
override final {
140 if (!send_info->callback)
return;
150 void send_close(std::shared_ptr<WebSocketSendInfo>& send_info)
override final {
154 if (!send_info->callback)
return;
166 template<
class ConnectionType>
168 const ConnectionType& connection,
172 [
this, send_info](
const SimpleWeb::error_code &ec) {
173 if (!send_info->callback) return;
174# ifdef ASIO_STANDALONE
175 add_send_callback(ec, send_info->callback);
177 add_send_callback(convert_boost_to_std(ec), send_info->callback);
186 template<
class ConnectionType>
188 const ConnectionType& connection,
190 connection->send_close(
193 [
this, send_info](
const SimpleWeb::error_code& ec) {
194 if (!send_info->callback) return;
195# ifdef ASIO_STANDALONE
196 add_send_callback(ec, send_info->callback);
198 add_send_callback(convert_boost_to_std(ec), send_info->callback);
204 template<
class ClientType,
class ConnectionType,
class MessageType>
208 client->config.timeout_idle =
m_config->idle_timeout;
209 client->config.timeout_request =
m_config->request_timeout;
214 if (!
m_config->user_agent.empty() &&
216 client->config.header.insert({
"User-Agent",
m_config->user_agent});
218 if (!
m_config->accept_encoding.empty() &&
220 client->config.header.insert({
"Accept-Encoding",
m_config->accept_encoding});
224 client->config.header.insert({
"Cookie",
m_config->cookie});
226 if (!
m_config->proxy_server.empty()) {
227 client->config.proxy_server =
m_config->proxy_server;
229 if (!
m_config->proxy_auth.empty()) {
230 client->config.proxy_auth =
m_config->proxy_auth;
234 client->on_open = [
this](std::shared_ptr<ConnectionType> connection) {
240 client->on_message = [
this](
241 std::shared_ptr<ConnectionType> connection,
242 std::shared_ptr<MessageType> message) {
246 client->on_close = [
this](
247 std::shared_ptr<ConnectionType> connection,
249 const std::string &reason) {
254 client->on_error = [
this](
255 std::shared_ptr<ConnectionType> connection,
256 const SimpleWeb::error_code &ec) {
266 typename std::enable_if<std::is_same<T, WssClient>::value>::type* = 0) {
279 typename std::enable_if<std::is_same<T, WsClient>::value>::type* = 0) {
285 template<
class ConnectionType>
287 std::shared_ptr<ConnectionType>& connection) {
290 websocket_event->status_code =
static_cast<long>(SimpleWeb::status_code(connection->status_code));
291 return websocket_event;
294 template<
class MessageType>
296 std::shared_ptr<MessageType>& message) {
299 websocket_event->message = message->string();
300 return websocket_event;
305 std::shared_ptr<T> &connection,
306 typename std::enable_if<std::is_same<T, WssClient::Connection>::value>::type* = 0) {
312 std::shared_ptr<T> &connection,
313 typename std::enable_if<std::is_same<T, WsClient::Connection>::value>::type* = 0) {
317 template<
class ConnectionType>
319 return connection->remote_endpoint().address().to_string() +
":" +
320 std::to_string(connection->remote_endpoint().port());
323 template<
typename SrcMap,
typename DstMap>
326 for (
const auto& header : src) {
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.
std::unique_ptr< WebSocketEventData > create_websocket_event()
Creates a generic WebSocket event.
BaseWebSocketClient()=default
Default constructor.
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.
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.
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.
std::unique_ptr< WebSocketConfig > m_config
Current configuration for the WebSocket.
std::shared_ptr< WebSocketSendInfo > send_info_ptr_t
Alias for shared pointers to WebSocketSendInfo.
@ ConnectionError
Error in connection.
@ ConnectionClosed
Connection closed.
@ MessageReceived
Incoming WebSocket message.
@ ConnectionOpened
Connection opened successfully.
std::unique_ptr< WebSocketEventData > create_websocket_error_event(const std::error_code &error_code)
Creates a WebSocket error event with a specified error code.
std::shared_ptr< WsClient > m_ws_client
void operator=(const SimpleWebSocketClientAdapter &)=delete
SimpleWeb::SocketClient< SimpleWeb::WSS > WssClient
SimpleWebSocketClientAdapter()
Constructs the WebSocket client and initializes the io_context.
void deinit_websocket() override final
Deinitializes the WebSocket connection.
Headers get_headers() override final
Retrieves the headers associated with the WebSocket connection.
std::shared_ptr< SimpleWeb::io_context > m_io_context
SimpleWeb::SocketClient< SimpleWeb::WS > WsClient
void copy_headers(const SrcMap &src, DstMap &dst)
void init_connection(std::shared_ptr< T > &connection, typename std::enable_if< std::is_same< T, WssClient::Connection >::value >::type *=0)
void send_close(const ConnectionType &connection, const send_info_ptr_t &send_info)
Helper to send a close request on a specific connection type.
std::shared_ptr< WssClient::Connection > m_wss_connection
std::shared_ptr< T > create_client(typename std::enable_if< std::is_same< T, WssClient >::value >::type *=0)
std::mutex m_client_mutex
std::shared_ptr< WsClient::Connection > m_ws_connection
virtual ~SimpleWebSocketClientAdapter()=default
Default destructor for cleanup.
void init_client()
Initializes a WebSocket client based on the provided type and sets up callbacks.
std::shared_ptr< T > create_client(typename std::enable_if< std::is_same< T, WsClient >::value >::type *=0)
void send_message(std::shared_ptr< WebSocketSendInfo > &send_info) override final
Sends a WebSocket message.
std::string get_http_version() override final
Retrieves the HTTP version used in the WebSocket connection.
SimpleWebSocketClientAdapter(const SimpleWebSocketClientAdapter &)=delete
std::unique_ptr< WebSocketEventData > create_websocket_message_event(std::shared_ptr< MessageType > &message)
std::shared_ptr< WssClient > m_wss_client
std::string get_remote_endpoint() override final
Retrieves the remote endpoint information.
void send_close(std::shared_ptr< WebSocketSendInfo > &send_info) override final
Sends a WebSocket close request.
std::unique_ptr< WebSocketEventData > create_websocket_open_event(std::shared_ptr< ConnectionType > &connection)
void init_connection(std::shared_ptr< T > &connection, typename std::enable_if< std::is_same< T, WsClient::Connection >::value >::type *=0)
bool init_websocket() override final
Initializes and starts a WebSocket connection.
std::string endpoint_to_string(const std::shared_ptr< ConnectionType > &connection) const
void send_message(const ConnectionType &connection, const send_info_ptr_t &send_info)
Helper to send a message on a specific connection type.
void notify()
Notifies the worker to check for pending tasks.
void start()
Starts the worker thread if it is not already running.
static SimpleWebSocketWorker & get_instance()
Get the singleton instance of SimpleWebSocketWorker.
std::shared_ptr< SimpleWeb::io_context > get_io_context()
Provides access to the I/O context for WebSocket operations.
@ NotConnected
Operation requires an active connection but none exists.
std::string extract_protocol(const std::string &url)
Extracts the protocol from a URL.
std::error_code make_error_code(ClientError e)
Creates a std::error_code from a ClientError value.
std::string remove_ws_prefix(const std::string &url)
Removes the first occurrence of "wss://" or "ws://" from the given URL.
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
@ WS_MESSAGE
Message received.
@ WS_OPEN
Connection established.
utils::CaseInsensitiveMultimap Headers
Alias for HTTP headers, providing a case-insensitive unordered multimap.