forked from aspnet/SignalR
-
Notifications
You must be signed in to change notification settings - Fork 2
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
[CPP] API sketch #1
Open
BrennanConroy
wants to merge
6
commits into
dev
Choose a base branch
from
brecon/cppAPI
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
235df69
API sketch
BrennanConroy 717d0f1
some cleanup
BrennanConroy c7c49e7
couple things
BrennanConroy 7661b6d
missed namespace
BrennanConroy 449e565
cleanup
BrennanConroy 424b1e3
Split into files, naive implementations
BrennanConroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#pragma once | ||
|
||
#include "transfer_format.h" | ||
#include "http_connection_options.h" | ||
#include <string> | ||
#include <future> | ||
|
||
namespace signalR | ||
{ | ||
namespace impl | ||
{ | ||
class http_connection | ||
{ | ||
public: | ||
http_connection() noexcept | ||
: mFormat(transfer_format::Text) | ||
{ | ||
} | ||
|
||
http_connection(const http_connection_options& options) noexcept | ||
: mOptions(options), mFormat(transfer_format::Text), mUrl(options.Url) | ||
{ | ||
} | ||
|
||
http_connection(http_connection&& rhs) noexcept | ||
: mOptions(rhs.mOptions), mFormat(rhs.mFormat), mUrl(std::forward<std::string>(rhs.mUrl)) | ||
{ | ||
} | ||
|
||
http_connection& operator=(http_connection&& rhs) noexcept | ||
{ | ||
mOptions = rhs.mOptions; | ||
mFormat = rhs.mFormat; | ||
mUrl = std::forward<std::string>(rhs.mUrl); | ||
return *this; | ||
} | ||
|
||
http_connection(const http_connection&) = delete; | ||
http_connection& operator=(const http_connection&) = delete; | ||
|
||
std::future<void> start(transfer_format format) | ||
{ | ||
mFormat = format; | ||
return std::async(std::launch::async, [] {}); | ||
} | ||
|
||
std::future<void> dispose() | ||
{ | ||
return std::async(std::launch::deferred, [] {}); | ||
} | ||
private: | ||
transfer_format mFormat; | ||
http_connection_options mOptions; | ||
std::string mUrl; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
#include <future> | ||
#include <string> | ||
#include "http_connection.h" | ||
#include "transfer_format.h" | ||
|
||
namespace signalR | ||
{ | ||
namespace impl | ||
{ | ||
class http_connection_factory | ||
{ | ||
public: | ||
http_connection_factory(const http_connection_options& options) noexcept | ||
: mOptions(options) | ||
{ | ||
} | ||
|
||
std::future<http_connection> connect(transfer_format format) | ||
{ | ||
return std::async(std::launch::async, [this, format] | ||
{ | ||
auto connection = http_connection(mOptions); | ||
try | ||
{ | ||
connection.start(format).get(); | ||
} | ||
catch (...) | ||
{ | ||
connection.dispose().get(); | ||
throw; | ||
} | ||
|
||
return connection; | ||
}); | ||
} | ||
private: | ||
http_connection_options mOptions; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace signalR | ||
{ | ||
struct http_connection_options | ||
{ | ||
int Transports; | ||
std::string Url; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#pragma once | ||
#ifndef _HUBCONNECTION_H | ||
#define _HUBCONNECTION_H | ||
|
||
#include <map> | ||
#include <future> | ||
#include "http_connection_factory.h" | ||
|
||
namespace signalR | ||
{ | ||
template <typename Protocol> | ||
class hub_connection | ||
{ | ||
public: | ||
explicit hub_connection(impl::http_connection_factory factory, Protocol hubProtocol) noexcept | ||
: mProtocol(hubProtocol), mFactory(factory) | ||
{} | ||
|
||
~hub_connection() {} | ||
|
||
hub_connection(hub_connection &&rhs) noexcept | ||
: mHandlers(std::move(rhs.mHandlers)), mProtocol(std::move(rhs.mProtocol)), mFactory(std::move(rhs.mFactory)), mConnection(std::move(rhs.mConnection)) | ||
{ | ||
} | ||
|
||
hub_connection(const hub_connection&) = delete; | ||
|
||
hub_connection& operator=(const hub_connection&) = delete; | ||
|
||
std::future<void> __cdecl start() | ||
{ | ||
mConnection = mFactory.connect(mProtocol.transferFormat).get(); | ||
|
||
return some_func(); | ||
} | ||
|
||
std::future<void> __cdecl stop() | ||
{ | ||
return some_func(); | ||
} | ||
|
||
void __cdecl on_closed(const std::function<void __cdecl()>& closed_callback); | ||
|
||
template <typename ...T> | ||
void __cdecl on(const std::string& name, const std::function<void __cdecl (T...)>& methodHandler) | ||
{ | ||
mHandlers[name] = [methodHandler, this](const std::string& arguments) | ||
{ | ||
auto tuple = mProtocol.parse_message<T...>(arguments); | ||
invoke_with_args<T...>(methodHandler, tuple); | ||
}; | ||
} | ||
|
||
template <typename R, typename ...T> | ||
std::future<R> invoke(const std::string& method_name, const T&... args) | ||
{ | ||
auto msg = mProtocol.write_message<T...>(args...); | ||
std::cout << "json created: " << msg << std::endl; | ||
return std::async(std::launch::deferred, []() { return R(); }); | ||
} | ||
|
||
template <typename ...T> | ||
std::future<void> send(const std::string& method_name, const T&... args) | ||
{ | ||
auto msg = mProtocol.write_message<T...>(args...); | ||
return std::async(std::launch::deferred, []() { }); | ||
} | ||
|
||
//private: | ||
std::future<void> some_func() | ||
{ | ||
return std::async(std::launch::deferred, []() { std::cout << std::endl; }); | ||
} | ||
|
||
std::map<std::string, std::function<void(const std::string&)>> mHandlers; | ||
Protocol mProtocol; | ||
impl::http_connection_factory mFactory; | ||
impl::http_connection mConnection; | ||
|
||
private: | ||
template <typename T> | ||
void invoke_with_args(const std::function<void(T)>& func, const std::tuple<T>& args) | ||
{ | ||
func(std::get<0>(args)); | ||
} | ||
|
||
template <typename T, typename T2> | ||
void invoke_with_args(const std::function<void(T, T2)>& func, const std::tuple<T, T2>& args) | ||
{ | ||
func(std::get<0>(args), std::get<1>(args)); | ||
} | ||
|
||
template <typename T, typename T2, typename T3> | ||
void invoke_with_args(const std::function<void(T, T2, T3)>& func, const std::tuple<T, T2, T3>& args) | ||
{ | ||
func(std::get<0>(args), std::get<1>(args), std::get<2>(args)); | ||
} | ||
|
||
template <typename T, typename T2, typename T3, typename T4> | ||
void invoke_with_args(const std::function<void(T, T2, T3, T4)>& func, const std::tuple<T, T2, T3, T4>& args) | ||
{ | ||
func(std::get<0>(args), std::get<1>(args), std::get<2>(args), std::get<3>(args)); | ||
} | ||
|
||
template <typename T, typename T2, typename T3, typename T4, typename T5> | ||
void invoke_with_args(const std::function<void(T, T2, T3, T4, T5)>& func, const std::tuple<T, T2, T3, T4, T5>& args) | ||
{ | ||
func(std::get<0>(args), std::get<1>(args), std::get<2>(args), std::get<3>(args), std::get<4>(args)); | ||
} | ||
|
||
template <typename T, typename T2, typename T3, typename T4, typename T5, typename T6> | ||
void invoke_with_args(const std::function<void(T, T2, T3, T4, T5, T6)>& func, const std::tuple<T, T2, T3, T4, T5, T6>& args) | ||
{ | ||
func(std::get<0>(args), std::get<1>(args), std::get<2>(args), std::get<3>(args), std::get<4>(args), std::get<5>(args)); | ||
} | ||
|
||
template <typename T, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> | ||
void invoke_with_args(const std::function<void(T, T2, T3, T4, T5, T6, T7)>& func, const std::tuple<T, T2, T3, T4, T5, T6, T7>& args) | ||
{ | ||
func(std::get<0>(args), std::get<1>(args), std::get<2>(args), std::get<3>(args), std::get<4>(args), std::get<5>(args), std::get<6>(args)); | ||
} | ||
}; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#pragma once | ||
#ifndef _HUBCONNECTIONBUILDER_H | ||
#define _HUBCONNECTIONBUILDER_H | ||
|
||
#include <string> | ||
#include <functional> | ||
#include "json_hub_protocol.h" | ||
#include "hub_connection.h" | ||
#include "http_connection_factory.h" | ||
|
||
namespace signalR | ||
{ | ||
namespace impl | ||
{ | ||
template <typename C> | ||
struct has_parse_message | ||
{ | ||
template <typename U> | ||
static char Test(decltype(&U::parse_message<>)*); | ||
template <typename U> | ||
static unsigned int Test(...); | ||
static const bool value = sizeof(Test<C>(0)) == sizeof(char); | ||
}; | ||
|
||
template <typename C> | ||
struct has_write_message | ||
{ | ||
template <typename U> | ||
static char Test(decltype(&U::write_message<>)*); | ||
template <typename U> | ||
static unsigned int Test(...); | ||
static const bool value = sizeof(Test<C>(0)) == sizeof(char); | ||
}; | ||
|
||
template <typename P> | ||
class hub_connection_builder_impl; | ||
} | ||
|
||
class hub_connection_builder | ||
{ | ||
public: | ||
hub_connection_builder() noexcept | ||
{ | ||
} | ||
|
||
hub_connection_builder& configure_logging() | ||
{ | ||
return *this; | ||
} | ||
|
||
hub_connection_builder& with_url(const std::string& url, std::function<void __cdecl(http_connection_options&)> configure = nullptr) | ||
{ | ||
mOptions.Transports = 1; | ||
mOptions.Url = url; | ||
if (configure) | ||
{ | ||
configure(mOptions); | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
template <typename P> | ||
impl::hub_connection_builder_impl<P> use_protocol(P p) | ||
{ | ||
return impl::hub_connection_builder_impl<P>(*this, p); | ||
} | ||
|
||
hub_connection<json_hub_protocol> build() | ||
{ | ||
return std::move(hub_connection<json_hub_protocol>(mOptions, json_hub_protocol())); | ||
} | ||
|
||
template <typename Protocol> | ||
hub_connection<Protocol> build(Protocol protocol) | ||
{ | ||
static_assert(impl::has_parse_message<Protocol>::value, "parse_message function expected from protocol"); | ||
static_assert(impl::has_write_message<Protocol>::value, "write_message function expected from protocol"); | ||
|
||
return hub_connection<Protocol>(mOptions, protocol); | ||
} | ||
|
||
private: | ||
http_connection_options mOptions; | ||
}; | ||
|
||
namespace impl | ||
{ | ||
template <typename Protocol> | ||
class hub_connection_builder_impl | ||
{ | ||
public: | ||
hub_connection_builder_impl(hub_connection_builder& internalBuilder, Protocol protocol) | ||
: mBuilder(internalBuilder), mProtocol(protocol) | ||
{ | ||
} | ||
|
||
hub_connection_builder_impl<Protocol>& configure_logging() | ||
{ | ||
return *this; | ||
} | ||
|
||
hub_connection_builder_impl<Protocol>& with_url(const std::string& url, std::function<void __cdecl(http_connection_options&)> configure = nullptr) | ||
{ | ||
mBuilder.with_url(url, configure); | ||
return *this; | ||
} | ||
|
||
hub_connection<Protocol> build() | ||
{ | ||
static_assert(has_parse_message<Protocol>::value, "parse_message function expected from protocol"); | ||
static_assert(has_write_message<Protocol>::value, "write_message function expected from protocol"); | ||
return mBuilder.build<Protocol>(mProtocol); | ||
} | ||
private: | ||
hub_connection_builder & mBuilder; | ||
Protocol mProtocol; | ||
}; | ||
} | ||
} | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is there (or could there be) any way for a user to add one of these if they really want to add an invocation that takes more than 7 args?