Skip to content
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
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions clients/cpp/CPPSketch/http_connection.h
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;
};
}
}
42 changes: 42 additions & 0 deletions clients/cpp/CPPSketch/http_connection_factory.h
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;
};
}
}
12 changes: 12 additions & 0 deletions clients/cpp/CPPSketch/http_connection_options.h
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;
};
}
124 changes: 124 additions & 0 deletions clients/cpp/CPPSketch/hub_connection.h
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)

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?

{
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
121 changes: 121 additions & 0 deletions clients/cpp/CPPSketch/hub_connection_builder.h
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
Loading