Skip to content

Commit 95e5918

Browse files
committed
Refactoring websocketpp client to prepare for tls support.
1 parent 3e7ac07 commit 95e5918

File tree

6 files changed

+248
-191
lines changed

6 files changed

+248
-191
lines changed

Release/include/cpprest/ws_client.h

Lines changed: 89 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ namespace pplx = Concurrency;
5252
#include "cpprest/asyncrt_utils.h"
5353
#include "cpprest/ws_msg.h"
5454

55-
namespace web
55+
namespace web
5656
{
5757
/// WebSocket client is currently in beta.
58-
namespace experimental
58+
namespace experimental
5959
{
6060
namespace websockets
6161
{
@@ -85,11 +85,56 @@ class websocket_client_config
8585
public:
8686

8787
/// <summary>
88-
/// Creates a websocket client configuration with default settings.
88+
/// Creates a websocket client configuration with default settings.
8989
/// </summary>
90-
websocket_client_config()
90+
websocket_client_config() {}
91+
92+
/// <summary>
93+
/// Copy constructor.
94+
/// </summary>
95+
websocket_client_config(const websocket_client_config &other) :
96+
m_proxy(other.m_proxy),
97+
m_credentials(other.m_credentials),
98+
m_headers(other.m_headers)
9199
{}
92100

101+
/// <summary>
102+
/// Move constructor.
103+
/// </summary>
104+
websocket_client_config(websocket_client_config &&other) :
105+
m_proxy(std::move(other.m_proxy)),
106+
m_credentials(std::move(other.m_credentials)),
107+
m_headers(std::move(other.m_headers))
108+
{}
109+
110+
/// <summary>
111+
/// Assignment operator.
112+
/// </summary>
113+
websocket_client_config &operator=(const websocket_client_config &other)
114+
{
115+
if (this != &other)
116+
{
117+
m_proxy = other.m_proxy;
118+
m_credentials = other.m_credentials;
119+
m_headers = other.m_headers;
120+
}
121+
return *this;
122+
}
123+
124+
/// <summary>
125+
/// Move assignment operator.
126+
/// </summary>
127+
websocket_client_config &operator=(websocket_client_config &&other)
128+
{
129+
if (this != &other)
130+
{
131+
m_proxy = std::move(other.m_proxy);
132+
m_credentials = std::move(other.m_credentials);
133+
m_headers = std::move(other.m_headers);
134+
}
135+
return *this;
136+
}
137+
93138
/// <summary>
94139
/// Get the web proxy object
95140
/// </summary>
@@ -103,9 +148,9 @@ class websocket_client_config
103148
/// Set the web proxy object
104149
/// </summary>
105150
/// <param name="proxy">The web proxy object.</param>
106-
void set_proxy(web_proxy proxy)
151+
void set_proxy(const web_proxy &proxy)
107152
{
108-
m_proxy = std::move(proxy);
153+
m_proxy = proxy;
109154
}
110155

111156
/// <summary>
@@ -121,9 +166,9 @@ class websocket_client_config
121166
/// Set the client credentials
122167
/// </summary>
123168
/// <param name="cred">The client credentials.</param>
124-
void set_credentials(web::credentials cred)
169+
void set_credentials(const web::credentials &cred)
125170
{
126-
m_credentials = std::move(cred);
171+
m_credentials = cred;
127172
}
128173

129174
/// <summary>
@@ -152,8 +197,8 @@ class websocket_client_config
152197
/// Gets list of the specified subprotocols.
153198
/// </summary>
154199
/// <returns>Vector of all the subprotocols </returns>
155-
/// <remarks>If you want all the subprotocols in a comma separated string
156-
/// they can be directly directly looked up in the headers using 'Sec-WebSocket-Protocol'.</remarks>
200+
/// <remarks>If you want all the subprotocols in a comma separated string
201+
/// they can be directly looked up in the headers using 'Sec-WebSocket-Protocol'.</remarks>
157202
_ASYNCRTIMP std::vector<::utility::string_t> subprotocols() const;
158203

159204
private:
@@ -258,52 +303,40 @@ class websocket_exception : public std::exception
258303

259304
namespace details
260305
{
261-
class winrt_client;
262-
class ws_desktop_client;
263306

264307
// Interface to be implemented by the websocket client implementations.
265308
class _websocket_client_impl
266309
{
267310
public:
311+
312+
_websocket_client_impl(websocket_client_config config) :
313+
m_config(std::move(config)) {}
314+
268315
virtual ~_websocket_client_impl() _noexcept {}
269316

270317
virtual pplx::task<void> connect() = 0;
271318

272-
virtual pplx::task<void> send(websocket_outgoing_message msg) = 0;
319+
virtual pplx::task<void> send(websocket_outgoing_message &msg) = 0;
273320

274-
virtual pplx::task<websocket_incoming_message> receive() = 0;
321+
virtual pplx::task<websocket_incoming_message> receive() = 0;
275322

276323
virtual pplx::task<void> close() = 0;
277324

278325
virtual pplx::task<void> close(websocket_close_status close_status, const utility::string_t &close_reason=_XPLATSTR("")) = 0;
279326

280-
/// <summary>
281-
/// Gets the base uri
282-
/// </summary>
283-
/// <returns>
284-
/// A base uri initialized in constructor
285-
/// </return>
286327
const web::uri& uri() const
287328
{
288329
return m_uri;
289330
}
290331

291-
/// <summary>
292-
/// Set the base uri.
293-
/// </summary>
294-
/// <param name="uri"> The user specified uri. </param>
295-
void set_uri(web::uri uri)
332+
void set_uri(const web::uri &uri)
296333
{
297-
m_uri = std::move(uri);
334+
m_uri = uri;
298335
}
299336

300-
/// <summary>
301-
/// Get client configuration object
302-
/// </summary>
303-
/// <returns>A reference to the client configuration object.</returns>
304337
const websocket_client_config& config() const
305338
{
306-
return m_client_config;
339+
return m_config;
307340
}
308341

309342
static void verify_uri(const web::uri& uri)
@@ -328,16 +361,9 @@ class _websocket_client_impl
328361
}
329362
}
330363

331-
332364
protected:
333365
web::uri m_uri;
334-
335-
_websocket_client_impl(websocket_client_config client_config)
336-
: m_client_config(std::move(client_config))
337-
{
338-
}
339-
340-
websocket_client_config m_client_config;
366+
websocket_client_config m_config;
341367
};
342368
}
343369

@@ -350,48 +376,58 @@ class websocket_client
350376
/// <summary>
351377
/// Creates a new websocket_client.
352378
/// </summary>
353-
_ASYNCRTIMP websocket_client();
379+
websocket_client() : websocket_client(websocket_client_config()) {}
354380

355381
/// <summary>
356382
/// Creates a new websocket_client.
357383
/// </summary>
358-
/// <param name="client_config">The client configuration object containing the possible configuration options to intitialize the <c>websocket_client</c>. </param>
384+
/// <param name="client_config">The client configuration object containing the possible configuration options to initialize the <c>websocket_client</c>. </param>
359385
_ASYNCRTIMP websocket_client(websocket_client_config client_config);
360386

361387
/// <summary>
362388
/// Destructor
363389
/// </summary>
364-
~websocket_client() { }
390+
~websocket_client() _noexcept {}
365391

366392
/// <summary>
367-
/// Connects to the remote network destination. The connect method initiates the websocket handshake with the
393+
/// Connects to the remote network destination. The connect method initiates the websocket handshake with the
368394
/// remote network destination, takes care of the protocol upgrade request.
369395
/// </summary>
370396
/// <param name="uri">The uri address to connect. </param>
371397
/// <returns>An asynchronous operation that is completed once the client has successfully connected to the websocket server.</returns>
372-
pplx::task<void> connect(web::uri uri)
373-
{
374-
m_client->set_uri(std::move(uri));
375-
return m_client->connect();
398+
pplx::task<void> connect(const web::uri &uri)
399+
{
400+
m_client->verify_uri(uri);
401+
m_client->set_uri(uri);
402+
return m_client->connect();
376403
}
377404

378405
/// <summary>
379406
/// Sends a websocket message to the server .
380407
/// </summary>
381408
/// <returns>An asynchronous operation that is completed once the message is sent.</returns>
382-
pplx::task<void> send(websocket_outgoing_message msg) { return m_client->send(msg); }
409+
pplx::task<void> send(websocket_outgoing_message msg)
410+
{
411+
return m_client->send(msg);
412+
}
383413

384414
/// <summary>
385415
/// Receive a websocket message.
386416
/// </summary>
387417
/// <returns>An asynchronous operation that is completed when a message has been received by the client endpoint.</returns>
388-
pplx::task<websocket_incoming_message> receive() { return m_client->receive(); }
418+
pplx::task<websocket_incoming_message> receive()
419+
{
420+
return m_client->receive();
421+
}
389422

390423
/// <summary>
391424
/// Closes a websocket client connection, sends a close frame to the server and waits for a close message from the server.
392425
/// </summary>
393426
/// <returns>An asynchronous operation that is completed the connection has been successfully closed.</returns>
394-
pplx::task<void> close() { return m_client->close(); }
427+
pplx::task<void> close()
428+
{
429+
return m_client->close();
430+
}
395431

396432
/// <summary>
397433
/// Closes a websocket client connection, sends a close frame to the server and waits for a close message from the server.
@@ -400,16 +436,14 @@ class websocket_client
400436
/// <param name="close_reason">While closing an established connection, an endpoint may indicate the reason for closure.</param>
401437
/// <returns>An asynchronous operation that is completed the connection has been successfully closed.</returns>
402438
pplx::task<void> close(websocket_close_status close_status, const utility::string_t& close_reason=_XPLATSTR(""))
403-
{
404-
return m_client->close(close_status, close_reason);
439+
{
440+
return m_client->close(close_status, close_reason);
405441
}
406442

407443
/// <summary>
408444
/// Gets the websocket client URI.
409445
/// </summary>
410-
/// <returns>
411-
/// A base uri initialized in constructor
412-
/// </return>
446+
/// <returns>URI connected to.</returns>
413447
const web::uri& uri() const
414448
{
415449
return m_client->uri();
@@ -425,7 +459,6 @@ class websocket_client
425459
}
426460

427461
private:
428-
429462
std::shared_ptr<details::_websocket_client_impl> m_client;
430463
};
431464

Release/include/cpprest/ws_msg.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@
3939
#if defined(_MSC_VER) && (_MSC_VER >= 1800)
4040
#include <ppltasks.h>
4141
namespace pplx = Concurrency;
42-
#else
42+
#else
4343
#include "pplx/pplxtasks.h"
4444
#endif
4545

4646
#include "cpprest/uri.h"
4747
#include "cpprest/basic_types.h"
4848
#include "cpprest/asyncrt_utils.h"
4949

50-
namespace web
51-
{
50+
namespace web
51+
{
5252
namespace experimental
5353
{
5454
namespace websockets
@@ -60,7 +60,7 @@ namespace details
6060
{
6161
class winrt_client;
6262
class ReceiveContext;
63-
class ws_desktop_client;
63+
class wspp_client;
6464
}
6565

6666
/// <summary>
@@ -162,7 +162,7 @@ class websocket_outgoing_message
162162
/// <summary>
163163
/// Sets binary data as the message body.
164164
/// </summary>
165-
/// <param name="istream">casablanca input stream representing the body of the message.</param>
165+
/// <param name="istream">Input stream representing the body of the message.</param>
166166
/// <remarks>Upon sending, the entire stream may be buffered to determine the length.</remarks>
167167
void set_binary_message(const concurrency::streams::istream &istream)
168168
{
@@ -171,7 +171,7 @@ class websocket_outgoing_message
171171

172172
private:
173173
friend class details::winrt_client;
174-
friend class details::ws_desktop_client;
174+
friend class details::wspp_client;
175175

176176
std::shared_ptr<details::_websocket_message> _m_impl;
177177

@@ -228,7 +228,7 @@ class websocket_incoming_message
228228
/// </summary>
229229
/// <returns>String containing body of the message.</returns>
230230
_ASYNCRTIMP pplx::task<std::string> extract_string() const;
231-
231+
232232
/// <summary>
233233
/// Produces a stream which the caller may use to retrieve body from an incoming message.
234234
/// Can be used for both UTF-8 (text) and binary message types.
@@ -264,13 +264,13 @@ class websocket_incoming_message
264264

265265
private:
266266
friend class details::winrt_client;
267-
friend class details::ws_desktop_client;
267+
friend class details::wspp_client;
268268
friend class details::ReceiveContext;
269-
269+
270270
// Store message body in a container buffer backed by a string.
271271
// Allows for optimization in the string message cases.
272272
concurrency::streams::container_buffer<std::string> m_body;
273-
273+
274274
std::shared_ptr<details::_websocket_message> _m_impl;
275275
};
276276

0 commit comments

Comments
 (0)