-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathhub_connection_impl.cpp
More file actions
693 lines (613 loc) · 29.4 KB
/
Copy pathhub_connection_impl.cpp
File metadata and controls
693 lines (613 loc) · 29.4 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include "stdafx.h"
#include "hub_connection_impl.h"
#include "signalrclient/hub_exception.h"
#include "trace_log_writer.h"
#include "signalrclient/signalr_exception.h"
#include "json_hub_protocol.h"
#include "message_type.h"
#include "handshake_protocol.h"
#include "signalrclient/websocket_client.h"
#include "signalr_default_scheduler.h"
namespace signalr
{
// unnamed namespace makes it invisble outside this translation unit
namespace
{
static std::function<void(const char*, const signalr::value&)> create_hub_invocation_callback(const logger& logger,
const std::function<void(const signalr::value&)>& set_result,
const std::function<void(const std::exception_ptr e)>& set_exception);
}
std::shared_ptr<hub_connection_impl> hub_connection_impl::create(const std::string& url, std::unique_ptr<hub_protocol>&& hub_protocol,
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer, std::function<std::shared_ptr<http_client>(const signalr_client_config&)> http_client_factory,
std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory, const bool skip_negotiation)
{
auto connection = std::shared_ptr<hub_connection_impl>(new hub_connection_impl(url, std::move(hub_protocol),
trace_level, log_writer, http_client_factory, websocket_factory, skip_negotiation));
connection->initialize();
return connection;
}
hub_connection_impl::hub_connection_impl(const std::string& url, std::unique_ptr<hub_protocol>&& hub_protocol, trace_level trace_level,
const std::shared_ptr<log_writer>& log_writer, std::function<std::shared_ptr<http_client>(const signalr_client_config&)> http_client_factory,
std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory, const bool skip_negotiation)
: m_connection(connection_impl::create(url, trace_level, log_writer, http_client_factory, websocket_factory, skip_negotiation))
, m_logger(log_writer, trace_level),
m_callback_manager("connection went out of scope before invocation result was received"),
m_handshakeReceived(false), m_disconnected([](std::exception_ptr) noexcept {}), m_protocol(std::move(hub_protocol))
{
hub_message ping_msg(signalr::message_type::ping);
m_cached_ping = m_protocol->write_message(&ping_msg);
}
void hub_connection_impl::initialize()
{
// weak_ptr prevents a circular dependency leading to memory leak and other problems
std::weak_ptr<hub_connection_impl> weak_hub_connection = shared_from_this();
m_connection->set_message_received([weak_hub_connection](std::string&& message)
{
auto connection = weak_hub_connection.lock();
if (connection)
{
connection->process_message(std::move(message));
}
});
m_connection->set_disconnected([weak_hub_connection](std::exception_ptr exception)
{
auto connection = weak_hub_connection.lock();
if (connection)
{
// start may be waiting on the handshake response so we complete it here, this no-ops if already set
connection->m_handshakeTask->set(std::make_exception_ptr(signalr_exception("connection closed while handshake was in progress.")));
try
{
connection->m_disconnect_cts->cancel();
}
catch (const std::exception& ex)
{
if (connection->m_logger.is_enabled(trace_level::warning))
{
connection->m_logger.log(trace_level::warning, std::string("disconnect event threw an exception during connection closure: ")
.append(ex.what()));
}
}
connection->m_callback_manager.clear("connection was stopped before invocation result was received");
connection->m_disconnected(exception);
}
});
}
void hub_connection_impl::on(const std::string& event_name, const std::function<void(const std::vector<signalr::value>&)>& handler)
{
if (event_name.length() == 0)
{
throw std::invalid_argument("event_name cannot be empty");
}
auto weak_connection = std::weak_ptr<hub_connection_impl>(shared_from_this());
auto connection = weak_connection.lock();
if (connection && connection->get_connection_state() != connection_state::disconnected)
{
throw signalr_exception("can't register a handler if the connection is not in a disconnected state");
}
if (m_subscriptions.find(event_name) != m_subscriptions.end())
{
throw signalr_exception(
"an action for this event has already been registered. event name: " + event_name);
}
m_subscriptions.insert({event_name, handler});
}
void hub_connection_impl::start(std::function<void(std::exception_ptr)> callback) noexcept
{
if (m_connection->get_connection_state() != connection_state::disconnected)
{
callback(std::make_exception_ptr(signalr_exception(
"the connection can only be started if it is in the disconnected state")));
return;
}
m_connection->set_client_config(m_signalr_client_config);
m_handshakeTask = std::make_shared<completion_event>();
m_disconnect_cts = std::make_shared<cancellation_token_source>();
m_handshakeReceived = false;
std::weak_ptr<hub_connection_impl> weak_connection = shared_from_this();
m_connection->start([weak_connection, callback](std::exception_ptr start_exception)
{
auto connection = weak_connection.lock();
if (!connection)
{
// The connection has been destructed
callback(std::make_exception_ptr(signalr_exception("the hub connection has been deconstructed")));
return;
}
if (start_exception)
{
assert(connection->get_connection_state() == connection_state::disconnected);
// connection didn't start, don't call stop
callback(start_exception);
return;
}
std::shared_ptr<std::mutex> handshake_request_lock = std::make_shared<std::mutex>();
std::shared_ptr<bool> handshake_request_done = std::make_shared<bool>();
auto handle_handshake = [weak_connection, handshake_request_done, handshake_request_lock, callback](std::exception_ptr exception, bool fromSend)
{
assert(fromSend ? *handshake_request_done : true);
auto connection = weak_connection.lock();
if (!connection)
{
// The connection has been destructed
callback(std::make_exception_ptr(signalr_exception("the hub connection has been deconstructed")));
return;
}
{
std::lock_guard<std::mutex> lock(*handshake_request_lock);
// connection.send will be waiting on the handshake task which has been set by the caller already
if (!fromSend && *handshake_request_done == true)
{
return;
}
*handshake_request_done = true;
}
try
{
if (exception == nullptr)
{
connection->m_handshakeTask->get();
callback(nullptr);
}
}
catch (...)
{
exception = std::current_exception();
}
if (exception != nullptr)
{
connection->m_connection->stop([callback, exception](std::exception_ptr)
{
callback(exception);
}, exception);
}
else
{
connection->start_keepalive();
}
};
auto handshake_request = handshake::write_handshake(connection->m_protocol);
auto handshake_task = connection->m_handshakeTask;
auto handshake_timeout = connection->m_signalr_client_config.get_handshake_timeout();
connection->m_disconnect_cts->register_callback([handle_handshake, handshake_request_lock, handshake_request_done]()
{
{
std::lock_guard<std::mutex> lock(*handshake_request_lock);
// no op after connection.send returned, m_handshakeTask should be set before m_disconnect_cts is canceled
if (*handshake_request_done == true)
{
return;
}
}
// if the request isn't completed then no one is waiting on the handshake task
// so we need to run the callback here instead of relying on connection.send completing
// handshake_request_done is set in handle_handshake, don't set it here
handle_handshake(nullptr, false);
});
timer(connection->m_signalr_client_config.get_scheduler(),
[handle_handshake, handshake_task, handshake_timeout, handshake_request_lock](std::chrono::milliseconds duration)
{
{
std::lock_guard<std::mutex> lock(*handshake_request_lock);
// if the task is set then connection.send is either already waiting on the handshake or has completed,
// or stop has been called and will be handling the callback
if (handshake_task->is_set())
{
return true;
}
if (duration < handshake_timeout)
{
return false;
}
}
auto exception = std::make_exception_ptr(signalr_exception("timed out waiting for the server to respond to the handshake message."));
// unblocks connection.send if it's waiting on the task
handshake_task->set(exception);
handle_handshake(exception, false);
return true;
});
connection->m_connection->send(handshake_request, connection->m_protocol->transfer_format(),
[handle_handshake, handshake_request_done, handshake_request_lock](std::exception_ptr exception)
{
{
std::lock_guard<std::mutex> lock(*handshake_request_lock);
if (*handshake_request_done == true)
{
// callback ran from timer or cancellation token, nothing to do here
return;
}
// indicates that the handshake timer doesn't need to call the callback, it just needs to set the timeout exception
// handle_handshake will be waiting on the handshake completion (error or success) to call the callback
*handshake_request_done = true;
}
handle_handshake(exception, true);
});
});
}
void hub_connection_impl::stop(std::function<void(std::exception_ptr)> callback, bool is_dtor) noexcept
{
if (get_connection_state() == connection_state::disconnected)
{
// don't log if already disconnected and stop called from dtor, it's just noise
if (!is_dtor)
{
m_logger.log(trace_level::debug, "stop ignored because the connection is already disconnected.");
}
callback(nullptr);
return;
}
else
{
{
std::lock_guard<std::mutex> lock(m_stop_callback_lock);
m_stop_callbacks.push_back(callback);
if (m_stop_callbacks.size() > 1)
{
m_logger.log(trace_level::info, "Stop is already in progress, waiting for it to finish.");
// we already registered the callback
// so we can just return now as the in-progress stop will trigger the callback when it completes
return;
}
}
std::weak_ptr<hub_connection_impl> weak_connection = shared_from_this();
m_connection->stop([weak_connection](std::exception_ptr exception)
{
auto connection = weak_connection.lock();
if (!connection)
{
return;
}
assert(connection->get_connection_state() == connection_state::disconnected);
std::vector<std::function<void(std::exception_ptr)>> callbacks;
{
std::lock_guard<std::mutex> lock(connection->m_stop_callback_lock);
// copy the callbacks out and clear the list inside the lock
// then run the callbacks outside of the lock
callbacks = connection->m_stop_callbacks;
connection->m_stop_callbacks.clear();
}
for (auto& callback : callbacks)
{
callback(exception);
}
}, nullptr);
}
}
void hub_connection_impl::process_message(std::string&& response)
{
try
{
if (!m_handshakeReceived)
{
signalr::value handshake;
std::tie(response, handshake) = handshake::parse_handshake(response);
auto& obj = handshake.as_map();
auto found = obj.find("error");
if (found != obj.end())
{
auto& error = found->second.as_string();
if (m_logger.is_enabled(trace_level::error))
{
m_logger.log(trace_level::error, std::string("handshake error: ")
.append(error));
}
m_handshakeTask->set(std::make_exception_ptr(signalr_exception(std::string("Received an error during handshake: ").append(error))));
return;
}
else
{
found = obj.find("type");
if (found != obj.end())
{
m_handshakeTask->set(std::make_exception_ptr(signalr_exception(std::string("Received unexpected message while waiting for the handshake response."))));
return;
}
m_handshakeReceived = true;
m_handshakeTask->set();
if (response.size() == 0)
{
return;
}
}
}
reset_server_timeout();
auto messages = m_protocol->parse_messages(response);
for (const auto& val : messages)
{
// Protocol received an unknown message type and gave us a null object, close the connection like we do in other client implementations
if (val == nullptr)
{
throw std::runtime_error("null message received");
}
switch (val->message_type)
{
case message_type::invocation:
{
auto invocation = static_cast<invocation_message*>(val.get());
auto event = m_subscriptions.find(invocation->target);
if (event != m_subscriptions.end())
{
const auto& args = invocation->arguments;
event->second(args);
}
else
{
m_logger.log(trace_level::info, "handler not found");
}
break;
}
case message_type::stream_invocation:
// Sent to server only, should not be received by client
throw std::runtime_error("Received unexpected message type 'StreamInvocation'");
case message_type::stream_item:
// TODO
break;
case message_type::completion:
{
auto completion = static_cast<completion_message*>(val.get());
invoke_callback(completion);
break;
}
case message_type::cancel_invocation:
// Sent to server only, should not be received by client
throw std::runtime_error("Received unexpected message type 'CancelInvocation'.");
case message_type::ping:
if (m_logger.is_enabled(trace_level::debug))
{
m_logger.log(trace_level::debug, "ping message received.");
}
break;
case message_type::close:
{
auto close = static_cast<close_message*>(val.get());
if (m_logger.is_enabled(trace_level::debug))
{
if (close->error.length() == 0)
{
m_logger.log(trace_level::debug, "received close message.");
}
else
{
m_logger.log(trace_level::debug, "received close message with an error: " + close->error);
}
}
m_connection->stop([](std::exception_ptr) {}, std::make_exception_ptr(std::runtime_error("the server closed the connection with the following error: " + close->error)));
return;
}
default:
throw std::runtime_error("unknown message type '" + std::to_string(static_cast<int>(val->message_type)) + "' received");
break;
}
}
}
catch (const std::exception &e)
{
if (m_logger.is_enabled(trace_level::error))
{
m_logger.log(trace_level::error, std::string("error occurred when parsing response: ")
.append(e.what())
.append(". response: ")
.append(response));
}
// TODO: Consider passing "reason" exception to stop
m_connection->stop([](std::exception_ptr) {}, std::current_exception());
}
}
bool hub_connection_impl::invoke_callback(completion_message* completion)
{
const char* error = nullptr;
if (!completion->error.empty())
{
error = completion->error.data();
}
// TODO: consider transferring ownership of 'result' so that if we run user callbacks on a different thread we don't need to
// worry about object lifetime
if (!m_callback_manager.invoke_callback(completion->invocation_id, error, completion->result, true))
{
if (m_logger.is_enabled(trace_level::info))
{
m_logger.log(trace_level::info, std::string("no callback found for id: ").append(completion->invocation_id));
}
}
return true;
}
void hub_connection_impl::invoke(const std::string& method_name, const std::vector<signalr::value>& arguments, std::function<void(const signalr::value&, std::exception_ptr)> callback) noexcept
{
const auto& callback_id = m_callback_manager.register_callback(
create_hub_invocation_callback(m_logger, [callback](const signalr::value& result) { callback(result, nullptr); },
[callback](const std::exception_ptr e) { callback(signalr::value(), e); }));
invoke_hub_method(method_name, arguments, callback_id, nullptr,
[callback](const std::exception_ptr e){ callback(signalr::value(), e); });
}
void hub_connection_impl::send(const std::string& method_name, const std::vector<signalr::value>& arguments, std::function<void(std::exception_ptr)> callback) noexcept
{
invoke_hub_method(method_name, arguments, "",
[callback]() { callback(nullptr); },
[callback](const std::exception_ptr e){ callback(e); });
}
void hub_connection_impl::invoke_hub_method(const std::string& method_name, const std::vector<signalr::value>& arguments,
const std::string& callback_id, std::function<void()> set_completion, std::function<void(const std::exception_ptr)> set_exception) noexcept
{
try
{
invocation_message invocation(callback_id, method_name, arguments);
auto message = m_protocol->write_message(&invocation);
// weak_ptr prevents a circular dependency leading to memory leak and other problems
auto weak_hub_connection = std::weak_ptr<hub_connection_impl>(shared_from_this());
m_connection->send(message, m_protocol->transfer_format(), [set_completion, set_exception, weak_hub_connection, callback_id](std::exception_ptr exception)
{
if (exception)
{
auto hub_connection = weak_hub_connection.lock();
if (hub_connection)
{
hub_connection->m_callback_manager.remove_callback(callback_id);
}
set_exception(exception);
}
else
{
if (callback_id.empty())
{
// complete nonBlocking call
set_completion();
}
}
});
reset_send_ping();
}
catch (const std::exception& e)
{
m_callback_manager.remove_callback(callback_id);
if (m_logger.is_enabled(trace_level::warning))
{
m_logger.log(trace_level::warning, std::string("failed to send invocation: ").append(e.what()));
}
set_exception(std::current_exception());
}
}
connection_state hub_connection_impl::get_connection_state() const noexcept
{
return m_connection->get_connection_state();
}
std::string hub_connection_impl::get_connection_id() const
{
return m_connection->get_connection_id();
}
void hub_connection_impl::set_client_config(const signalr_client_config& config)
{
m_signalr_client_config = config;
m_connection->set_client_config(config);
}
void hub_connection_impl::set_disconnected(const std::function<void(std::exception_ptr)>& disconnected)
{
m_disconnected = disconnected;
}
void hub_connection_impl::reset_send_ping()
{
auto timeMs = (std::chrono::steady_clock::now() + m_signalr_client_config.get_keepalive_interval()).time_since_epoch();
m_nextActivationSendPing.store(std::chrono::duration_cast<std::chrono::milliseconds>(timeMs).count());
}
void hub_connection_impl::reset_server_timeout()
{
auto timeMs = (std::chrono::steady_clock::now() + m_signalr_client_config.get_server_timeout()).time_since_epoch();
m_nextActivationServerTimeout.store(std::chrono::duration_cast<std::chrono::milliseconds>(timeMs).count());
}
void hub_connection_impl::start_keepalive()
{
if (m_logger.is_enabled(trace_level::debug))
{
m_logger.log(trace_level::debug, "starting keep alive timer.");
}
auto send_ping = [](std::shared_ptr<hub_connection_impl> connection)
{
if (!connection)
{
return;
}
if (connection->get_connection_state() != connection_state::connected)
{
return;
}
try
{
std::weak_ptr<hub_connection_impl> weak_connection = connection;
connection->m_connection->send(
connection->m_cached_ping,
connection->m_protocol->transfer_format(), [weak_connection](std::exception_ptr exception)
{
auto connection = weak_connection.lock();
if (connection)
{
if (exception)
{
if (connection->m_logger.is_enabled(trace_level::warning))
{
connection->m_logger.log(trace_level::warning, "failed to send ping!");
}
}
else
{
connection->reset_send_ping();
}
}
});
}
catch (const std::exception& e)
{
if (connection->m_logger.is_enabled(trace_level::warning))
{
connection->m_logger.log(trace_level::warning, std::string("failed to send ping: ").append(e.what()));
}
}
};
send_ping(shared_from_this());
reset_server_timeout();
std::weak_ptr<hub_connection_impl> weak_connection = shared_from_this();
timer(m_signalr_client_config.get_scheduler(),
[send_ping, weak_connection](std::chrono::milliseconds)
{
auto connection = weak_connection.lock();
if (!connection)
{
return true;
}
if (connection->get_connection_state() != connection_state::connected)
{
return true;
}
auto timeNowmSeconds =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
if (timeNowmSeconds > connection->m_nextActivationServerTimeout.load())
{
if (connection->get_connection_state() == connection_state::connected)
{
auto error_msg = std::string("server timeout (")
.append(std::to_string(connection->m_signalr_client_config.get_server_timeout().count()))
.append(" ms) elapsed without receiving a message from the server.");
if (connection->m_logger.is_enabled(trace_level::warning))
{
connection->m_logger.log(trace_level::warning, error_msg);
}
connection->m_connection->stop([](std::exception_ptr)
{
}, std::make_exception_ptr(signalr_exception(error_msg)));
}
}
if (timeNowmSeconds > connection->m_nextActivationSendPing.load())
{
if (connection->m_logger.is_enabled(trace_level::debug))
{
connection->m_logger.log(trace_level::debug, "sending ping to server.");
}
send_ping(connection);
}
return false;
});
}
// unnamed namespace makes it invisble outside this translation unit
namespace
{
static std::function<void(const char* error, const signalr::value&)> create_hub_invocation_callback(const logger& logger,
const std::function<void(const signalr::value&)>& set_result,
const std::function<void(const std::exception_ptr)>& set_exception)
{
return [logger, set_result, set_exception](const char* error, const signalr::value& message)
{
if (error != nullptr)
{
set_exception(
std::make_exception_ptr(
hub_exception(error)));
}
else
{
set_result(message);
}
};
}
}
}