Skip to content

C++ client low level API #8420

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 19 commits into from
Mar 25, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public async Task RegisterIPEndPoint_IPv6StaticPort_Success()
[ConditionalTheory]
[MemberData(nameof(IPEndPointRegistrationDataDynamicPort))]
[IPv6SupportedCondition]
[Flaky("https://github.com/aspnet/AspNetCore-Internal/issues/2074", FlakyOn.AzP.macOS)]
public async Task RegisterIPEndPoint_DynamicPort_Success(IPEndPoint endPoint, string testUrl)
{
await RegisterIPEndPoint_Success(endPoint, testUrl);
Expand Down Expand Up @@ -447,6 +448,7 @@ public Task DefaultsServerAddress_BindsToIPv4WithHttps()

[ConditionalFact]
[IPv6SupportedCondition]
[Flaky("https://github.com/aspnet/AspNetCore-Internal/issues/1756", FlakyOn.AzP.macOS)]
public Task DefaultsServerAddress_BindsToIPv6WithHttps()
{
if (!CanBindToEndpoint(IPAddress.Loopback, 5000) || !CanBindToEndpoint(IPAddress.IPv6Loopback, 5000)
Expand Down
54 changes: 54 additions & 0 deletions src/SignalR/clients/cpp/include/signalrclient/http_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#pragma once

#include <string>
#include <functional>
#include <map>
#include <chrono>

namespace signalr
{
enum class http_method
{
GET,
POST
};

class http_request
{
public:
http_method method;
std::map<std::string, std::string> headers;
std::string content;
std::chrono::seconds timeout;
};

class http_response
{
public:
http_response() {}
http_response(http_response&& rhs) noexcept : status_code(rhs.status_code), content(std::move(rhs.content)) {}
http_response(int code, const std::string& content) : status_code(code), content(content) {}

http_response& operator=(http_response&& rhs)
{
status_code = rhs.status_code;
content = std::move(rhs.content);

return *this;
}

int status_code = 0;
std::string content;
};

class http_client
{
public:
virtual void send(std::string url, http_request request, std::function<void(http_response, std::exception_ptr)> callback) = 0;

virtual ~http_client() {}
};
}
13 changes: 13 additions & 0 deletions src/SignalR/clients/cpp/include/signalrclient/transfer_format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#pragma once

namespace signalr
{
enum class transfer_format
{
text,
binary
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ namespace signalr
long_polling,
websockets
};
}
}
23 changes: 23 additions & 0 deletions src/SignalR/clients/cpp/include/signalrclient/websocket_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#pragma once

#include "transfer_format.h"

namespace signalr
{
class websocket_client
{
public:
virtual ~websocket_client() {};

virtual void start(std::string url, transfer_format format, std::function<void(std::exception_ptr)> callback) = 0;

virtual void stop(std::function<void(std::exception_ptr)> callback) = 0;

virtual void send(std::string payload, std::function<void(std::exception_ptr)> callback) = 0;

virtual void receive(std::function<void(std::string, std::exception_ptr)> callback) = 0;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ class logger : public signalr::log_writer
}
};

void send_message(signalr::hub_connection& connection, const std::string& name, const std::string& message)
void send_message(signalr::hub_connection& connection, const std::string& message)
{
web::json::value args{};
args[0] = web::json::value::string(utility::conversions::to_string_t(name));
args[1] = web::json::value(utility::conversions::to_string_t(message));
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("Invoke", args/*, [](const web::json::value&){}*/)
connection.invoke("Send", args)
.then([](pplx::task<web::json::value> invoke_task) // fire and forget but we need to observe exceptions
{
try
Expand All @@ -39,7 +38,7 @@ void send_message(signalr::hub_connection& connection, const std::string& name,
});
}

void chat(const std::string& name)
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)
Expand All @@ -48,7 +47,7 @@ void chat(const std::string& name)
});

connection.start()
.then([&connection, name]()
.then([&connection]()
{
ucout << U("Enter your message:");
for (;;)
Expand All @@ -61,7 +60,7 @@ void chat(const std::string& name)
break;
}

send_message(connection, name, message);
send_message(connection, message);
}
})
.then([&connection]() // fine to capture by reference - we are blocking so it is guaranteed to be valid
Expand All @@ -84,11 +83,7 @@ void chat(const std::string& name)

int main()
{
ucout << U("Enter your name: ");
std::string name;
std::getline(std::cin, name);

chat(name);
chat();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,35 @@
<ItemGroup>
<ClInclude Include="..\..\..\..\include\signalrclient\connection.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\connection_state.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\http_client.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\hub_connection.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\hub_exception.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\log_writer.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\signalr_client_config.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\signalr_exception.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\trace_level.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\transfer_format.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\transport_type.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\websocket_client.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\web_exception.h" />
<ClInclude Include="..\..\..\..\include\signalrclient\_exports.h" />
<ClInclude Include="..\..\case_insensitive_comparison_utils.h" />
<ClInclude Include="..\..\connection_impl.h" />
<ClInclude Include="..\..\constants.h" />
<ClInclude Include="..\..\default_http_client.h" />
<ClInclude Include="..\..\default_websocket_client.h" />
<ClInclude Include="..\..\event.h" />
<ClInclude Include="..\..\http_sender.h" />
<ClInclude Include="..\..\hub_connection_impl.h" />
<ClInclude Include="..\..\callback_manager.h" />
<ClInclude Include="..\..\logger.h" />
<ClInclude Include="..\..\negotiation_response.h" />
<ClInclude Include="..\..\request_sender.h" />
<ClInclude Include="..\..\negotiate.h" />
<ClInclude Include="..\..\stdafx.h" />
<ClInclude Include="..\..\trace_log_writer.h" />
<ClInclude Include="..\..\transport.h" />
<ClInclude Include="..\..\transport_factory.h" />
<ClInclude Include="..\..\url_builder.h" />
<ClInclude Include="..\..\websocket_client.h" />
<ClInclude Include="..\..\websocket_transport.h" />
<ClInclude Include="..\..\web_request.h" />
<ClInclude Include="..\..\web_request_factory.h" />
Expand All @@ -75,12 +78,13 @@
<ItemGroup>
<ClCompile Include="..\..\connection.cpp" />
<ClCompile Include="..\..\connection_impl.cpp" />
<ClCompile Include="..\..\default_http_client.cpp" />
<ClCompile Include="..\..\http_sender.cpp" />
<ClCompile Include="..\..\hub_connection.cpp" />
<ClCompile Include="..\..\hub_connection_impl.cpp" />
<ClCompile Include="..\..\callback_manager.cpp" />
<ClCompile Include="..\..\logger.cpp" />
<ClCompile Include="..\..\request_sender.cpp" />
<ClCompile Include="..\..\negotiate.cpp" />
<ClCompile Include="..\..\signalr_client_config.cpp" />
<ClCompile Include="..\..\stdafx.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
Expand All @@ -105,4 +109,4 @@
<PackageReference Include="cpprestsdk.v140.windesktop.msvcstl.dyn.rt-dyn" Version="2.9.1" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@
<ClInclude Include="..\..\negotiation_response.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\request_sender.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\websocket_transport.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand All @@ -57,9 +54,6 @@
<ClInclude Include="..\..\..\..\include\signalrclient\connection_state.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\websocket_client.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\default_websocket_client.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -111,6 +105,21 @@
<ClInclude Include="..\..\..\..\include\signalrclient\signalr_client_config.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\default_http_client.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\negotiate.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\signalrclient\http_client.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\signalrclient\transfer_format.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\signalrclient\websocket_client.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\stdafx.cpp">
Expand Down Expand Up @@ -164,7 +173,10 @@
<ClCompile Include="..\..\signalr_client_config.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\request_sender.cpp">
<ClCompile Include="..\..\default_http_client.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\negotiate.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
Expand Down
Loading