-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove pplx::task from public API #8747
Changes from all commits
bb6f6ab
4bbaf1c
6896256
a9d309d
7a83cf7
d4915e8
62d9852
2ad902d
cf894f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
#include "_exports.h" | ||
#include <memory> | ||
#include <functional> | ||
#include "pplx/pplxtasks.h" | ||
#include "connection_state.h" | ||
#include "trace_level.h" | ||
#include "log_writer.h" | ||
|
@@ -29,16 +28,16 @@ namespace signalr | |
|
||
connection& operator=(const connection&) = delete; | ||
|
||
SIGNALRCLIENT_API pplx::task<void> __cdecl start(); | ||
SIGNALRCLIENT_API void __cdecl start(std::function<void(std::exception_ptr)> callback) noexcept; | ||
|
||
SIGNALRCLIENT_API pplx::task<void> __cdecl send(const std::string& data); | ||
SIGNALRCLIENT_API void __cdecl send(const std::string& data, std::function<void(std::exception_ptr)> callback) noexcept; | ||
|
||
SIGNALRCLIENT_API void __cdecl set_message_received(const message_received_handler& message_received_callback); | ||
SIGNALRCLIENT_API void __cdecl set_disconnected(const std::function<void __cdecl()>& disconnected_callback); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to have set_disconnected's completion callback take an exception as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably, I'll track that in our public API review issue #8717 |
||
|
||
SIGNALRCLIENT_API void __cdecl set_client_config(const signalr_client_config& config); | ||
|
||
SIGNALRCLIENT_API pplx::task<void> __cdecl stop(); | ||
SIGNALRCLIENT_API void __cdecl stop(std::function<void(std::exception_ptr)> callback) noexcept; | ||
|
||
SIGNALRCLIENT_API connection_state __cdecl get_connection_state() const noexcept; | ||
SIGNALRCLIENT_API std::string __cdecl get_connection_id() const; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
#include <sstream> | ||
#include "hub_connection.h" | ||
#include "log_writer.h" | ||
#include <future> | ||
|
||
class logger : public signalr::log_writer | ||
{ | ||
|
@@ -23,13 +24,16 @@ void send_message(signalr::hub_connection& connection, const std::string& messag | |
args[0] = web::json::value(utility::conversions::to_string_t(message)); | ||
|
||
// if you get an internal compiler error uncomment the lambda below or install VS Update 4 | ||
connection.invoke("Send", args) | ||
.then([](pplx::task<web::json::value> invoke_task) // fire and forget but we need to observe exceptions | ||
connection.invoke("Send", args, [](const web::json::value& value, std::exception_ptr exception) | ||
{ | ||
try | ||
{ | ||
auto val = invoke_task.get(); | ||
ucout << U("Received: ") << val.serialize() << std::endl; | ||
if (exception) | ||
{ | ||
std::rethrow_exception(exception); | ||
} | ||
|
||
ucout << U("Received: ") << value.serialize() << std::endl; | ||
} | ||
catch (const std::exception &e) | ||
{ | ||
|
@@ -41,44 +45,63 @@ void send_message(signalr::hub_connection& connection, const std::string& messag | |
void chat() | ||
{ | ||
signalr::hub_connection connection("http://localhost:5000/default", signalr::trace_level::all, std::make_shared<logger>()); | ||
connection.on("Send", [](const web::json::value& m) | ||
connection.on("Send", [](const web::json::value & m) | ||
{ | ||
ucout << std::endl << m.at(0).as_string() << /*U(" wrote:") << m.at(1).as_string() <<*/ std::endl << U("Enter your message: "); | ||
}); | ||
|
||
connection.start() | ||
.then([&connection]() | ||
std::promise<void> task; | ||
connection.start([&connection, &task](std::exception_ptr exception) | ||
{ | ||
if (exception) | ||
{ | ||
ucout << U("Enter your message:"); | ||
for (;;) | ||
try | ||
{ | ||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::rethrow_exception(exception); | ||
} | ||
catch (const std::exception & ex) | ||
{ | ||
std::string message; | ||
std::getline(std::cin, message); | ||
ucout << U("exception when starting connection: ") << ex.what() << std::endl; | ||
} | ||
task.set_value(); | ||
return; | ||
} | ||
|
||
if (message == ":q") | ||
{ | ||
break; | ||
} | ||
ucout << U("Enter your message:"); | ||
for (;;) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while true lol There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 for |
||
{ | ||
std::string message; | ||
std::getline(std::cin, message); | ||
|
||
send_message(connection, message); | ||
if (message == ":q") | ||
{ | ||
break; | ||
} | ||
}) | ||
.then([&connection]() // fine to capture by reference - we are blocking so it is guaranteed to be valid | ||
{ | ||
return connection.stop(); | ||
}) | ||
.then([](pplx::task<void> stop_task) | ||
|
||
send_message(connection, message); | ||
} | ||
|
||
connection.stop([&task](std::exception_ptr exception) | ||
{ | ||
try | ||
{ | ||
stop_task.get(); | ||
if (exception) | ||
{ | ||
std::rethrow_exception(exception); | ||
} | ||
|
||
ucout << U("connection stopped successfully") << std::endl; | ||
} | ||
catch (const std::exception &e) | ||
catch (const std::exception & e) | ||
{ | ||
ucout << U("exception when starting or stopping connection: ") << e.what() << std::endl; | ||
ucout << U("exception when stopping connection: ") << e.what() << std::endl; | ||
} | ||
}).get(); | ||
|
||
task.set_value(); | ||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}); | ||
|
||
task.get_future().get(); | ||
} | ||
|
||
int main() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,14 +16,14 @@ namespace signalr | |
// undefinded behavior since we are using an incomplete type. More details here: http://herbsutter.com/gotw/_100/ | ||
connection::~connection() = default; | ||
|
||
pplx::task<void> connection::start() | ||
void connection::start(std::function<void(std::exception_ptr)> callback) noexcept | ||
{ | ||
return m_pImpl->start(); | ||
m_pImpl->start(callback); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need checks for the implementation object here as well? |
||
} | ||
|
||
pplx::task<void> connection::send(const std::string& data) | ||
void connection::send(const std::string& data, std::function<void(std::exception_ptr)> callback) noexcept | ||
{ | ||
return m_pImpl->send(data); | ||
m_pImpl->send(data, callback); | ||
} | ||
|
||
void connection::set_message_received(const message_received_handler& message_received_callback) | ||
|
@@ -41,9 +41,9 @@ namespace signalr | |
m_pImpl->set_client_config(config); | ||
} | ||
|
||
pplx::task<void> connection::stop() | ||
void connection::stop(std::function<void(std::exception_ptr)> callback) noexcept | ||
{ | ||
return m_pImpl->stop(); | ||
m_pImpl->stop(callback); | ||
} | ||
|
||
connection_state connection::get_connection_state() const noexcept | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these callbacks be marked noexcept too?
std::function<void(std::exception_ptr) noexcept>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it, but I don't think it works
https://godbolt.org/z/dI2GuJ