25 #ifndef _CASA_WS_CLIENT_H
26 #define _CASA_WS_CLIENT_H
28 #include "cpprest/details/basic_types.h"
30 #if !defined(CPPREST_EXCLUDE_WEBSOCKETS)
34 #include <condition_variable>
37 #include "pplx/pplxtasks.h"
38 #include "cpprest/uri.h"
39 #include "cpprest/details/web_utilities.h"
40 #include "cpprest/http_headers.h"
41 #include "cpprest/asyncrt_utils.h"
42 #include "cpprest/ws_msg.h"
53 namespace web_sockets = websockets;
66 protocol_error = 1002,
68 abnormal_close = 1006,
69 inconsistent_datatype = 1007,
70 policy_violation = 1008,
72 negotiate_error = 1010,
73 server_terminate = 1011,
113 return m_credentials;
122 m_credentials = cred;
130 m_sni_enabled =
false;
139 return m_sni_enabled;
149 m_sni_hostname = name;
158 return m_sni_hostname;
189 _ASYNCRTIMP std::vector<::utility::string_t>
subprotocols()
const;
196 utf8string m_sni_hostname;
211 : m_msg(
utility::conversions::to_utf8string(whatArg)) {}
227 : m_errorCode(
utility::details::create_error_code(errorCode))
229 m_msg = m_errorCode.message();
238 : m_errorCode(
utility::details::create_error_code(errorCode)),
239 m_msg(
utility::conversions::to_utf8string(whatArg))
249 : m_errorCode(
utility::details::create_error_code(errorCode)),
250 m_msg(std::move(whatArg))
259 m_errorCode(std::move(code)),
260 m_msg(std::move(whatArg))
272 m_msg = m_errorCode.message();
281 m_errorCode(std::move(code)),
282 m_msg(
utility::conversions::to_utf8string(whatArg))
289 const char*
what() const CPPREST_NOEXCEPT
291 return m_msg.c_str();
304 std::error_code m_errorCode;
317 m_config(std::move(config)) {}
331 virtual void set_close_handler(
const std::function<
void(
websocket_close_status,
const utility::string_t&,
const std::error_code&)>& handler) = 0;
348 static void verify_uri(
const web::uri& uri)
352 if (uri.
scheme() != _XPLATSTR(
"ws") && uri.
scheme() != _XPLATSTR(
"wss"))
354 throw std::invalid_argument(
"URI scheme must be 'ws' or 'wss'");
357 if (uri.
host().empty())
359 throw std::invalid_argument(
"URI must contain a hostname.");
366 throw std::invalid_argument(
"WebSocket URI must not contain fragment identifiers");
382 _ASYNCRTIMP
virtual ~websocket_client_task_impl() CPPREST_NOEXCEPT;
388 const std::shared_ptr<websocket_client_callback_impl> & callback_client()
const {
return m_callback_client; };
396 std::mutex m_receive_queue_lock;
398 std::queue<websocket_incoming_message> m_receive_msg_queue;
400 std::queue<pplx::task_completion_event<websocket_incoming_message>> m_receive_task_queue;
404 bool m_client_closed;
406 std::shared_ptr<websocket_client_callback_impl> m_callback_client;
428 m_client(std::make_shared<details::websocket_client_task_impl>(std::move(config)))
439 m_client->callback_client()->verify_uri(uri);
440 m_client->callback_client()->set_uri(uri);
441 auto client = m_client;
442 return m_client->callback_client()->connect().then([client](
pplx::task<void> result)
450 client->close_pending_tasks_with_error(ex);
462 return m_client->callback_client()->send(msg);
471 return m_client->receive();
480 return m_client->callback_client()->close();
491 return m_client->callback_client()->close(close_status, close_reason);
500 return m_client->callback_client()->uri();
509 return m_client->callback_client()->config();
513 std::shared_ptr<details::websocket_client_task_impl> m_client;
542 m_client->verify_uri(uri);
543 m_client->set_uri(uri);
544 return m_client->connect();
553 return m_client->send(msg);
565 m_client->set_message_handler(handler);
574 return m_client->close();
585 return m_client->close(close_status, close_reason);
598 m_client->set_close_handler(handler);
607 return m_client->uri();
616 return m_client->config();
620 std::shared_ptr<details::websocket_client_callback_impl> m_client;
pplx::task< websocket_incoming_message > receive()
Receive a websocket message.
Definition: ws_client.h:469
Represents a set of user credentials (user name and password) to be used for authentication.
Definition: web_utilities.h:80
pplx::task< void > connect(const web::uri &uri)
Connects to the remote network destination. The connect method initiates the websocket handshake with...
Definition: ws_client.h:540
_ASYNCRTIMP std::vector<::utility::string_t > subprotocols() const
Gets list of the specified subprotocols.
void set_credentials(const web::credentials &cred)
Set the client credentials
Definition: ws_client.h:120
Definition: ws_client.h:312
const std::error_code & error_code() const CPPREST_NOEXCEPT
Gets the underlying error code for the cause of the exception.
Definition: ws_client.h:298
pplx::task< void > close(websocket_close_status close_status, const utility::string_t &close_reason=_XPLATSTR(""))
Closes a websocket client connection, sends a close frame to the server and waits for a close message...
Definition: ws_client.h:583
void disable_sni()
Disables Server Name Indication (SNI). Default is on.
Definition: ws_client.h:128
A flexible, protocol independent URI implementation.
Definition: base_uri.h:151
bool is_sni_enabled() const
Determines if Server Name Indication (SNI) is enabled.
Definition: ws_client.h:137
Represents an incoming websocket message
Definition: ws_msg.h:173
The web namespace contains functionality common to multiple protocols like HTTP and WebSockets...
Definition: base_uri.h:37
web::http::http_headers & headers()
Gets the headers of the HTTP request message used in the WebSocket protocol handshake.
Definition: ws_client.h:168
const utf8string & server_name() const
Gets the server host name to usefor TLS Server Name Indication (SNI).
Definition: ws_client.h:156
websocket_client()
Creates a new websocket_client.
Definition: ws_client.h:419
void get() const
Returns the result this task produced. If the task is not in a terminal state, a call to get will wai...
Definition: pplxtasks.h:4440
void set_proxy(const web_proxy &proxy)
Set the web proxy object
Definition: ws_client.h:102
const websocket_client_config & config() const
Gets the websocket client config object.
Definition: ws_client.h:507
pplx::task< void > connect(const web::uri &uri)
Connects to the remote network destination. The connect method initiates the websocket handshake with...
Definition: ws_client.h:437
websocket_close_status
Websocket close status values.
Definition: ws_client.h:62
void set_server_name(const utf8string &name)
Sets the server host name to use for TLS Server Name Indication (SNI).
Definition: ws_client.h:147
const web_proxy & proxy() const
Get the web proxy object
Definition: ws_client.h:93
pplx::task< void > send(websocket_outgoing_message msg)
Sends a websocket message to the server .
Definition: ws_client.h:460
web_proxy represents the concept of the web proxy, which can be auto-discovered, disabled, or specified explicitly by the user.
Definition: web_utilities.h:157
const web::http::http_headers & headers() const
Gets a const reference to the headers of the WebSocket protocol handshake HTTP message.
Definition: ws_client.h:174
websocket_client_config()
Creates a websocket client configuration with default settings.
Definition: ws_client.h:87
const char * what() const CPPREST_NOEXCEPT
Gets a string identifying the cause of the exception.
Definition: ws_client.h:289
websocket_exception(const utility::string_t &whatArg)
Creates an websocket_exception with just a string message and no error code.
Definition: ws_client.h:210
Websocket client configuration class, used to set the possible configuration options used to create a...
Definition: ws_client.h:80
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:176
pplx::task< void > close()
Closes a websocket client connection, sends a close frame to the server and waits for a close message...
Definition: ws_client.h:572
const utility::string_t & host() const
Get the host component of the URI as an encoded string.
Definition: base_uri.h:298
Websocket client class, used to maintain a connection to a remote host for an extended session...
Definition: ws_client.h:413
void set_close_handler(const std::function< void(websocket_close_status close_status, const utility::string_t &reason, const std::error_code &error)> &handler)
Set the closed handler for notification of client websocket closing event.
Definition: ws_client.h:596
const web::credentials & credentials() const
Get the client credentials
Definition: ws_client.h:111
websocket_exception(int errorCode, const utility::string_t &whatArg)
Creates a websocket_exception from a error code using the current platform error category.
Definition: ws_client.h:237
const websocket_client_config & config() const
Gets the websocket client config object.
Definition: ws_client.h:614
Represents a websocket error. This class holds an error message and an optional error code...
Definition: ws_client.h:202
pplx::task< void > close()
Closes a websocket client connection, sends a close frame to the server and waits for a close message...
Definition: ws_client.h:478
websocket_client(websocket_client_config config)
Creates a new websocket_client.
Definition: ws_client.h:427
void set_message_handler(const std::function< void(const websocket_incoming_message &msg)> &handler)
Set the received handler for notification of client websocket messages.
Definition: ws_client.h:563
Definition: ws_client.h:376
The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed as...
Definition: pplxtasks.h:4173
Websocket client class, used to maintain a connection to a remote host for an extended session...
Definition: ws_client.h:520
const web::uri & uri() const
Gets the websocket client URI.
Definition: ws_client.h:498
websocket_exception(std::error_code code, const utility::string_t &whatArg)
Creates a websocket_exception from a error code and string message to use as the what() argument...
Definition: ws_client.h:280
Represents an outgoing websocket message
Definition: ws_msg.h:72
const utility::string_t & fragment() const
Get the fragment component of the URI as an encoded string.
Definition: base_uri.h:322
_ASYNCRTIMP void add_subprotocol(const ::utility::string_t &name)
Adds a subprotocol to the request headers.
websocket_exception(int errorCode, const std::error_category &cat)
Creates a websocket_exception from a error code and category. The message of the error code will be u...
Definition: ws_client.h:270
_ASYNCRTIMP websocket_callback_client()
Creates a new websocket_callback_client.
websocket_exception(int errorCode)
Creates a websocket_exception from a error code using the current platform error category. The message of the error code will be used as the what() string message.
Definition: ws_client.h:226
pplx::task< void > send(websocket_outgoing_message msg)
Sends a websocket message to the server .
Definition: ws_client.h:551
const web::uri & uri() const
Gets the websocket client URI.
Definition: ws_client.h:605
const utility::string_t & scheme() const
Get the scheme component of the URI as an encoded string.
Definition: base_uri.h:286
Various utilities for string conversions and date and time manipulation.
Definition: asyncrt_utils.h:50
pplx::task< void > close(websocket_close_status close_status, const utility::string_t &close_reason=_XPLATSTR(""))
Closes a websocket client connection, sends a close frame to the server and waits for a close message...
Definition: ws_client.h:489