Skip to content

Commit

Permalink
1.add some new connect interface;
Browse files Browse the repository at this point in the history
2.add call with timeout interface;

all are for more friendly interface.
  • Loading branch information
qicosmos committed Apr 19, 2019
1 parent f7abca6 commit c08ea5e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
28 changes: 26 additions & 2 deletions examples/client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ void test_add() {
return;
}

auto result = client.call<int>("add", 1, 2);
std::cout << result << std::endl;
{
auto result = client.call<int>("add", 1, 2);
std::cout << result << std::endl;
}

{
auto result = client.call<2000, int>("add", 1, 2);
std::cout << result << std::endl;
}
}
catch (const std::exception& e){
std::cout << e.what() << std::endl;
Expand Down Expand Up @@ -254,9 +261,26 @@ void test_performance2() {
std::cin >> str;
}

void test_call_with_timeout() {
rpc_client client;
client.async_connect("127.0.0.1", 9000);

try {
auto result = client.call<50, person>("get_person");
std::cout << result.name << std::endl;
}
catch (const std::exception& ex) {
std::cout << ex.what() << std::endl;
}

std::string str;
std::cin >> str;
}

int main() {
test_sync_client();
test_async_client();
test_call_with_timeout();
//test_upload();
//test_download();
//test_performance1();
Expand Down
56 changes: 49 additions & 7 deletions include/rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,17 @@ namespace rest_rpc {
string_view data_;
};

const constexpr size_t DEFAULT_TIMEOUT = 1000; //milliseconds

class rpc_client : private boost::noncopyable {
public:
rpc_client() : socket_(ios_), work_(ios_), strand_(ios_),
deadline_(ios_), body_(INIT_BUF_SIZE) {
thd_ = std::make_shared<std::thread>([this] {
ios_.run();
});
}

rpc_client(const std::string& host, unsigned short port) : socket_(ios_), work_(ios_), strand_(ios_),
deadline_(ios_), host_(host), port_(port), body_(INIT_BUF_SIZE) {
thd_ = std::make_shared<std::thread>([this] {
Expand Down Expand Up @@ -94,6 +103,24 @@ namespace rest_rpc {
return wait_conn(timeout);
}

bool connect(const std::string& host, unsigned short port, size_t timeout = 1) {
if (port_==0) {
host_ = host;
port_ = port;
}

return connect(timeout);
}

void async_connect(const std::string& host, unsigned short port) {
if (port_ == 0) {
host_ = host;
port_ = port;
}

async_connect();
}

bool wait_conn(size_t timeout) {
if (has_connected_) {
return true;
Expand All @@ -111,10 +138,10 @@ namespace rest_rpc {

//sync call
#if __cplusplus > 201402L
template<typename T = void, typename... Args>
template<size_t TIMEOUT, typename T = void, typename... Args>
auto call(const std::string& rpc_name, Args&& ... args) {
std::future<req_result> future = async_call(rpc_name, std::forward<Args>(args)...);
auto status = future.wait_for(std::chrono::seconds(1));
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");
}
Expand All @@ -126,28 +153,43 @@ namespace rest_rpc {
return future.get().as<T>();
}
}

template<typename T = void, typename... Args>
auto call(const std::string& rpc_name, Args&& ... args) {
return call<DEFAULT_TIMEOUT, T>(rpc_name, std::forward<Args>(args)...);
}
#else
template<typename T=void, typename... Args>
template<size_t TIMEOUT, typename T=void, typename... Args>
typename std::enable_if<std::is_void<T>::value>::type call(const std::string& rpc_name, Args&& ... args) {
std::future<req_result> future = async_call(rpc_name, std::forward<Args>(args)...);
auto status = future.wait_for(std::chrono::seconds(1));
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");
}

future.get().as();
}

template<typename T, typename... Args>
template<typename T = void, typename... Args>
typename std::enable_if<std::is_void<T>::value>::type call(const std::string& rpc_name, Args&& ... args) {
call<DEFAULT_TIMEOUT, T>(rpc_name, std::forward<Args>(args)...);
}

template<size_t TIMEOUT, typename T, typename... Args>
typename std::enable_if<!std::is_void<T>::value, T>::type call(const std::string& rpc_name, Args&& ... args) {
std::future<req_result> future = async_call(rpc_name, std::forward<Args>(args)...);
auto status = future.wait_for(std::chrono::seconds(1));
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");
}

return future.get().as<T>();
}

template<typename T, typename... Args>
typename std::enable_if<!std::is_void<T>::value, T>::type call(const std::string& rpc_name, Args&& ... args) {
return call<DEFAULT_TIMEOUT, T>(rpc_name, std::forward<Args>(args)...);
}
#endif

template<typename... Args>
Expand Down Expand Up @@ -325,7 +367,7 @@ namespace rest_rpc {
std::shared_ptr<std::thread> thd_ = nullptr;

std::string host_;
unsigned short port_;
unsigned short port_ = 0;
size_t connect_timeout_ = 2;//s
size_t wait_timeout_ = 2;//s
int reconnect_cnt_ = -1;
Expand Down

0 comments on commit c08ea5e

Please sign in to comment.