-
Notifications
You must be signed in to change notification settings - Fork 126
/
websocketpp_websocket.cpp
971 lines (848 loc) · 34 KB
/
websocketpp_websocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
// Copyright (c) Microsoft Corporation
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "pch.h"
#if !HC_NOWEBSOCKETS
#include "websocketpp_websocket.h"
#include "uri.h"
#include "x509_cert_utilities.hpp"
#if HC_PLATFORM_IS_APPLE
#include "Apple/utils_apple.h"
#endif
// Force websocketpp to use C++ std::error_code instead of Boost.
#define _WEBSOCKETPP_CPP11_SYSTEM_ERROR_
#ifdef _WIN32
#pragma warning( push )
#pragma warning( disable : 4100 4127 4512 4996 4701 4267 )
#define _WEBSOCKETPP_CPP11_STL_
#define _WEBSOCKETPP_CONSTEXPR_TOKEN_
#define _SCL_SECURE_NO_WARNINGS
#if (_MSC_VER >= 1900)
#define ASIO_ERROR_CATEGORY_NOEXCEPT noexcept(true)
#endif // (_MSC_VER >= 1900)
#elif defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
#endif
#pragma warning(disable: 4244)
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#if HC_PLATFORM == HC_PLATFORM_ANDROID
#include "../HTTP/Android/android_platform_context.h"
#endif
#if defined(_WIN32)
#pragma warning( pop )
#elif defined(__clang__)
#pragma clang diagnostic pop
#endif
#define SUB_PROTOCOL_HEADER "Sec-WebSocket-Protocol"
#define WSPP_PING_INTERVAL_MS 1000
#define WSPP_SHUTDOWN_TIMEOUT_MS 5000
using namespace xbox::httpclient;
struct websocket_outgoing_message
{
XAsyncBlock* async{ nullptr };
http_internal_string payload;
http_internal_vector<uint8_t> payloadBinary;
websocketpp::lib::error_code error;
uint64_t id{ 0 };
};
struct wspp_websocket_impl : public hc_websocket_impl, public std::enable_shared_from_this<wspp_websocket_impl>
{
private:
enum State {
CONNECTING,
CONNECTED,
DISCONNECTING,
DISCONNECTED
};
public:
wspp_websocket_impl(
HCWebsocketHandle hcHandle,
const char* uri,
const char* subprotocol
) :
m_hcWebsocketHandle{ hcHandle },
m_uri{ uri },
m_subprotocol{ subprotocol }
{
}
~wspp_websocket_impl()
{
// Because we pass shared pointers to wspp_client handlers we should never blow up while we are connected
ASSERT(m_state == DISCONNECTED);
if (m_backgroundQueue)
{
XTaskQueueCloseHandle(m_backgroundQueue);
}
}
HRESULT connect(XAsyncBlock* async)
{
if (m_uri.Scheme() == "wss")
{
m_client = std::unique_ptr<websocketpp_client_base>(new websocketpp_tls_client());
auto sharedThis{ shared_from_this() };
// Options specific to TLS client.
auto &client = m_client->client<websocketpp::config::asio_tls_client>();
client.set_tls_init_handler([sharedThis](websocketpp::connection_hdl)
{
auto sslContext = websocketpp::lib::shared_ptr<asio::ssl::context>(new asio::ssl::context(asio::ssl::context::sslv23));
sslContext->set_default_verify_paths();
sslContext->set_options(asio::ssl::context::default_workarounds);
sslContext->set_verify_mode(asio::ssl::context::verify_peer);
sharedThis->m_opensslFailed = false;
sslContext->set_verify_callback([sharedThis](bool preverified, asio::ssl::verify_context &verifyCtx)
{
// allow to use proxies that decrypt https for debugging
if (sharedThis->m_hcWebsocketHandle->websocket->ProxyDecryptsHttps())
{
return true;
}
// On OS X, iOS, and Android, OpenSSL doesn't have access to where the OS
// stores keychains. If OpenSSL fails we will doing verification at the
// end using the whole certificate chain so wait until the 'leaf' cert.
// For now return true so OpenSSL continues down the certificate chain.
if (!preverified)
{
sharedThis->m_opensslFailed = true;
}
if (sharedThis->m_opensslFailed)
{
return xbox::httpclient::verify_cert_chain_platform_specific(verifyCtx, sharedThis->m_uri.Host());
}
asio::ssl::rfc2818_verification rfc2818(sharedThis->m_uri.Host().data());
return rfc2818(preverified, verifyCtx);
});
// OpenSSL stores some per thread state that never will be cleaned up until
// the dll is unloaded. If static linking, like we do, the state isn't cleaned up
// at all and will be reported as leaks.
// See http://www.openssl.org/support/faq.html#PROG13
// This is necessary here because it is called on the user's thread calling connect(...)
// eventually through websocketpp::client::get_connection(...)
#if HC_PLATFORM == HC_PLATFORM_ANDROID || HC_PLATFORM_IS_APPLE || HC_PLATFORM == HC_PLATFORM_LINUX
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
ERR_remove_thread_state(nullptr);
#pragma clang diagnostic pop
#else
ERR_remove_thread_state(nullptr);
#endif // HC_ANDROID_API || HC_PLATFORM_IS_APPLE || HC_PLATFORM_LINUX
return sslContext;
});
// Options specific to underlying socket.
client.set_socket_init_handler([sharedThis](websocketpp::connection_hdl, asio::ssl::stream<asio::ip::tcp::socket> &ssl_stream)
{
// If user specified server name is empty default to use URI host name.
SSL_set_tlsext_host_name(ssl_stream.native_handle(), sharedThis->m_uri.Host().data());
});
return connect_impl<websocketpp::config::asio_tls_client>(async);
}
else
{
m_client = std::unique_ptr<websocketpp_client_base>(new websocketpp_client());
return connect_impl<websocketpp::config::asio_client>(async);
}
}
HRESULT send(XAsyncBlock* async, const char* payloadPtr)
{
if (payloadPtr == nullptr)
{
return E_INVALIDARG;
}
if (m_state != CONNECTED)
{
HC_TRACE_ERROR(WEBSOCKET, "Client not connected");
return E_UNEXPECTED;
}
auto httpSingleton = get_http_singleton();
if (httpSingleton == nullptr)
{
return E_HC_NOT_INITIALISED;
}
http_internal_string payload(payloadPtr);
if (payload.length() == 0)
{
return E_INVALIDARG;
}
websocket_outgoing_message message;
message.async = async;
message.payload = std::move(payload);
message.id = ++httpSingleton->m_lastId;
{
// Only actually have to take the lock if touching the queue.
std::lock_guard<std::recursive_mutex> lock(m_outgoingMessageQueueLock);
m_outgoingMessageQueue.push(std::move(message));
}
if (++m_numSends == 1) // No sends in progress
{
// Start sending the message
return send_msg();
}
return S_OK;
}
HRESULT sendBinary(XAsyncBlock* async, const uint8_t* payloadBytes, uint32_t payloadSize)
{
if (payloadBytes == nullptr)
{
return E_INVALIDARG;
}
if (m_state != CONNECTED)
{
HC_TRACE_ERROR(WEBSOCKET, "Client not connected");
return E_UNEXPECTED;
}
auto httpSingleton = get_http_singleton();
if (httpSingleton == nullptr)
{
return E_HC_NOT_INITIALISED;
}
if (payloadSize == 0)
{
return E_INVALIDARG;
}
websocket_outgoing_message message;
message.async = async;
message.payloadBinary.assign(payloadBytes, payloadBytes + payloadSize);
message.id = ++httpSingleton->m_lastId;
{
// Only actually have to take the lock if touching the queue.
std::lock_guard<std::recursive_mutex> lock(m_outgoingMessageQueueLock);
m_outgoingMessageQueue.push(std::move(message));
}
if (++m_numSends == 1) // No sends in progress
{
// Start sending the message
return send_msg();
}
return S_OK;
}
HRESULT close()
{
return close(HCWebSocketCloseStatus::Normal);
}
HRESULT close(HCWebSocketCloseStatus status)
{
std::lock_guard<std::recursive_mutex> lock(m_wsppClientLock);
if (m_state == CONNECTED)
{
m_state = DISCONNECTING;
websocketpp::lib::error_code ec{};
if (m_client->is_tls_client())
{
auto &client = m_client->client<websocketpp::config::asio_tls_client>();
client.close(m_con, static_cast<websocketpp::close::status::value>(status), std::string(), ec);
}
else
{
auto &client = m_client->client<websocketpp::config::asio_client>();
client.close(m_con, static_cast<websocketpp::close::status::value>(status), std::string(), ec);
}
return ec ? E_FAIL : S_OK;
}
else
{
return E_UNEXPECTED;
}
}
private:
template<typename WebsocketConfigType>
HRESULT connect_impl(XAsyncBlock* async)
{
if (async->queue)
{
XTaskQueuePortHandle worker{ nullptr };
RETURN_IF_FAILED(XTaskQueueGetPort(async->queue, XTaskQueuePort::Work, &worker));
RETURN_IF_FAILED(XTaskQueueCreateComposite(worker, worker, &m_backgroundQueue));
}
auto &client = m_client->client<WebsocketConfigType>();
client.clear_access_channels(websocketpp::log::alevel::all);
client.clear_error_channels(websocketpp::log::alevel::all);
client.init_asio();
client.start_perpetual();
auto sharedThis { shared_from_this() };
ASSERT(m_state == DISCONNECTED);
client.set_open_handler([sharedThis, async](websocketpp::connection_hdl)
{
ASSERT(sharedThis->m_state == CONNECTING);
sharedThis->m_state = CONNECTED;
sharedThis->set_connection_error<WebsocketConfigType>();
sharedThis->send_ping();
XAsyncComplete(async, S_OK, sizeof(WebSocketCompletionResult));
});
client.set_fail_handler([sharedThis, async](websocketpp::connection_hdl)
{
ASSERT(sharedThis->m_state == CONNECTING);
sharedThis->set_connection_error<WebsocketConfigType>();
sharedThis->shutdown_wspp_impl<WebsocketConfigType>(
[
sharedThis,
async
]
{
XAsyncComplete(async, S_OK, sizeof(WebSocketCompletionResult));
});
});
client.set_message_handler([sharedThis](websocketpp::connection_hdl, const websocketpp::config::asio_client::message_type::ptr &msg)
{
HCWebSocketMessageFunction messageFunc{ nullptr };
HCWebSocketBinaryMessageFunction binaryMessageFunc{ nullptr };
void* context{ nullptr };
auto hr = HCWebSocketGetEventFunctions(sharedThis->m_hcWebsocketHandle, &messageFunc, &binaryMessageFunc, nullptr, &context);
if (SUCCEEDED(hr))
{
ASSERT(messageFunc && binaryMessageFunc);
// TODO: hook up HCWebSocketCloseEventFunction handler upon unexpected disconnect
// TODO: verify auto disconnect when closing client's websocket handle
if (msg->get_opcode() == websocketpp::frame::opcode::text)
{
auto& payload = msg->get_raw_payload();
messageFunc(sharedThis->m_hcWebsocketHandle, payload.data(), context);
}
else if (msg->get_opcode() == websocketpp::frame::opcode::binary)
{
auto& payload = msg->get_raw_payload();
binaryMessageFunc(sharedThis->m_hcWebsocketHandle, (uint8_t*)payload.data(), (uint32_t)payload.size(), context);
}
}
});
client.set_close_handler([sharedThis](websocketpp::connection_hdl)
{
ASSERT(sharedThis->m_state == CONNECTED || sharedThis->m_state == DISCONNECTING);
sharedThis->shutdown_wspp_impl<WebsocketConfigType>([sharedThis]()
{
HCWebSocketCloseEventFunction closeFunc{ nullptr };
void* context{ nullptr };
HCWebSocketGetEventFunctions(sharedThis->m_hcWebsocketHandle, nullptr, nullptr, &closeFunc, &context);
if (closeFunc)
{
closeFunc(sharedThis->m_hcWebsocketHandle, static_cast<HCWebSocketCloseStatus>(sharedThis->m_closeCode), context);
}
});
});
// Set User Agent specified by the user. This needs to happen before any connection is created
const auto& headers = m_hcWebsocketHandle->websocket->Headers();
auto user_agent_it = headers.find(websocketpp::user_agent);
if (user_agent_it != headers.end())
{
client.set_user_agent(user_agent_it->second.data());
}
// Get the connection handle to save for later, have to create temporary
// because type erasure occurs with connection_hdl.
websocketpp::lib::error_code ec;
auto con = client.get_connection(m_uri.FullPath().data(), ec);
m_con = con;
if (ec.value() != 0)
{
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: wspp get_connection failed", TO_ULL(m_hcWebsocketHandle->websocket->id));
return E_FAIL;
}
// Add any request headers specified by the user.
for (const auto & header : headers)
{
// Subprotocols are handled separately below
if (str_icmp(header.first, SUB_PROTOCOL_HEADER) != 0)
{
con->append_header(header.first.data(), header.second.data());
}
}
// Add any specified subprotocols.
if (!m_subprotocol.empty())
{
con->add_subprotocol(m_subprotocol.data(), ec);
if (ec.value())
{
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: add_subprotocol failed", TO_ULL(m_hcWebsocketHandle->websocket->id));
return E_FAIL;
}
}
// Setup proxy options.
if (!m_hcWebsocketHandle->websocket->ProxyUri().empty())
{
con->set_proxy(m_hcWebsocketHandle->websocket->ProxyUri().data(), ec);
if (ec)
{
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: wspp set_proxy failed", TO_ULL(m_hcWebsocketHandle->websocket->id));
return E_FAIL;
}
}
#if HC_PLATFORM_IS_MICROSOFT
else
{
// On windows platforms use the IE proxy if the user didn't specify one
Uri proxyUri;
auto proxyType = get_ie_proxy_info(proxy_protocol::websocket, proxyUri);
if (proxyType == proxy_type::named_proxy)
{
con->set_proxy(proxyUri.FullPath().data(), ec);
if (ec)
{
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: wspp set_proxy failed", TO_ULL(m_hcWebsocketHandle->websocket->id));
return E_FAIL;
}
}
}
#elif HC_PLATFORM_IS_APPLE
else
{
Uri proxyUri;
std::string username;
std::string password;
if (getSystemProxyForUri(m_uri, &proxyUri, &username, &password))
{
con->set_proxy(proxyUri.FullPath().data(), ec);
if (ec)
{
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: wspp set_proxy failed", TO_ULL(m_hcWebsocketHandle->websocket->id));
return E_FAIL;
}
if (!username.empty() && !password.empty())
{
con->set_proxy_basic_auth(username, password, ec);
if (ec)
{
HC_TRACE_ERROR(WEBSOCKET, "Websocket [ID %llu]: wspp set_proxy_basic_auth failed", TO_ULL(m_hcWebsocketHandle->websocket->id));
return E_FAIL;
}
}
}
}
#endif
// Initialize the 'connect' XAsyncBlock here, but the actually work will happen on the ASIO background thread below
auto hr = XAsyncBegin(async, shared_ptr_cache::store(shared_from_this()), (void*)HCWebSocketConnectAsync, __FUNCTION__,
[](XAsyncOp op, const XAsyncProviderData* data)
{
if (op == XAsyncOp::GetResult)
{
auto context = shared_ptr_cache::fetch<wspp_websocket_impl>(data->context);
if (context == nullptr)
{
return E_HC_NOT_INITIALISED;
}
auto result = reinterpret_cast<WebSocketCompletionResult*>(data->buffer);
result->websocket = context->m_hcWebsocketHandle;
result->platformErrorCode = context->m_connectError.value();
result->errorCode = context->m_connectError ? E_FAIL : S_OK;
}
else if (op == XAsyncOp::Cleanup)
{
shared_ptr_cache::remove(data->context);
}
return S_OK;
});
if (SUCCEEDED(hr))
{
m_state = CONNECTING;
client.connect(con);
try
{
struct client_context
{
client_context(websocketpp::client<WebsocketConfigType>& _client) : client(_client) {}
websocketpp::client<WebsocketConfigType>& client;
};
auto context = http_allocate_shared<client_context>(client);
m_websocketThread = std::thread([context, id{ m_hcWebsocketHandle->websocket->id }]()
{
HC_TRACE_INFORMATION(WEBSOCKET, "id=%u Wspp client work thread starting", id);
#if HC_PLATFORM == HC_PLATFORM_ANDROID
JavaVM* javaVm = nullptr;
{
// Allow our singleton to go out of scope quickly once we're done with it
auto httpSingleton = xbox::httpclient::get_http_singleton();
if (httpSingleton)
{
auto platformContext = httpSingleton->m_performEnv->androidPlatformContext;
javaVm = platformContext->GetJavaVm();
}
}
if (javaVm == nullptr)
{
HC_TRACE_ERROR(HTTPCLIENT, "javaVm is null");
throw std::runtime_error("JavaVm is null");
}
JNIEnv* jniEnv = nullptr;
if (javaVm->AttachCurrentThread(&jniEnv, nullptr) != 0)
{
assert(false);
}
#endif
try
{
context->client.run();
}
catch (...)
{
HC_TRACE_ERROR(WEBSOCKET, "Caught exception in wspp client::run!");
}
// OpenSSL stores some per thread state that never will be cleaned up until
// the dll is unloaded. If static linking, like we do, the state isn't cleaned up
// at all and will be reported as leaks.
// See http://www.openssl.org/support/faq.html#PROG13
#if HC_PLATFORM == HC_PLATFORM_ANDROID || HC_PLATFORM_IS_APPLE || HC_PLATFORM == HC_PLATFORM_LINUX
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
ERR_remove_thread_state(nullptr);
#pragma clang diagnostic pop
#if HC_PLATFORM == HC_PLATFORM_ANDROID
javaVm->DetachCurrentThread();
#endif // HC_PLATFORM_ANDROID
#else
ERR_remove_thread_state(nullptr);
#endif // HC_PLATFORM_ANDROID || HC_PLATFORM_IS_APPLE || HC_PLATFORM_LINUX
HC_TRACE_INFORMATION(WEBSOCKET, "id=%u Wspp client work thread end", id);
});
hr = S_OK;
}
catch (std::system_error err)
{
HC_TRACE_ERROR(WEBSOCKET, "Websocket: couldn't create background websocket thread (%d)", err.code().value());
hr = E_FAIL;
}
}
return hr;
}
struct send_msg_context
{
std::shared_ptr<wspp_websocket_impl> pThis;
websocket_outgoing_message message;
};
HRESULT send_msg_do_work(websocket_outgoing_message& message)
{
HRESULT hr = S_OK;
try
{
std::lock_guard<std::recursive_mutex> lock(m_wsppClientLock);
if (m_state != State::CONNECTED)
{
hr = E_UNEXPECTED;
}
else
{
if (message.payload.empty())
{
if (message.payloadBinary.size() > 0)
{
if (m_client->is_tls_client())
{
m_client->client<websocketpp::config::asio_tls_client>().send(m_con, message.payloadBinary.data(), message.payloadBinary.size(), websocketpp::frame::opcode::binary, message.error);
}
else
{
m_client->client<websocketpp::config::asio_client>().send(m_con, message.payloadBinary.data(), message.payloadBinary.size(), websocketpp::frame::opcode::binary, message.error);
}
}
else
{
hr = E_FAIL;
}
}
else
{
if (m_client->is_tls_client())
{
m_client->client<websocketpp::config::asio_tls_client>().send(m_con, message.payload.data(), message.payload.length(), websocketpp::frame::opcode::text, message.error);
}
else
{
m_client->client<websocketpp::config::asio_client>().send(m_con, message.payload.data(), message.payload.length(), websocketpp::frame::opcode::text, message.error);
}
}
}
if (message.error.value() != 0)
{
hr = E_FAIL;
}
XAsyncComplete(message.async, hr, sizeof(WebSocketCompletionResult));
if (--m_numSends > 0)
{
hr = send_msg();
}
}
catch (...)
{
hr = E_FAIL;
XAsyncComplete(message.async, hr, sizeof(WebSocketCompletionResult));
}
return hr;
}
// Pull the next message from the queue and send it
HRESULT send_msg()
{
auto sendContext = http_allocate_shared<send_msg_context>();
sendContext->pThis = shared_from_this();
{
std::lock_guard<std::recursive_mutex> lock(m_outgoingMessageQueueLock);
ASSERT(!m_outgoingMessageQueue.empty());
sendContext->message = std::move(m_outgoingMessageQueue.front());
m_outgoingMessageQueue.pop();
}
auto rawSendContext = shared_ptr_cache::store(sendContext);
if (rawSendContext == nullptr)
{
XAsyncComplete(sendContext->message.async, E_HC_NOT_INITIALISED, 0);
return E_HC_NOT_INITIALISED;
}
auto hr = XAsyncBegin(sendContext->message.async, rawSendContext, (void*)HCWebSocketSendMessageAsync, __FUNCTION__,
[](XAsyncOp op, const XAsyncProviderData* data)
{
auto httpSingleton = get_http_singleton();
if (nullptr == httpSingleton)
{
return E_HC_NOT_INITIALISED;
}
WebSocketCompletionResult* result;
switch (op)
{
case XAsyncOp::DoWork:
{
auto context = shared_ptr_cache::fetch<send_msg_context>(data->context);
if (context == nullptr)
{
return E_HC_NOT_INITIALISED;
}
return context->pThis->send_msg_do_work(context->message);
}
case XAsyncOp::GetResult:
{
auto context = shared_ptr_cache::fetch<send_msg_context>(data->context);
if (context == nullptr)
{
return E_HC_NOT_INITIALISED;
}
result = reinterpret_cast<WebSocketCompletionResult*>(data->buffer);
result->websocket = context->pThis->m_hcWebsocketHandle;
result->platformErrorCode = context->message.error.value();
result->errorCode = XAsyncGetStatus(data->async, false);
return S_OK;
}
case XAsyncOp::Cleanup:
{
shared_ptr_cache::remove(data->context);
return S_OK;
}
default:
break;
}
return S_OK;
});
if (SUCCEEDED(hr))
{
hr = XAsyncSchedule(sendContext->message.async, 0);
}
return hr;
}
void send_ping()
{
// By design, wspp doesn't raise any sort of event when the client's connection
// is terminated (i.e. by disconnecting the network cable). Sending periodic ping
// allows us to detect this situation. See https://github.com/zaphoyd/websocketpp/issues/695.
RunAsync(
[
weakThis = std::weak_ptr<wspp_websocket_impl>{ shared_from_this() }
]
{
auto sharedThis{ weakThis.lock() };
if (sharedThis)
{
try
{
std::lock_guard<std::recursive_mutex> lock{ sharedThis->m_wsppClientLock };
if (sharedThis->m_state == CONNECTED)
{
if (sharedThis->m_client->is_tls_client())
{
sharedThis->m_client->client<websocketpp::config::asio_tls_client>().ping(sharedThis->m_con, std::string{});
}
else
{
sharedThis->m_client->client<websocketpp::config::asio_client>().ping(sharedThis->m_con, std::string{});
}
sharedThis->send_ping();
}
}
catch (...)
{
HC_TRACE_ERROR(WEBSOCKET, "Websocket: caught exception in ping!");
}
}
},
m_backgroundQueue,
WSPP_PING_INTERVAL_MS
);
}
template <typename WebsocketConfigType>
void shutdown_wspp_impl(std::function<void()> shutdownCompleteCallback)
{
auto &client = m_client->client<WebsocketConfigType>();
const auto &connection = client.get_con_from_hdl(m_con);
m_closeCode = connection->get_local_close_code();
client.stop_perpetual();
// Yield and wait for background thread to finish
RunAsync(
[
sharedThis{ shared_from_this() },
shutdownCompleteCallback
]
{
// Wait for background thread to finish
if (sharedThis->m_websocketThread.joinable())
{
auto future = std::async(std::launch::async, &std::thread::join, &sharedThis->m_websocketThread);
if (future.wait_for(std::chrono::milliseconds(WSPP_SHUTDOWN_TIMEOUT_MS)) == std::future_status::timeout)
{
HC_TRACE_WARNING(WEBSOCKET, "Warning: WSPP client thread didn't complete execution within the expected timeout. Force stopping processing loop.");
sharedThis->m_client->client<WebsocketConfigType>().stop();
}
}
{
std::lock_guard<std::recursive_mutex> lock{ sharedThis->m_wsppClientLock };
// Delete client to make sure Websocketpp cleans up all Boost.Asio portions.
sharedThis->m_client.reset();
sharedThis->m_state = DISCONNECTED;
}
shutdownCompleteCallback();
},
m_backgroundQueue,
0
);
}
template <typename WebsocketConfigType>
inline void set_connection_error()
{
auto &client = m_client->client<WebsocketConfigType>();
const auto &connection = client.get_con_from_hdl(m_con);
m_connectError = connection->get_ec();
}
// Wrappers for the different types of websocketpp clients.
// Perform type erasure to set the websocketpp client in use at runtime
// after construction based on the URI.
struct websocketpp_client_base
{
virtual ~websocketpp_client_base() noexcept {}
template <typename WebsocketConfig>
websocketpp::client<WebsocketConfig> & client()
{
if (is_tls_client())
{
return reinterpret_cast<websocketpp::client<WebsocketConfig> &>(tls_client());
}
else
{
return reinterpret_cast<websocketpp::client<WebsocketConfig> &>(non_tls_client());
}
}
virtual websocketpp::client<websocketpp::config::asio_client> & non_tls_client()
{
throw std::bad_cast();
}
virtual websocketpp::client<websocketpp::config::asio_tls_client> & tls_client()
{
throw std::bad_cast();
}
virtual bool is_tls_client() const = 0;
};
struct websocketpp_client : websocketpp_client_base
{
websocketpp::client<websocketpp::config::asio_client> & non_tls_client() override
{
return m_client;
}
bool is_tls_client() const override { return false; }
websocketpp::client<websocketpp::config::asio_client> m_client;
};
struct websocketpp_tls_client : websocketpp_client_base
{
websocketpp::client<websocketpp::config::asio_tls_client> & tls_client() override
{
return m_client;
}
bool is_tls_client() const override { return true; }
websocketpp::client<websocketpp::config::asio_tls_client> m_client;
};
// Asio client has a long running "run" task that we need to provide a thread for
std::thread m_websocketThread;
XTaskQueueHandle m_backgroundQueue = nullptr;
websocketpp::connection_hdl m_con;
websocketpp::lib::error_code m_connectError{};
websocketpp::close::status::value m_closeCode{};
// Used to safe guard the wspp client.
std::recursive_mutex m_wsppClientLock;
std::atomic<State> m_state{ State::DISCONNECTED };
std::unique_ptr<websocketpp_client_base> m_client;
// Guards access to m_outgoing_msg_queue
std::recursive_mutex m_outgoingMessageQueueLock;
// Queue to order the sends
http_internal_queue<websocket_outgoing_message> m_outgoingMessageQueue;
// Number of sends in progress and queued up.
std::atomic<int> m_numSends{ 0 };
// Used to track if any of the OpenSSL server certificate verifications
// failed. This can safely be tracked at the client level since connections
// only happen once for each client.
bool m_opensslFailed{ false };
HCWebsocketHandle m_hcWebsocketHandle{ nullptr };
Uri m_uri;
http_internal_string m_subprotocol;
};
NAMESPACE_XBOX_HTTP_CLIENT_BEGIN
HRESULT WebSocketppProvider::ConnectAsync(
String const& uri,
String const& subprotocol,
HCWebsocketHandle websocketHandle,
XAsyncBlock* async
) noexcept
{
auto wsppSocket{ std::dynamic_pointer_cast<wspp_websocket_impl>(websocketHandle->websocket->impl) };
if (!wsppSocket)
{
wsppSocket = http_allocate_shared<wspp_websocket_impl>(websocketHandle, uri.data(), subprotocol.data());
websocketHandle->websocket->impl = wsppSocket;
}
return wsppSocket->connect(async);
}
HRESULT WebSocketppProvider::SendAsync(
HCWebsocketHandle websocketHandle,
const char* message,
XAsyncBlock* async
) noexcept
{
std::shared_ptr<wspp_websocket_impl> wsppSocket = std::dynamic_pointer_cast<wspp_websocket_impl>(websocketHandle->websocket->impl);
if (wsppSocket == nullptr)
{
return E_UNEXPECTED;
}
return wsppSocket->send(async, message);
}
HRESULT WebSocketppProvider::SendBinaryAsync(
HCWebsocketHandle websocketHandle,
const uint8_t* payloadBytes,
uint32_t payloadSize,
XAsyncBlock* async
) noexcept
{
std::shared_ptr<wspp_websocket_impl> wsppSocket = std::dynamic_pointer_cast<wspp_websocket_impl>(websocketHandle->websocket->impl);
if (wsppSocket == nullptr)
{
return E_UNEXPECTED;
}
return wsppSocket->sendBinary(async, payloadBytes, payloadSize);
}
HRESULT WebSocketppProvider::Disconnect(
HCWebsocketHandle websocketHandle,
HCWebSocketCloseStatus closeStatus
) noexcept
{
if (websocketHandle == nullptr)
{
return E_INVALIDARG;
}
std::shared_ptr<wspp_websocket_impl> wsppSocket = std::dynamic_pointer_cast<wspp_websocket_impl>(websocketHandle->websocket->impl);
if (wsppSocket == nullptr)
{
return E_UNEXPECTED;
}
HC_TRACE_INFORMATION(WEBSOCKET, "Websocket [ID %llu]: disconnecting", TO_ULL(websocketHandle->websocket->id));
return wsppSocket->close(closeStatus);
}
NAMESPACE_XBOX_HTTP_CLIENT_END
#endif // !HC_NOWEBSOCKETS