Skip to content

Commit

Permalink
Merge branch 'qicosmos:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
wf0312 authored Jan 28, 2024
2 parents ee3e1c3 + 439925f commit fb7f903
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 134 deletions.
10 changes: 5 additions & 5 deletions examples/client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ void test_echo() {
}

{
auto result = client.call<std::string>("async_echo", "test");
auto result = client.call<std::string>("delay_echo", "test");
std::cout << result << std::endl;
}
}
Expand Down Expand Up @@ -349,19 +349,19 @@ void test_callback() {
rpc_client client;
bool r = client.connect("127.0.0.1", 9000);

for (size_t i = 0; i < 100; i++) {
for (size_t i = 0; i < 10; i++) {
std::string test = "test" + std::to_string(i + 1);
// set timeout 100ms
client.async_call<100>(
"async_echo",
client.async_call<10000>(
"delay_echo",
[](const asio::error_code &ec, string_view data) {
if (ec) {
std::cout << ec.value() << " timeout" << std::endl;
return;
}

auto str = as<std::string>(data);
std::cout << "echo " << str << '\n';
std::cout << "delay echo " << str << '\n';
},
test);

Expand Down
14 changes: 7 additions & 7 deletions examples/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ std::string get_name(rpc_conn conn, const person &p) {

// if you want to response later, you can use async model, you can control when
// to response
void async_echo(rpc_conn conn, const std::string &src) {
auto req_id =
conn.lock()->request_id(); // note: you need keep the request id at that
// time, and pass it into the async thread

void delay_echo(rpc_conn conn, const std::string &src) {
auto sp = conn.lock();
sp->set_delay(true);
auto req_id = sp->request_id(); // note: you need keep the request id at that
// time, and pass it into the async thread
std::thread thd([conn, req_id, src] {
std::this_thread::sleep_for(std::chrono::seconds(1));
auto conn_sp = conn.lock();
Expand Down Expand Up @@ -121,7 +121,7 @@ dummy1 get_dummy(rpc_conn conn, dummy1 d) { return d; }

int main() {
// benchmark_test();
rpc_server server(9000, std::thread::hardware_concurrency());
rpc_server server(9000, std::thread::hardware_concurrency(), 3600);

dummy d;
server.register_handler("add", &dummy::add, &d);
Expand All @@ -135,7 +135,7 @@ int main() {
server.register_handler("upload", upload);
server.register_handler("download", download);
server.register_handler("get_name", get_name);
server.register_handler<Async>("async_echo", async_echo);
server.register_handler("delay_echo", delay_echo);
server.register_handler("echo", echo);
server.register_handler("get_int", get_int);

Expand Down
13 changes: 11 additions & 2 deletions include/rest_rpc/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class connection : public std::enable_shared_from_this<connection>,
callback_ = std::move(callback);
}

void set_delay(bool delay) { delay_ = delay; }

void on_network_error(std::function<void(std::shared_ptr<connection>,
std::string)> &on_net_err) {
on_net_err_ = &on_net_err;
Expand Down Expand Up @@ -208,8 +210,14 @@ class connection : public std::enable_shared_from_this<connection>,
if (!ec) {
read_head();
if (req_type_ == request_type::req_res) {
router_.route<connection>(func_id, body_.data(), length,
this->shared_from_this());
route_result_t ret = router_.route<connection>(
func_id, nonstd::string_view{body_.data(), length},
this->shared_from_this());
if (delay_) {
delay_ = false;
} else {
response(req_id_, std::move(ret.result));
}
} else if (req_type_ == request_type::sub_pub) {
try {
msgpack_codec codec;
Expand Down Expand Up @@ -420,6 +428,7 @@ class connection : public std::enable_shared_from_this<connection>,
nullptr;
router &router_;
nonstd::any user_data_;
bool delay_ = false;
};
} // namespace rpc_service
} // namespace rest_rpc
Expand Down
37 changes: 18 additions & 19 deletions include/rest_rpc/cplusplus_14.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <tuple>
#include <type_traits>

#if __cplusplus == 201103L

namespace std {
template <class T> struct unique_if { typedef unique_ptr<T> single_object; };
namespace nonstd {
template <class T> struct unique_if {
typedef std::unique_ptr<T> single_object;
};

template <class T> struct unique_if<T[]> {
typedef unique_ptr<T[]> unknown_bound;
typedef std::unique_ptr<T[]> unknown_bound;
};

template <class T, size_t N> struct unique_if<T[N]> {
Expand All @@ -20,12 +20,12 @@ template <class T, size_t N> struct unique_if<T[N]> {

template <class T, class... Args>
typename unique_if<T>::single_object make_unique(Args &&...args) {
return unique_ptr<T>(new T(forward<Args>(args)...));
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

template <class T> typename unique_if<T>::unknown_bound make_unique(size_t n) {
typedef typename remove_extent<T>::type U;
return unique_ptr<T>(new U[n]());
typedef typename std::remove_extent<T>::type U;
return std::unique_ptr<T>(new U[n]());
}

template <class T, class... Args>
Expand Down Expand Up @@ -59,31 +59,32 @@ template <typename... T>
using index_sequence_for = make_index_sequence<sizeof...(T)>;

template <bool B, class T = void>
using enable_if_t = typename enable_if<B, T>::type;
using enable_if_t = typename std::enable_if<B, T>::type;

template <typename T> using remove_const_t = typename remove_const<T>::type;
template <typename T>
using remove_const_t = typename std::remove_const<T>::type;

template <typename T>
using remove_reference_t = typename remove_reference<T>::type;
using remove_reference_t = typename std::remove_reference<T>::type;

template <int I, typename T>
using tuple_element_t = typename tuple_element<I, T>::type;
using tuple_element_t = typename std::tuple_element<I, T>::type;

template <typename T> using decay_t = typename decay<T>::type;
template <typename T> using decay_t = typename std::decay<T>::type;

template <typename F, typename Tuple, size_t... Idx>
auto apply_helper(F &&f, Tuple &&tp, std::index_sequence<Idx...>)
auto apply_helper(F &&f, Tuple &&tp, nonstd::index_sequence<Idx...>)
-> decltype(std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(tp))...)) {
return std::forward<F>(f)(std::get<Idx>(std::forward<Tuple>(tp))...);
}

template <typename F, typename Tuple>
auto apply(F &&f, Tuple &&tp) -> decltype(apply_helper(
std::forward<F>(f), std::forward<Tuple>(tp),
std::make_index_sequence<std::tuple_size<decay_t<Tuple>>::value>{})) {
make_index_sequence<std::tuple_size<decay_t<Tuple>>::value>{})) {
return apply_helper(
std::forward<F>(f), std::forward<Tuple>(tp),
std::make_index_sequence<std::tuple_size<decay_t<Tuple>>::value>{});
make_index_sequence<std::tuple_size<decay_t<Tuple>>::value>{});
}

template <typename F, typename... Args>
Expand All @@ -92,8 +93,6 @@ auto invoke(F &&f, Args &&...args)
return std::forward<F>(f)(std::forward<Args>(args)...);
}

} // namespace std

#endif
} // namespace nonstd

#endif // REST_RPC_CPLUSPLUS_14_H_
27 changes: 14 additions & 13 deletions include/rest_rpc/meta_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace rest_rpc {

template <typename... Args, typename Func, std::size_t... Idx>
void for_each(const std::tuple<Args...> &t, Func &&f,
std::index_sequence<Idx...>) {
nonstd::index_sequence<Idx...>) {
(void)std::initializer_list<int>{(f(std::get<Idx>(t)), void(), 0)...};
}

template <typename... Args, typename Func, std::size_t... Idx>
void for_each_i(const std::tuple<Args...> &t, Func &&f,
std::index_sequence<Idx...>) {
nonstd::index_sequence<Idx...>) {
(void)std::initializer_list<int>{
(f(std::get<Idx>(t), std::integral_constant<size_t, Idx>{}), void(),
0)...};
Expand All @@ -32,14 +32,15 @@ struct function_traits<Ret(Arg, Args...)> {
typedef Ret (*pointer)(Arg, Args...);

typedef std::tuple<Arg, Args...> tuple_type;
typedef std::tuple<std::remove_const_t<std::remove_reference_t<Args>>...>
typedef std::tuple<
nonstd::remove_const_t<nonstd::remove_reference_t<Args>>...>
bare_tuple_type;
using args_tuple =
std::tuple<std::string, Arg,
std::remove_const_t<std::remove_reference_t<Args>>...>;
nonstd::remove_const_t<nonstd::remove_reference_t<Args>>...>;
using args_tuple_2nd =
std::tuple<std::string,
std::remove_const_t<std::remove_reference_t<Args>>...>;
nonstd::remove_const_t<nonstd::remove_reference_t<Args>>...>;
};

template <typename Ret> struct function_traits<Ret()> {
Expand Down Expand Up @@ -76,24 +77,24 @@ struct function_traits : function_traits<decltype(&Callable::operator())> {};

template <typename T>
using remove_const_reference_t =
std::remove_const_t<std::remove_reference_t<T>>;
nonstd::remove_const_t<nonstd::remove_reference_t<T>>;

template <size_t... Is>
auto make_tuple_from_sequence(std::index_sequence<Is...>)
auto make_tuple_from_sequence(nonstd::index_sequence<Is...>)
-> decltype(std::make_tuple(Is...)) {
std::make_tuple(Is...);
}

template <size_t N>
constexpr auto make_tuple_from_sequence()
-> decltype(make_tuple_from_sequence(std::make_index_sequence<N>{})) {
return make_tuple_from_sequence(std::make_index_sequence<N>{});
-> decltype(make_tuple_from_sequence(nonstd::make_index_sequence<N>{})) {
return make_tuple_from_sequence(nonstd::make_index_sequence<N>{});
}

namespace detail {
template <class Tuple, class F, std::size_t... Is>
void tuple_switch(const std::size_t i, Tuple &&t, F &&f,
std::index_sequence<Is...>) {
nonstd::index_sequence<Is...>) {
(void)std::initializer_list<int>{
(i == Is &&
((void)std::forward<F>(f)(std::integral_constant<size_t, Is>{}), 0))...};
Expand All @@ -102,14 +103,14 @@ void tuple_switch(const std::size_t i, Tuple &&t, F &&f,

template <class Tuple, class F>
inline void tuple_switch(const std::size_t i, Tuple &&t, F &&f) {
constexpr auto N = std::tuple_size<std::remove_reference_t<Tuple>>::value;
constexpr auto N = std::tuple_size<nonstd::remove_reference_t<Tuple>>::value;

detail::tuple_switch(i, std::forward<Tuple>(t), std::forward<F>(f),
std::make_index_sequence<N>{});
nonstd::make_index_sequence<N>{});
}

template <int N, typename... Args>
using nth_type_of = std::tuple_element_t<N, std::tuple<Args...>>;
using nth_type_of = nonstd::tuple_element_t<N, std::tuple<Args...>>;

template <typename... Args>
using last_type_of = nth_type_of<sizeof...(Args) - 1, Args...>;
Expand Down
Loading

0 comments on commit fb7f903

Please sign in to comment.