-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathhcwebsocket.cpp
582 lines (494 loc) · 17.1 KB
/
hcwebsocket.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
// Copyright (c) Microsoft Corporation
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include "pch.h"
#include "hcwebsocket.h"
#if !HC_NOWEBSOCKETS
using namespace xbox::httpclient;
#define WEBSOCKET_RECVBUFFER_MAXSIZE_DEFAULT (1024 * 20)
HC_WEBSOCKET_OBSERVER::HC_WEBSOCKET_OBSERVER(std::shared_ptr<xbox::httpclient::WebSocket> websocket) : websocket{ std::move(websocket) }
{
}
HC_WEBSOCKET_OBSERVER::~HC_WEBSOCKET_OBSERVER()
{
HC_TRACE_INFORMATION(WEBSOCKET, __FUNCTION__);
websocket->UnregisterEventCallbacks(m_handlerToken);
}
int HC_WEBSOCKET_OBSERVER::AddRef() noexcept
{
return ++m_refCount;
}
int HC_WEBSOCKET_OBSERVER::Release() noexcept
{
int count = --m_refCount;
if (count == 0)
{
this->~HC_WEBSOCKET_OBSERVER();
http_stl_allocator<HC_WEBSOCKET_OBSERVER> alloc;
std::allocator_traits<http_stl_allocator<HC_WEBSOCKET_OBSERVER>>::deallocate(alloc, this, 1);
}
else if (count < 0)
{
assert(false);
}
return count;
}
xbox::httpclient::ObserverPtr HC_WEBSOCKET_OBSERVER::Initialize(
_In_ std::shared_ptr<xbox::httpclient::WebSocket> websocket,
_In_opt_ HCWebSocketMessageFunction messageFunc,
_In_opt_ HCWebSocketBinaryMessageFunction binaryMessageFunc,
_In_opt_ HCWebSocketBinaryMessageFragmentFunction binaryFragmentFunc,
_In_opt_ HCWebSocketCloseEventFunction closeFunc,
_In_opt_ void* callbackContext
)
{
http_stl_allocator<HC_WEBSOCKET_OBSERVER> a{};
xbox::httpclient::ObserverPtr observer{ new (a.allocate(1)) HC_WEBSOCKET_OBSERVER{ std::move(websocket) } };
observer->m_messageFunc = messageFunc;
observer->m_binaryMessageFunc = binaryMessageFunc;
observer->m_binaryFragmentFunc = binaryFragmentFunc;
observer->m_closeFunc = closeFunc;
observer->m_callbackContext = callbackContext;
observer->m_handlerToken = observer->websocket->RegisterEventCallbacks(MessageFunc, BinaryMessageFunc, BinaryMessageFragmentFunc, CloseFunc, observer.get());
return observer;
}
void HC_WEBSOCKET_OBSERVER::SetBinaryMessageFragmentEventFunction(HCWebSocketBinaryMessageFragmentFunction binaryFragmentFunc)
{
m_binaryFragmentFunc = binaryFragmentFunc;
}
void CALLBACK HC_WEBSOCKET_OBSERVER::MessageFunc(HCWebsocketHandle internalHandle, const char* message, void* context)
{
HC_WEBSOCKET_OBSERVER* observer{ static_cast<HC_WEBSOCKET_OBSERVER*>(context) };
assert(internalHandle->websocket->id == observer->websocket->id);
UNREFERENCED_PARAMETER(internalHandle);
if (observer->m_messageFunc)
{
observer->m_messageFunc(observer, message, observer->m_callbackContext);
}
}
void CALLBACK HC_WEBSOCKET_OBSERVER::BinaryMessageFunc(HCWebsocketHandle internalHandle, const uint8_t* bytes, uint32_t payloadSize, void* context)
{
HC_WEBSOCKET_OBSERVER* observer{ static_cast<HC_WEBSOCKET_OBSERVER*>(context) };
assert(internalHandle->websocket->id == observer->websocket->id);
UNREFERENCED_PARAMETER(internalHandle);
if (observer->m_binaryMessageFunc)
{
observer->m_binaryMessageFunc(observer, bytes, payloadSize, observer->m_callbackContext);
}
}
void CALLBACK HC_WEBSOCKET_OBSERVER::BinaryMessageFragmentFunc(HCWebsocketHandle internalHandle, const uint8_t* payloadBytes, uint32_t payloadSize, bool isLastFragment, void* context)
{
HC_WEBSOCKET_OBSERVER* observer{ static_cast<HC_WEBSOCKET_OBSERVER*>(context) };
assert(internalHandle->websocket->id == observer->websocket->id);
UNREFERENCED_PARAMETER(internalHandle);
if (observer->m_binaryFragmentFunc)
{
observer->m_binaryFragmentFunc(observer, payloadBytes, payloadSize, isLastFragment, observer->m_callbackContext);
}
}
void CALLBACK HC_WEBSOCKET_OBSERVER::CloseFunc(HCWebsocketHandle internalHandle, HCWebSocketCloseStatus status, void* context)
{
HC_WEBSOCKET_OBSERVER* observer{ static_cast<HC_WEBSOCKET_OBSERVER*>(context) };
assert(internalHandle->websocket->id == observer->websocket->id);
UNREFERENCED_PARAMETER(internalHandle);
if (observer->m_closeFunc)
{
observer->m_closeFunc(observer, status, observer->m_callbackContext);
}
}
namespace xbox
{
namespace httpclient
{
WebSocket::WebSocket(uint64_t _id, IWebSocketProvider& provider) :
id{ _id },
m_maxReceiveBufferSize{ WEBSOCKET_RECVBUFFER_MAXSIZE_DEFAULT },
m_provider{ provider }
{
}
WebSocket::~WebSocket()
{
HC_TRACE_INFORMATION(WEBSOCKET, __FUNCTION__);
}
uint32_t WebSocket::RegisterEventCallbacks(
_In_opt_ HCWebSocketMessageFunction messageFunc,
_In_opt_ HCWebSocketBinaryMessageFunction binaryMessageFunc,
_In_opt_ HCWebSocketBinaryMessageFragmentFunction binaryFragmentFunc,
_In_opt_ HCWebSocketCloseEventFunction closeFunc,
_In_opt_ void* callbackContext
)
{
std::unique_lock<std::recursive_mutex> lock{ m_eventCallbacksMutex };
m_eventCallbacks[m_nextToken] = EventCallbacks{ messageFunc, binaryMessageFunc, binaryFragmentFunc, closeFunc, callbackContext };
return m_nextToken++;
}
void WebSocket::UnregisterEventCallbacks(uint32_t registrationToken)
{
std::unique_lock<std::recursive_mutex> lock{ m_eventCallbacksMutex };
m_eventCallbacks.erase(registrationToken);
}
// Context for ConnectAsync operation. Ensures lifetime until Connect operation completes
struct WebSocket::ConnectContext
{
ConnectContext(std::shared_ptr<WebSocket> websocket, XAsyncBlock* async) :
observer{ HC_WEBSOCKET_OBSERVER::Initialize(std::move(websocket)) },
clientAsyncBlock{ async },
internalAsyncBlock{ nullptr, this, WebSocket::ConnectComplete }
{
}
~ConnectContext()
{
if (internalAsyncBlock.queue)
{
XTaskQueueCloseHandle(internalAsyncBlock.queue);
}
}
xbox::httpclient::ObserverPtr observer;
XAsyncBlock* const clientAsyncBlock;
XAsyncBlock internalAsyncBlock;
WebSocketCompletionResult result{};
};
// Context for Provider event callbacks. Ensure's lifetime as long as the WebSocket is connected (until CloseFunc is called)
struct WebSocket::ProviderContext
{
xbox::httpclient::ObserverPtr observer;
};
HRESULT WebSocket::ConnectAsync(
_In_ http_internal_string&& uri,
_In_ http_internal_string&& subProtocol,
_Inout_ XAsyncBlock* asyncBlock
) noexcept
{
m_uri = std::move(uri);
m_subProtocol = std::move(subProtocol);
auto context = http_allocate_unique<ConnectContext>(shared_from_this(), asyncBlock);
RETURN_IF_FAILED(XAsyncBegin(asyncBlock, context.get(), (void*)HCWebSocketConnectAsync, nullptr, ConnectAsyncProvider));
context.release();
return S_OK;
}
HRESULT CALLBACK WebSocket::ConnectAsyncProvider(XAsyncOp op, XAsyncProviderData const* data)
{
ConnectContext* context{ static_cast<ConnectContext*>(data->context) };
auto& ws{ context->observer->websocket };
switch (op)
{
case XAsyncOp::Begin:
{
std::unique_lock<std::mutex> lock{ ws->m_stateMutex };
RETURN_HR_IF(E_UNEXPECTED, ws->m_state != State::Initial);
XTaskQueuePortHandle workPort{ nullptr };
XTaskQueueGetPort(data->async->queue, XTaskQueuePort::Work, &workPort);
XTaskQueueCreateComposite(workPort, workPort, &context->internalAsyncBlock.queue);
ws->m_state = State::Connecting;
lock.unlock();
try
{
return ws->m_provider.ConnectAsync(ws->m_uri, ws->m_subProtocol, context->observer.get(), &context->internalAsyncBlock);
}
catch (...)
{
HC_TRACE_ERROR(WEBSOCKET, "Caught unhandled exception in HCWebSocketConnectFunction [ID %llu]", TO_ULL(ws->id));
return E_FAIL;
}
}
case XAsyncOp::GetResult:
{
WebSocketCompletionResult* result{ reinterpret_cast<WebSocketCompletionResult*>(data->buffer) };
*result = context->result;
return S_OK;
}
case XAsyncOp::Cleanup:
{
HC_UNIQUE_PTR<ConnectContext> reclaim{ context };
return S_OK;
}
default:
{
return S_OK;
}
}
}
void CALLBACK WebSocket::ConnectComplete(XAsyncBlock* async)
{
ConnectContext* context{ static_cast<ConnectContext*>(async->context) };
auto& ws{ context->observer->websocket };
assert(ws->m_state == State::Connecting);
HRESULT hr = HCGetWebSocketConnectResult(&context->internalAsyncBlock, &context->result);
std::unique_lock<std::mutex> lock{ ws->m_stateMutex };
if (SUCCEEDED(hr) && SUCCEEDED(context->result.errorCode))
{
// Connect was sucessful. Allocate ProviderContext to ensure WebSocket lifetime until it is reclaimed in WebSocket::CloseFunc
ws->m_state = State::Connected;
ws->m_providerContext = new (http_stl_allocator<ProviderContext>{}.allocate(1)) ProviderContext{
std::move(context->observer)
};
}
else
{
ws->m_state = State::Disconnected;
}
lock.unlock();
XAsyncComplete(context->clientAsyncBlock, hr, sizeof(WebSocketCompletionResult));
}
HRESULT WebSocket::SendAsync(
_In_z_ const char* message,
_Inout_ XAsyncBlock* asyncBlock
) noexcept
{
RETURN_HR_IF(E_UNEXPECTED, m_state != State::Connected);
RETURN_HR_IF(E_UNEXPECTED, !m_providerContext);
try
{
NotifyWebSocketRoutedHandlers(m_providerContext->observer.get(), false, message, nullptr, 0);
return m_provider.SendAsync(m_providerContext->observer.get(), message, asyncBlock);
}
catch (...)
{
HC_TRACE_ERROR(WEBSOCKET, "Caught unhandled exception in HCWebSocketSendMessageFunction [ID %llu]", TO_ULL(id));
return E_FAIL;
}
}
HRESULT WebSocket::SendBinaryAsync(
_In_reads_bytes_(payloadSize) const uint8_t* payloadBytes,
_In_ uint32_t payloadSize,
_Inout_ XAsyncBlock* asyncBlock
) noexcept
{
RETURN_HR_IF(E_UNEXPECTED, m_state != State::Connected);
RETURN_HR_IF(E_UNEXPECTED, !m_providerContext);
try
{
NotifyWebSocketRoutedHandlers(m_providerContext->observer.get(), false, nullptr, payloadBytes, payloadSize);
return m_provider.SendBinaryAsync(m_providerContext->observer.get(), payloadBytes, payloadSize, asyncBlock);
}
catch (...)
{
HC_TRACE_ERROR(WEBSOCKET, "Caught unhandled exception in HCWebSocketSendBinaryMessageFunction [ID %llu]", TO_ULL(id));
return E_FAIL;
}
}
HRESULT WebSocket::Disconnect()
{
RETURN_HR_IF(E_UNEXPECTED, !m_providerContext);
std::unique_lock<std::mutex> lock{ m_stateMutex };
RETURN_HR_IF(S_OK, m_state == State::Disconnecting);
RETURN_HR_IF(E_UNEXPECTED, m_state != State::Connected);
m_state = State::Disconnecting;
lock.unlock();
try
{
return m_provider.Disconnect(m_providerContext->observer.get(), HCWebSocketCloseStatus::Normal);
}
catch (...)
{
HC_TRACE_ERROR(WEBSOCKET, "Caught unhandled exception in HCWebSocketDisconnectFunction [ID %llu]", TO_ULL(id));
return E_FAIL;
}
}
const http_internal_string& WebSocket::Uri() const noexcept
{
return m_uri;
}
const http_internal_string& WebSocket::SubProtocol() const noexcept
{
return m_subProtocol;
}
const HttpHeaders& WebSocket::Headers() const noexcept
{
return m_connectHeaders;
}
const http_internal_string& WebSocket::ProxyUri() const noexcept
{
return m_proxyUri;
}
const bool WebSocket::ProxyDecryptsHttps() const noexcept
{
return m_allowProxyToDecryptHttps;
}
size_t WebSocket::MaxReceiveBufferSize() const noexcept
{
return m_maxReceiveBufferSize;
}
uint32_t WebSocket::PingInterval() const noexcept
{
return m_pingInterval;
}
HRESULT WebSocket::SetHeader(
http_internal_string&& headerName,
http_internal_string&& headerValue
) noexcept
{
RETURN_HR_IF(E_HC_CONNECT_ALREADY_CALLED, m_state != State::Initial);
m_connectHeaders[headerName] = headerValue;
return S_OK;
}
HRESULT WebSocket::SetProxyUri(
http_internal_string&& proxyUri
) noexcept
{
RETURN_HR_IF(E_HC_CONNECT_ALREADY_CALLED, m_state != State::Initial);
m_proxyUri = std::move(proxyUri);
m_allowProxyToDecryptHttps = false;
return S_OK;
}
HRESULT WebSocket::SetProxyDecryptsHttps(
bool allowProxyToDecryptHttps
) noexcept
{
if (m_proxyUri.empty())
{
return E_UNEXPECTED;
}
m_allowProxyToDecryptHttps = allowProxyToDecryptHttps;
return S_OK;
}
HRESULT WebSocket::SetMaxReceiveBufferSize(size_t maxReceiveBufferSizeBytes) noexcept
{
m_maxReceiveBufferSize = maxReceiveBufferSizeBytes;
return S_OK;
}
HRESULT WebSocket::SetPingInterval(uint32_t pingInterval) noexcept
{
m_pingInterval = pingInterval;
return S_OK;
}
void CALLBACK WebSocket::MessageFunc(
HCWebsocketHandle handle,
const char* message,
void* /*context*/
)
{
auto& websocket{ handle->websocket };
NotifyWebSocketRoutedHandlers(handle, true, message, nullptr, 0);
std::unique_lock<std::recursive_mutex> lock{ websocket->m_eventCallbacksMutex };
// Copy callbacks in case callback unregisters itself
auto callbacks{ websocket->m_eventCallbacks };
for (auto& pair : callbacks)
{
try
{
if (pair.second.messageFunc)
{
pair.second.messageFunc(handle, message, pair.second.context);
}
}
catch (...)
{
HC_TRACE_ERROR(WEBSOCKET, "Caught unhandled exception in HCWebSocketMessageFunction");
}
}
}
void CALLBACK WebSocket::BinaryMessageFunc(
HCWebsocketHandle handle,
const uint8_t* bytes,
uint32_t payloadSize,
void* /*context*/
)
{
auto& websocket{ handle->websocket };
NotifyWebSocketRoutedHandlers(handle, true, nullptr, bytes, payloadSize);
std::unique_lock<std::recursive_mutex> lock{ websocket->m_eventCallbacksMutex };
auto callbacks{ websocket->m_eventCallbacks };
for (auto& pair : callbacks)
{
try
{
if (pair.second.binaryMessageFunc)
{
pair.second.binaryMessageFunc(handle, bytes, payloadSize, pair.second.context);
}
}
catch (...)
{
HC_TRACE_ERROR(WEBSOCKET, "Caught unhandled exception in HCWebSocketBinaryMessageFunction");
}
}
}
void CALLBACK WebSocket::BinaryMessageFragmentFunc(
HCWebsocketHandle handle,
const uint8_t* bytes,
uint32_t payloadSize,
bool isLastFragment,
void* /*context*/
)
{
auto& websocket{ handle->websocket };
NotifyWebSocketRoutedHandlers(handle, true, nullptr, bytes, payloadSize);
std::unique_lock<std::recursive_mutex> lock{ websocket->m_eventCallbacksMutex };
auto callbacks{ websocket->m_eventCallbacks };
for (auto& pair : callbacks)
{
try
{
if (pair.second.binaryMessageFragmentFunc)
{
pair.second.binaryMessageFragmentFunc(handle, bytes, payloadSize, isLastFragment, pair.second.context);
}
else if (pair.second.binaryMessageFunc)
{
HC_TRACE_INFORMATION(WEBSOCKET, "Received binary message fragment but no client handler has been set. Invoking HCWebSocketBinaryMessageFunction instead.");
pair.second.binaryMessageFunc(handle, bytes, payloadSize, pair.second.context);
}
}
catch (...)
{
HC_TRACE_WARNING(WEBSOCKET, "Caught unhandled exception in HCWebSocketBinaryMessageFragmentFunction");
}
}
}
void CALLBACK WebSocket::CloseFunc(
HCWebsocketHandle handle,
HCWebSocketCloseStatus status,
void* /*context*/
)
{
HC_TRACE_INFORMATION(WEBSOCKET, __FUNCTION__);
auto websocket{ handle->websocket };
std::unique_lock<std::mutex> stateLock{ websocket->m_stateMutex };
if (!websocket->m_providerContext)
{
HC_TRACE_ERROR(WEBSOCKET, "Unexpected call to WebSocket::CloseFunc will be ignored!");
return;
}
// We no longer expect callbacks from Provider at this point, so cleanup m_providerContext. If there are no other
// observers of websocket, it may be destroyed now
HC_UNIQUE_PTR<ProviderContext> reclaim{ websocket->m_providerContext };
websocket->m_state = State::Disconnected;
stateLock.unlock();
std::unique_lock<std::recursive_mutex> callbackLock{ websocket->m_eventCallbacksMutex };
auto callbacks{ websocket->m_eventCallbacks };
for (auto& pair : callbacks)
{
try
{
if (pair.second.closeFunc)
{
pair.second.closeFunc(handle, status, pair.second.context);
}
}
catch (...)
{
HC_TRACE_ERROR(WEBSOCKET, "Caught unhandled exception in HCWebSocketCloseEventFunction");
}
}
}
void WebSocket::NotifyWebSocketRoutedHandlers(
_In_ HCWebsocketHandle websocket,
_In_ bool receiving,
_In_opt_z_ const char* message,
_In_opt_ const uint8_t* payloadBytes,
_In_ size_t payloadSize
)
{
auto httpSingleton = get_http_singleton();
if (httpSingleton)
{
std::lock_guard<std::recursive_mutex> lock(httpSingleton->m_callRoutedHandlersLock);
for (const auto& pair : httpSingleton->m_webSocketRoutedHandlers)
{
pair.second.first(websocket, receiving, message, payloadBytes, payloadSize, pair.second.second);
}
}
}
} // namespace httpclient
} // namespace xbox
#endif