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>;
78# ifdef BOOST_ASIO_VERSION
82 std::error_code convert_boost_to_std(
const boost::system::error_code& boost_ec) {
83 return std::error_code(boost_ec.value(), std::generic_category());
91 const size_t MIN_URL_SIZE = 6;
94 m_config->url.size() < MIN_URL_SIZE) {
99 if (protocol !=
"wss" && protocol !=
"ws") {
105 if (protocol ==
"wss") {
108 WssClient::Connection,
109 WssClient::InMessage>();
113 WsClient::Connection,
114 WsClient::InMessage>();
134 void send_message(std::shared_ptr<WebSocketSendInfo>& send_info)
override final {
138 if (!send_info->callback)
return;
148 void send_close(std::shared_ptr<WebSocketSendInfo>& send_info)
override final {
152 if (!send_info->callback)
return;
164 template<
class ConnectionType>
166 const ConnectionType& connection,
170 [
this, send_info](
const SimpleWeb::error_code &ec) {
171 if (!send_info->callback) return;
172# ifdef ASIO_STANDALONE
173 add_send_callback(ec, send_info->callback);
175 add_send_callback(convert_boost_to_std(ec), send_info->callback);
184 template<
class ConnectionType>
186 const ConnectionType& connection,
188 connection->send_close(
191 [
this, send_info](
const SimpleWeb::error_code& ec) {
192 if (!send_info->callback) return;
193# ifdef ASIO_STANDALONE
194 add_send_callback(ec, send_info->callback);
196 add_send_callback(convert_boost_to_std(ec), send_info->callback);
202 template<
class ClientType,
class ConnectionType,
class MessageType>
206 client->config.timeout_idle =
m_config->idle_timeout;
207 client->config.timeout_request =
m_config->request_timeout;
212 if (!
m_config->user_agent.empty() &&
214 client->config.header.insert({
"User-Agent",
m_config->user_agent});
216 if (!
m_config->accept_encoding.empty() &&
218 client->config.header.insert({
"Accept-Encoding",
m_config->accept_encoding});
222 client->config.header.insert({
"Cookie",
m_config->cookie});
224 if (!
m_config->proxy_server.empty()) {
225 client->config.proxy_server =
m_config->proxy_server;
227 if (!
m_config->proxy_auth.empty()) {
228 client->config.proxy_auth =
m_config->proxy_auth;
232 client->on_open = [
this](std::shared_ptr<ConnectionType> connection) {
238 client->on_message = [
this](
239 std::shared_ptr<ConnectionType> connection,
240 std::shared_ptr<MessageType> message) {
244 client->on_close = [
this](
245 std::shared_ptr<ConnectionType> connection,
247 const std::string &reason) {
252 client->on_error = [
this](
253 std::shared_ptr<ConnectionType> connection,
254 const SimpleWeb::error_code &ec) {
264 typename std::enable_if<std::is_same<T, WssClient>::value>::type* = 0) {
277 typename std::enable_if<std::is_same<T, WsClient>::value>::type* = 0) {
283 template<
class ConnectionType>
285 std::shared_ptr<ConnectionType>& connection) {
288 websocket_event->status_code =
static_cast<long>(SimpleWeb::status_code(connection->status_code));
289 return websocket_event;
292 template<
class MessageType>
294 std::shared_ptr<MessageType>& message) {
297 websocket_event->message = message->string();
298 return websocket_event;
303 std::shared_ptr<T> &connection,
304 typename std::enable_if<std::is_same<T, WssClient::Connection>::value>::type* = 0) {
310 std::shared_ptr<T> &connection,
311 typename std::enable_if<std::is_same<T, WsClient::Connection>::value>::type* = 0) {
315 template<
class ConnectionType>
317 return connection->remote_endpoint().address().to_string() +
":" +
318 std::to_string(connection->remote_endpoint().port());
321 template<
typename SrcMap,
typename DstMap>
324 for (
const auto& header : src) {
std::unique_ptr< WebSocketEventData > create_websocket_event()
Creates a generic WebSocket event.
BaseWebSocketClient()=default
Default constructor.
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.