diff --git a/examples/client/CMakeLists.txt b/examples/client/CMakeLists.txt index e449365..e45eb43 100644 --- a/examples/client/CMakeLists.txt +++ b/examples/client/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.7) project(basic_client) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++17") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11") find_package(Boost COMPONENTS system REQUIRED) include_directories( diff --git a/examples/client/basic_client.vcxproj b/examples/client/basic_client.vcxproj index 02aba23..1af657f 100644 --- a/examples/client/basic_client.vcxproj +++ b/examples/client/basic_client.vcxproj @@ -87,7 +87,6 @@ Disabled true true - stdcpp17 4996 @@ -97,7 +96,6 @@ Disabled true true - stdcpp17 4996 @@ -109,7 +107,6 @@ true true true - stdcpp17 4996 @@ -125,7 +122,6 @@ true true true - stdcpp17 4996 diff --git a/examples/client/main.cpp b/examples/client/main.cpp index 2382654..315dac3 100644 --- a/examples/client/main.cpp +++ b/examples/client/main.cpp @@ -256,7 +256,6 @@ void test_performance2() { } int main() { - test_performance1(); test_sync_client(); test_async_client(); //test_upload(); diff --git a/examples/server/CMakeLists.txt b/examples/server/CMakeLists.txt index 79171d6..4e5d730 100644 --- a/examples/server/CMakeLists.txt +++ b/examples/server/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.7) project(basic_server) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++17") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11") find_package(Boost COMPONENTS system REQUIRED) include_directories( diff --git a/examples/server/basic_server.vcxproj b/examples/server/basic_server.vcxproj index 999f4af..58a393d 100644 --- a/examples/server/basic_server.vcxproj +++ b/examples/server/basic_server.vcxproj @@ -87,7 +87,6 @@ Disabled true true - stdcpp17 4996 @@ -97,7 +96,6 @@ Disabled true true - stdcpp17 4996 @@ -109,7 +107,6 @@ true true true - stdcpp17 4996 @@ -125,7 +122,6 @@ true true true - stdcpp17 4996 diff --git a/examples/server/main.cpp b/examples/server/main.cpp index 49bfe95..acd9ac9 100644 --- a/examples/server/main.cpp +++ b/examples/server/main.cpp @@ -68,7 +68,7 @@ std::string get_name(connection* conn, const person& p) { } int main() { - rpc_server server(9000, 4); + rpc_server server(9000, std::thread::hardware_concurrency()); dummy d; server.register_handler("add", &dummy::add, &d); diff --git a/include/client_util.hpp b/include/client_util.hpp index b3d236c..44cdcf8 100644 --- a/include/client_util.hpp +++ b/include/client_util.hpp @@ -1,9 +1,16 @@ #pragma once +#include +#if __cplusplus > 201402L #include +using string_view = std::string_view; +#else +#include +using string_view = boost::string_view; +#endif #include "codec.h" namespace rest_rpc { - inline bool has_error(std::string_view result) { + inline bool has_error(string_view result) { rpc_service::msgpack_codec codec; auto tp = codec.unpack>(result.data(), result.size()); @@ -11,7 +18,7 @@ namespace rest_rpc { } template - inline T get_result(std::string_view result) { + inline T get_result(string_view result) { rpc_service::msgpack_codec codec; auto tp = codec.unpack>(result.data(), result.size()); if (std::get<0>(tp) != 0) { diff --git a/include/cplusplus_14.h b/include/cplusplus_14.h index 487ced7..a72592a 100644 --- a/include/cplusplus_14.h +++ b/include/cplusplus_14.h @@ -1,6 +1,9 @@ #ifndef REST_RPC_CPLUSPLUS_14_H_ #define REST_RPC_CPLUSPLUS_14_H_ +#include +#include + #if __cplusplus == 201103L namespace std { diff --git a/include/rpc_client.hpp b/include/rpc_client.hpp index 35f6b1d..bd89bbf 100644 --- a/include/rpc_client.hpp +++ b/include/rpc_client.hpp @@ -18,7 +18,7 @@ namespace rest_rpc { class req_result { public: req_result() = default; - req_result(std::string_view data) : data_(data) {} + req_result(string_view data) : data_(data) {} bool success() const { return !has_error(data_); } @@ -34,7 +34,7 @@ namespace rest_rpc { } } private: - std::string_view data_; + string_view data_; }; class rpc_client : private boost::noncopyable { @@ -99,7 +99,7 @@ namespace rest_rpc { return true; } - std::unique_lock lock(conn_mtx_); + std::unique_lock lock(conn_mtx_); bool result = conn_cond_.wait_for(lock, std::chrono::seconds(timeout), [this] {return has_connected_; }); return result; @@ -110,8 +110,9 @@ namespace rest_rpc { } //sync call +#if __cplusplus > 201402L template - auto call(const std::string& rpc_name, Args&&... args) { + auto call(const std::string& rpc_name, Args&& ... args) { std::future future = async_call(rpc_name, std::forward(args)...); auto status = future.wait_for(std::chrono::seconds(1)); if (status == std::future_status::timeout || status == std::future_status::deferred) { @@ -125,6 +126,29 @@ namespace rest_rpc { return future.get().as(); } } +#else + template + typename std::enable_if::value>::type call(const std::string& rpc_name, Args&& ... args) { + std::future future = async_call(rpc_name, std::forward(args)...); + auto status = future.wait_for(std::chrono::seconds(1)); + if (status == std::future_status::timeout || status == std::future_status::deferred) { + throw std::out_of_range("timeout or deferred"); + } + + future.get().as(); + } + + template + typename std::enable_if::value, T>::type call(const std::string& rpc_name, Args&& ... args) { + std::future future = async_call(rpc_name, std::forward(args)...); + auto status = future.wait_for(std::chrono::seconds(1)); + if (status == std::future_status::timeout || status == std::future_status::deferred) { + throw std::out_of_range("timeout or deferred"); + } + + return future.get().as(); + } +#endif template auto async_call(const std::string& rpc_name, Args&&... args) { @@ -149,7 +173,7 @@ namespace rest_rpc { } private: - using message_type = std::pair; + using message_type = std::pair; void reset_deadline_timer(size_t timeout) { deadline_.expires_from_now(boost::posix_time::seconds((long)timeout)); deadline_.async_wait([this](const boost::system::error_code& ec) { @@ -260,14 +284,14 @@ namespace rest_rpc { std::future get_future() { auto p = std::make_shared>(); - std::future future = p->get_future(); + std::future future = p->get_future(); strand_.post([this, p1 = std::move(p)] () mutable { future_map_.emplace(req_id_, std::move(p1)); }); return future; } - void call_back(uint64_t req_id, const boost::system::error_code& ec, std::string_view data) { + void call_back(uint64_t req_id, const boost::system::error_code& ec, string_view data) { if (ec) { //LOG<