From 280e2c40ddc4f1166a716fec2032228b3671431f Mon Sep 17 00:00:00 2001 From: qicosmos Date: Thu, 23 May 2019 17:28:27 +0800 Subject: [PATCH] modify async_call interface --- examples/client/main.cpp | 12 ++++++------ include/rpc_client.hpp | 12 +++++++++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/examples/client/main.cpp b/examples/client/main.cpp index b17a0f8..0f4dfc7 100644 --- a/examples/client/main.cpp +++ b/examples/client/main.cpp @@ -119,7 +119,7 @@ void test_async_client() { std::cout << ec.message() << std::endl; }); - auto f = client.async_call("get_person"); + auto f = client.async_call("get_person"); if (f.wait_for(std::chrono::milliseconds(50)) == std::future_status::timeout) { std::cout << "timeout" << std::endl; } @@ -128,7 +128,7 @@ void test_async_client() { std::cout << p.name << std::endl; } - auto fu = client.async_call("hello", "purecpp"); + auto fu = client.async_call("hello", "purecpp"); fu.get().as(); //no return //sync call @@ -158,7 +158,7 @@ void test_upload() { std::cout << file_len << std::endl; { - auto f = client.async_call("upload", "test", conent); + auto f = client.async_call("upload", "test", conent); if (f.wait_for(std::chrono::milliseconds(500)) == std::future_status::timeout) { std::cout << "timeout" << std::endl; } @@ -168,7 +168,7 @@ void test_upload() { } } { - auto f = client.async_call("upload", "test1", conent); + auto f = client.async_call("upload", "test1", conent); if (f.wait_for(std::chrono::milliseconds(500)) == std::future_status::timeout) { std::cout << "timeout" << std::endl; } @@ -188,7 +188,7 @@ void test_download() { return; } - auto f = client.async_call("download", "test"); + auto f = client.async_call("download", "test"); if (f.wait_for(std::chrono::milliseconds(500)) == std::future_status::timeout) { std::cout << "timeout" << std::endl; } @@ -238,7 +238,7 @@ void test_performance1() { person p{ 1, "tom", 20 }; for (size_t i = 0; i < 100000000; i++) { - auto future = client.async_call("get_name", p); + auto future = client.async_call("get_name", p); auto status = future.wait_for(std::chrono::seconds(2)); if (status == std::future_status::deferred) { std::cout << "deferred\n"; diff --git a/include/rpc_client.hpp b/include/rpc_client.hpp index bf04b03..c98b95b 100644 --- a/include/rpc_client.hpp +++ b/include/rpc_client.hpp @@ -36,6 +36,12 @@ namespace rest_rpc { string_view data_; }; + enum class CallModel { + future, + callback + }; + const constexpr auto FUTURE = CallModel::future; + const constexpr size_t DEFAULT_TIMEOUT = 5000; //milliseconds class rpc_client : private asio::noncopyable { @@ -192,7 +198,7 @@ namespace rest_rpc { #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)...); + std::future future = async_call(rpc_name, std::forward(args)...); auto status = future.wait_for(std::chrono::milliseconds(TIMEOUT)); if (status == std::future_status::timeout || status == std::future_status::deferred) { throw std::out_of_range("timeout or deferred"); @@ -208,7 +214,7 @@ namespace rest_rpc { 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)...); + std::future future = async_call(rpc_name, std::forward(args)...); auto status = future.wait_for(std::chrono::milliseconds(TIMEOUT)); if (status == std::future_status::timeout || status == std::future_status::deferred) { throw std::out_of_range("timeout or deferred"); @@ -223,7 +229,7 @@ namespace rest_rpc { } #endif - template + template std::future async_call(const std::string& rpc_name, Args&&... args) { req_id_++; auto future = get_future();