Skip to content

Add negotiation response support #7675

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

Merged
merged 9 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/SignalR/clients/cpp/include/signalrclient/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ namespace signalr
public:
typedef std::function<void __cdecl(const utility::string_t&)> message_received_handler;

SIGNALRCLIENT_API explicit connection(const utility::string_t& url, const utility::string_t& query_string = _XPLATSTR(""),
trace_level trace_level = trace_level::all, std::shared_ptr<log_writer> log_writer = nullptr);
SIGNALRCLIENT_API explicit connection(const utility::string_t& url, trace_level trace_level = trace_level::all, std::shared_ptr<log_writer> log_writer = nullptr);

SIGNALRCLIENT_API ~connection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace signalr
public:
typedef std::function<void __cdecl (const web::json::value&)> method_invoked_handler;

SIGNALRCLIENT_API explicit hub_connection(const utility::string_t& url, const utility::string_t& query_string = _XPLATSTR(""),
trace_level trace_level = trace_level::all, std::shared_ptr<log_writer> log_writer = nullptr);
SIGNALRCLIENT_API explicit hub_connection(const utility::string_t& url, trace_level trace_level = trace_level::all,
std::shared_ptr<log_writer> log_writer = nullptr);

SIGNALRCLIENT_API ~hub_connection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void send_message(signalr::hub_connection& connection, const utility::string_t&

void chat(const utility::string_t& name)
{
signalr::hub_connection connection(U("http://localhost:5000/default"), U(""), signalr::trace_level::all, std::make_shared<logger>());
signalr::hub_connection connection(U("http://localhost:5000/default"), signalr::trace_level::all, std::make_shared<logger>());
connection.on(U("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: ");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\Build\SignalRClient.Build.Settings" />
<ItemGroup Label="ProjectConfigurations">
Expand Down Expand Up @@ -36,7 +36,6 @@
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Import Project="..\..\packages\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.2.9.1\build\native\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.targets" Condition="Exists('..\..\packages\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.2.9.1\build\native\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.targets')" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
Expand Down Expand Up @@ -110,10 +109,10 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
<Error Condition="!Exists('..\..\packages\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.2.9.1\build\native\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.2.9.1\build\native\cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn.targets'))" />
</Target>
<Target Name="AfterBuild">
<Exec Command="copy /y &quot;$(SolutionDir)bin\Desktop\$(Platform)\$(Configuration)\dll\$(SignalrClientTargetName).dll&quot; &quot;$(SolutionDir)$(Configuration)\$(SignalrClientTargetName).dll&quot;" />
<Exec Command="copy /y &quot;$(SolutionDir)bin\Desktop\$(Platform)\$(Configuration)\dll\$(SignalrClientTargetName).dll&quot; &quot;$(SolutionDir)bin\Desktop\$(Platform)\$(Configuration)\$(SignalrClientTargetName).dll&quot;" />
<Exec Command="copy /y &quot;$(SolutionDir)bin\Desktop\$(Platform)\$(Configuration)\dll\$(SignalrClientTargetName).pdb&quot; &quot;$(SolutionDir)bin\Desktop\$(Platform)\$(Configuration)\$(SignalrClientTargetName).pdb&quot;" />
</Target>
</Project>
</Project>
4 changes: 2 additions & 2 deletions src/SignalR/clients/cpp/src/signalrclient/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace signalr
{
connection::connection(const utility::string_t& url, const utility::string_t& query_string, trace_level trace_level, std::shared_ptr<log_writer> log_writer)
: m_pImpl(connection_impl::create(url, query_string, trace_level, std::move(log_writer)))
connection::connection(const utility::string_t& url, trace_level trace_level, std::shared_ptr<log_writer> log_writer)
: m_pImpl(connection_impl::create(url, trace_level, std::move(log_writer)))
{}

// Do NOT remove this destructor. Letting the compiler generate and inline the default dtor may lead to
Expand Down
186 changes: 113 additions & 73 deletions src/SignalR/clients/cpp/src/signalrclient/connection_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ namespace signalr
static void log(const logger& logger, trace_level level, const utility::string_t& entry);
}

std::shared_ptr<connection_impl> connection_impl::create(const utility::string_t& url, const utility::string_t& query_string,
trace_level trace_level, const std::shared_ptr<log_writer>& log_writer)
std::shared_ptr<connection_impl> connection_impl::create(const utility::string_t& url, trace_level trace_level, const std::shared_ptr<log_writer>& log_writer)
{
return connection_impl::create(url, query_string, trace_level, log_writer, std::make_unique<web_request_factory>(), std::make_unique<transport_factory>());
return connection_impl::create(url, trace_level, log_writer, std::make_unique<web_request_factory>(), std::make_unique<transport_factory>());
}

std::shared_ptr<connection_impl> connection_impl::create(const utility::string_t& url, const utility::string_t& query_string, trace_level trace_level,
const std::shared_ptr<log_writer>& log_writer, std::unique_ptr<web_request_factory> web_request_factory, std::unique_ptr<transport_factory> transport_factory)
std::shared_ptr<connection_impl> connection_impl::create(const utility::string_t& url, trace_level trace_level, const std::shared_ptr<log_writer>& log_writer,
std::unique_ptr<web_request_factory> web_request_factory, std::unique_ptr<transport_factory> transport_factory)
{
return std::shared_ptr<connection_impl>(new connection_impl(url, query_string, trace_level,
return std::shared_ptr<connection_impl>(new connection_impl(url, trace_level,
log_writer ? log_writer : std::make_shared<trace_log_writer>(), std::move(web_request_factory), std::move(transport_factory)));
}

connection_impl::connection_impl(const utility::string_t& url, const utility::string_t& query_string, trace_level trace_level, const std::shared_ptr<log_writer>& log_writer,
connection_impl::connection_impl(const utility::string_t& url, trace_level trace_level, const std::shared_ptr<log_writer>& log_writer,
std::unique_ptr<web_request_factory> web_request_factory, std::unique_ptr<transport_factory> transport_factory)
: m_base_url(url), m_query_string(query_string), m_connection_state(connection_state::disconnected), m_logger(log_writer, trace_level),
: m_base_url(url), m_connection_state(connection_state::disconnected), m_logger(log_writer, trace_level),
m_transport(nullptr), m_web_request_factory(std::move(web_request_factory)), m_transport_factory(std::move(transport_factory)),
m_message_received([](const utility::string_t&) noexcept {}), m_disconnected([]() noexcept {})
{ }
Expand Down Expand Up @@ -84,94 +83,138 @@ namespace signalr
m_message_id = m_groups_token = m_connection_id = _XPLATSTR("");
}

return start_negotiate(m_base_url, 0);
}

pplx::task<void> connection_impl::start_negotiate(const web::uri& url, int redirect_count)
{
if (redirect_count >= MAX_NEGOTIATE_REDIRECTS)
{
return pplx::task_from_exception<void>(signalr_exception(_XPLATSTR("Negotiate redirection limit exceeded.")));
}

pplx::task_completion_event<void> start_tce;

auto weak_connection = weak_from_this();

pplx::task_from_result()
.then([weak_connection]()
.then([weak_connection, url]()
{
auto connection = weak_connection.lock();
if (!connection)
{
auto connection = weak_connection.lock();
if (!connection)
return pplx::task_from_exception<negotiation_response>(_XPLATSTR("connection no longer exists"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalization and periods for the exception messages

Copy link
Member Author

@BrennanConroy BrennanConroy Feb 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do an exception/log message pass in a different PR as there are a lot that need to be updated.

Updated the giant list with log and exception messages as an item.

}
return request_sender::negotiate(*connection->m_web_request_factory, url, connection->m_signalr_client_config);
}, m_disconnect_cts.get_token())
.then([weak_connection, start_tce, redirect_count, url](negotiation_response negotiation_response)
{
auto connection = weak_connection.lock();
if (!connection)
{
return pplx::task_from_exception<void>(_XPLATSTR("connection no longer exists"));
}

if (!negotiation_response.error.empty())
{
return pplx::task_from_exception<void>(signalr_exception(negotiation_response.error));
}

if (!negotiation_response.url.empty())
{
if (!negotiation_response.accessToken.empty())
{
return pplx::task_from_exception<negotiation_response>(_XPLATSTR("connection no longer exists"));
auto headers = connection->m_signalr_client_config.get_http_headers();
headers[_XPLATSTR("Authorization")] = _XPLATSTR("Bearer ") + negotiation_response.accessToken;
connection->m_signalr_client_config.set_http_headers(headers);
}
return request_sender::negotiate(*connection->m_web_request_factory, connection->m_base_url,
connection->m_query_string, connection->m_signalr_client_config);
}, m_disconnect_cts.get_token())
.then([weak_connection](negotiation_response negotiation_response)
return connection->start_negotiate(negotiation_response.url, redirect_count + 1);
}

connection->m_connection_id = std::move(negotiation_response.connectionId);

// TODO: fallback logic

bool foundWebsockets = false;
for (auto availableTransport : negotiation_response.availableTransports)
{
auto connection = weak_connection.lock();
if (!connection)
if (availableTransport.transport == _XPLATSTR("WebSockets"))
{
return pplx::task_from_exception<void>(_XPLATSTR("connection no longer exists"));
foundWebsockets = true;
break;
}
connection->m_connection_id = std::move(negotiation_response.connection_id);
}

// TODO: check available transports
if (!foundWebsockets)
{
return pplx::task_from_exception<void>(signalr_exception(_XPLATSTR("The server does not support WebSockets which is currently the only transport supported by this client.")));
}

return connection->start_transport()
.then([weak_connection](std::shared_ptr<transport> transport)
{
auto connection = weak_connection.lock();
if (!connection)
{
return pplx::task_from_exception<void>(_XPLATSTR("connection no longer exists"));
}
connection->m_transport = transport;
return pplx::task_from_result();
});
}, m_disconnect_cts.get_token())
.then([start_tce, weak_connection](pplx::task<void> previous_task)
// TODO: use transfer format

return connection->start_transport(url)
.then([weak_connection, start_tce](std::shared_ptr<transport> transport)
{
auto connection = weak_connection.lock();
if (!connection)
{
return pplx::task_from_exception<void>(_XPLATSTR("connection no longer exists"));
}
try
{
previous_task.get();
if (!connection->change_state(connection_state::connecting, connection_state::connected))
{
connection->m_logger.log(trace_level::errors,
utility::string_t(_XPLATSTR("internal error - transition from an unexpected state. expected state: connecting, actual state: "))
.append(translate_connection_state(connection->get_connection_state())));

_ASSERTE(false);
}
connection->m_transport = transport;

connection->m_start_completed_event.set();
start_tce.set();
}
catch (const std::exception &e)
if (!connection->change_state(connection_state::connecting, connection_state::connected))
{
auto task_canceled_exception = dynamic_cast<const pplx::task_canceled *>(&e);
if (task_canceled_exception)
{
connection->m_logger.log(trace_level::info,
_XPLATSTR("starting the connection has been canceled."));
}
else
{
connection->m_logger.log(trace_level::errors,
utility::string_t(_XPLATSTR("connection could not be started due to: "))
.append(utility::conversions::to_string_t(e.what())));
}
connection->m_logger.log(trace_level::errors,
utility::string_t(_XPLATSTR("internal error - transition from an unexpected state. expected state: connecting, actual state: "))
.append(translate_connection_state(connection->get_connection_state())));

connection->m_transport = nullptr;
connection->change_state(connection_state::disconnected);
connection->m_start_completed_event.set();
start_tce.set_exception(std::current_exception());
_ASSERTE(false);
}

return pplx::task_from_result();
});
}, m_disconnect_cts.get_token())
.then([start_tce, weak_connection](pplx::task<void> previous_task)
{
auto connection = weak_connection.lock();
if (!connection)
{
return pplx::task_from_exception<void>(_XPLATSTR("connection no longer exists"));
}
try
{
previous_task.get();
connection->m_start_completed_event.set();
start_tce.set();
}
catch (const std::exception & e)
{
auto task_canceled_exception = dynamic_cast<const pplx::task_canceled*>(&e);
if (task_canceled_exception)
{
connection->m_logger.log(trace_level::info,
_XPLATSTR("starting the connection has been canceled."));
}
else
{
connection->m_logger.log(trace_level::errors,
utility::string_t(_XPLATSTR("connection could not be started due to: "))
.append(utility::conversions::to_string_t(e.what())));
}

connection->m_transport = nullptr;
connection->change_state(connection_state::disconnected);
connection->m_start_completed_event.set();
start_tce.set_exception(std::current_exception());
}

return pplx::task_from_result();
});

return pplx::create_task(start_tce);
}

pplx::task<std::shared_ptr<transport>> connection_impl::start_transport()
pplx::task<std::shared_ptr<transport>> connection_impl::start_transport(const web::uri& url)
{
auto connection = shared_from_this();

Expand Down Expand Up @@ -247,18 +290,15 @@ namespace signalr
}
});

return connection->send_connect_request(transport, connect_request_tce)
return connection->send_connect_request(transport, url, connect_request_tce)
.then([transport](){ return pplx::task_from_result(transport); });
}

pplx::task<void> connection_impl::send_connect_request(const std::shared_ptr<transport>& transport, const pplx::task_completion_event<void>& connect_request_tce)
pplx::task<void> connection_impl::send_connect_request(const std::shared_ptr<transport>& transport, const web::uri& url, const pplx::task_completion_event<void>& connect_request_tce)
{
auto logger = m_logger;
auto query_string = m_query_string;
if (!query_string.empty())
query_string.append(_XPLATSTR("&"));
query_string.append(_XPLATSTR("id=")).append(m_connection_id);
auto connect_url = url_builder::build_connect(m_base_url, transport->get_transport_type(), query_string);
auto query_string = _XPLATSTR("id=" + m_connection_id);
auto connect_url = url_builder::build_connect(url, transport->get_transport_type(), query_string);

transport->connect(connect_url)
.then([transport, connect_request_tce, logger](pplx::task<void> connect_task)
Expand Down
Loading