Skip to content

Commit 5c499e3

Browse files
Flow error to disconnected callback (#56)
1 parent 4a10b9b commit 5c499e3

File tree

11 files changed

+207
-72
lines changed

11 files changed

+207
-72
lines changed

include/signalrclient/connection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ namespace signalr
3535
SIGNALRCLIENT_API void __cdecl send(const std::string& data, transfer_format transfer_format, std::function<void(std::exception_ptr)> callback) noexcept;
3636

3737
SIGNALRCLIENT_API void __cdecl set_message_received(const message_received_handler& message_received_callback);
38-
SIGNALRCLIENT_API void __cdecl set_disconnected(const std::function<void __cdecl()>& disconnected_callback);
38+
SIGNALRCLIENT_API void __cdecl set_disconnected(const std::function<void __cdecl(std::exception_ptr)>& disconnected_callback);
3939

4040
SIGNALRCLIENT_API void __cdecl set_client_config(const signalr_client_config& config);
4141

42-
SIGNALRCLIENT_API void __cdecl stop(std::function<void(std::exception_ptr)> callback) noexcept;
42+
SIGNALRCLIENT_API void __cdecl stop(std::function<void(std::exception_ptr)> callback, std::exception_ptr exception) noexcept;
4343

4444
SIGNALRCLIENT_API connection_state __cdecl get_connection_state() const noexcept;
4545
SIGNALRCLIENT_API std::string __cdecl get_connection_id() const;

include/signalrclient/hub_connection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace signalr
4141
SIGNALRCLIENT_API connection_state __cdecl get_connection_state() const;
4242
SIGNALRCLIENT_API std::string __cdecl get_connection_id() const;
4343

44-
SIGNALRCLIENT_API void __cdecl set_disconnected(const std::function<void __cdecl()>& disconnected_callback);
44+
SIGNALRCLIENT_API void __cdecl set_disconnected(const std::function<void __cdecl(std::exception_ptr)>& disconnected_callback);
4545

4646
SIGNALRCLIENT_API void __cdecl set_client_config(const signalr_client_config& config);
4747

src/signalrclient/connection.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace signalr
3232
m_pImpl->set_message_received(message_received_callback);
3333
}
3434

35-
void connection::set_disconnected(const std::function<void()>& disconnected_callback)
35+
void connection::set_disconnected(const std::function<void(std::exception_ptr)>& disconnected_callback)
3636
{
3737
m_pImpl->set_disconnected(disconnected_callback);
3838
}
@@ -42,9 +42,9 @@ namespace signalr
4242
m_pImpl->set_client_config(config);
4343
}
4444

45-
void connection::stop(std::function<void(std::exception_ptr)> callback) noexcept
45+
void connection::stop(std::function<void(std::exception_ptr)> callback, std::exception_ptr exception) noexcept
4646
{
47-
m_pImpl->stop(callback);
47+
m_pImpl->stop(callback, exception);
4848
}
4949

5050
connection_state connection::get_connection_state() const noexcept

src/signalrclient/connection_impl.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace signalr
3535
connection_impl::connection_impl(const std::string& url, trace_level trace_level, const std::shared_ptr<log_writer>& log_writer,
3636
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)
3737
: m_base_url(url), m_connection_state(connection_state::disconnected), m_logger(log_writer, trace_level), m_transport(nullptr), m_skip_negotiation(skip_negotiation),
38-
m_message_received([](const std::string&) noexcept {}), m_disconnected([]() noexcept {}), m_disconnect_cts(std::make_shared<cancellation_token>())
38+
m_message_received([](const std::string&) noexcept {}), m_disconnected([](std::exception_ptr) noexcept {}), m_disconnect_cts(std::make_shared<cancellation_token>())
3939
{
4040
if (http_client_factory != nullptr)
4141
{
@@ -583,8 +583,9 @@ namespace signalr
583583
});
584584
}
585585

586-
void connection_impl::stop(std::function<void(std::exception_ptr)> callback) noexcept
586+
void connection_impl::stop(std::function<void(std::exception_ptr)> callback, std::exception_ptr exception) noexcept
587587
{
588+
m_stop_error = exception;
588589
m_logger.log(trace_level::info, "stopping connection");
589590

590591
shutdown(callback);
@@ -601,7 +602,7 @@ namespace signalr
601602
if (current_state == connection_state::disconnected)
602603
{
603604
m_disconnect_cts->cancel();
604-
callback(nullptr);
605+
callback(m_stop_error);
605606
return;
606607
}
607608

@@ -654,6 +655,13 @@ namespace signalr
654655
return;
655656
}
656657

658+
// if we have a m_stop_error, it takes precedence over the error from the transport
659+
if (m_stop_error)
660+
{
661+
error = m_stop_error;
662+
m_stop_error = nullptr;
663+
}
664+
657665
change_state(connection_state::disconnected);
658666
m_transport = nullptr;
659667
}
@@ -679,7 +687,7 @@ namespace signalr
679687

680688
try
681689
{
682-
m_disconnected();
690+
m_disconnected(error);
683691
}
684692
catch (const std::exception & e)
685693
{
@@ -726,7 +734,7 @@ namespace signalr
726734
m_signalr_client_config = config;
727735
}
728736

729-
void connection_impl::set_disconnected(const std::function<void()>& disconnected)
737+
void connection_impl::set_disconnected(const std::function<void(std::exception_ptr)>& disconnected)
730738
{
731739
ensure_disconnected("cannot set the disconnected callback when the connection is not in the disconnected state. ");
732740
m_disconnected = disconnected;

src/signalrclient/connection_impl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ namespace signalr
4040

4141
void start(std::function<void(std::exception_ptr)> callback) noexcept;
4242
void send(const std::string &data, transfer_format transfer_format, std::function<void(std::exception_ptr)> callback) noexcept;
43-
void stop(std::function<void(std::exception_ptr)> callback) noexcept;
43+
void stop(std::function<void(std::exception_ptr)> callback, std::exception_ptr exception) noexcept;
4444

4545
connection_state get_connection_state() const noexcept;
4646
std::string get_connection_id() const noexcept;
4747

4848
void set_message_received(const std::function<void(std::string&&)>& message_received);
49-
void set_disconnected(const std::function<void()>& disconnected);
49+
void set_disconnected(const std::function<void(std::exception_ptr)>& disconnected);
5050
void set_client_config(const signalr_client_config& config);
5151

5252
private:
@@ -57,9 +57,10 @@ namespace signalr
5757
std::shared_ptr<transport> m_transport;
5858
std::unique_ptr<transport_factory> m_transport_factory;
5959
bool m_skip_negotiation;
60+
std::exception_ptr m_stop_error;
6061

6162
std::function<void(std::string&&)> m_message_received;
62-
std::function<void()> m_disconnected;
63+
std::function<void(std::exception_ptr)> m_disconnected;
6364
signalr_client_config m_signalr_client_config;
6465

6566
std::shared_ptr<cancellation_token> m_disconnect_cts;

src/signalrclient/hub_connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ namespace signalr
104104
return m_pImpl->get_connection_id();
105105
}
106106

107-
void hub_connection::set_disconnected(const std::function<void()>& disconnected_callback)
107+
void hub_connection::set_disconnected(const std::function<void(std::exception_ptr)>& disconnected_callback)
108108
{
109109
if (!m_pImpl)
110110
{

src/signalrclient/hub_connection_impl.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace signalr
4040
: m_connection(connection_impl::create(url, trace_level, log_writer,
4141
http_client_factory, websocket_factory, skip_negotiation)), m_logger(log_writer, trace_level),
4242
m_callback_manager("connection went out of scope before invocation result was received"),
43-
m_handshakeReceived(false), m_disconnected([]() noexcept {}), m_protocol(std::unique_ptr<json_hub_protocol>(new json_hub_protocol()))
43+
m_handshakeReceived(false), m_disconnected([](std::exception_ptr) noexcept {}), m_protocol(std::unique_ptr<json_hub_protocol>(new json_hub_protocol()))
4444
{}
4545

4646
void hub_connection_impl::initialize()
@@ -57,7 +57,7 @@ namespace signalr
5757
}
5858
});
5959

60-
m_connection->set_disconnected([weak_hub_connection]()
60+
m_connection->set_disconnected([weak_hub_connection](std::exception_ptr exception)
6161
{
6262
auto connection = weak_hub_connection.lock();
6363
if (connection)
@@ -67,7 +67,7 @@ namespace signalr
6767

6868
connection->m_callback_manager.clear("connection was stopped before invocation result was received");
6969

70-
connection->m_disconnected();
70+
connection->m_disconnected(exception);
7171
}
7272
});
7373
}
@@ -120,7 +120,7 @@ namespace signalr
120120

121121
if (start_exception)
122122
{
123-
connection->m_connection->stop([start_exception, callback, weak_connection](std::exception_ptr)
123+
connection->m_connection->stop([callback, weak_connection](std::exception_ptr ex)
124124
{
125125
try
126126
{
@@ -133,8 +133,8 @@ namespace signalr
133133
}
134134
catch (...) {}
135135

136-
callback(start_exception);
137-
});
136+
callback(ex);
137+
}, start_exception);
138138
return;
139139
}
140140

@@ -167,7 +167,7 @@ namespace signalr
167167
connection->m_connection->stop([callback, handshake_exception](std::exception_ptr)
168168
{
169169
callback(handshake_exception);
170-
});
170+
}, nullptr);
171171
}
172172
});
173173
});
@@ -218,7 +218,7 @@ namespace signalr
218218
{
219219
callback(exception);
220220
}
221-
});
221+
}, nullptr);
222222
}
223223
}
224224

@@ -318,7 +318,7 @@ namespace signalr
318318
}
319319

320320
// TODO: Consider passing "reason" exception to stop
321-
m_connection->stop([](std::exception_ptr) {});
321+
m_connection->stop([](std::exception_ptr) {}, std::current_exception());
322322
}
323323
}
324324

@@ -419,7 +419,7 @@ namespace signalr
419419
m_connection->set_client_config(config);
420420
}
421421

422-
void hub_connection_impl::set_disconnected(const std::function<void()>& disconnected)
422+
void hub_connection_impl::set_disconnected(const std::function<void(std::exception_ptr)>& disconnected)
423423
{
424424
m_disconnected = disconnected;
425425
}

src/signalrclient/hub_connection_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace signalr
4343
std::string get_connection_id() const;
4444

4545
void set_client_config(const signalr_client_config& config);
46-
void set_disconnected(const std::function<void()>& disconnected);
46+
void set_disconnected(const std::function<void(std::exception_ptr)>& disconnected);
4747

4848
private:
4949
hub_connection_impl(const std::string& url, trace_level trace_level,
@@ -57,7 +57,7 @@ namespace signalr
5757
std::unordered_map<std::string, std::function<void(const signalr::value&)>, case_insensitive_hash, case_insensitive_equals> m_subscriptions;
5858
bool m_handshakeReceived;
5959
std::shared_ptr<completion_event> m_handshakeTask;
60-
std::function<void()> m_disconnected;
60+
std::function<void(std::exception_ptr)> m_disconnected;
6161
signalr_client_config m_signalr_client_config;
6262
std::unique_ptr<hub_protocol> m_protocol;
6363

0 commit comments

Comments
 (0)