Skip to content

Commit

Permalink
support automatic heartbeat.
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Aug 23, 2019
1 parent d2f5e9e commit 315d47a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
29 changes: 15 additions & 14 deletions examples/client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ void test_call_with_timeout() {
void test_connect() {
rpc_client client;
client.enable_auto_reconnect(); //automatic reconnect
client.enable_auto_heartbeat(); //automatic heartbeat
bool r = client.connect("127.0.0.1", 9000);
int count = 0;
while (true) {
Expand All @@ -323,20 +324,20 @@ void test_connect() {
std::this_thread::sleep_for(std::chrono::seconds(1));
}

{
rpc_client client;
bool r = client.connect("127.0.0.1", 9000);
int count = 0;
while (true) {
if (client.connect()) {
std::cout << "connected ok\n";
break;
}
else {
std::cout << "connected failed: " << count++ << "\n";
}
}
}
//{
// rpc_client client;
// bool r = client.connect("127.0.0.1", 9000);
// int count = 0;
// while (true) {
// if (client.connect()) {
// std::cout << "connected ok\n";
// break;
// }
// else {
// std::cout << "connected failed: " << count++ << "\n";
// }
// }
//}

std::string str;
std::cin >> str;
Expand Down
2 changes: 1 addition & 1 deletion include/router.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace rest_rpc {
template<typename F, typename Self, size_t... Indexes, typename Arg, typename... Args>
static typename std::result_of<F(Self, std::weak_ptr<connection>, Args...)>::type call_member_helper(
const F & f, Self * self, const std::index_sequence<Indexes...>&,
std::tuple<Arg, Args...> tup, std::weak_ptr<connection> ptr = nullptr) {
std::tuple<Arg, Args...> tup, std::weak_ptr<connection> ptr = std::shared_ptr<connection>{ nullptr }) {
return (*self.*f)(ptr, std::move(std::get<Indexes + 1>(tup))...);
}

Expand Down
23 changes: 18 additions & 5 deletions include/rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace rest_rpc {
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_),
Expand Down Expand Up @@ -118,8 +118,17 @@ namespace rest_rpc {
return has_connected_;
}

void enable_auto_reconnect() {
enable_reconnect_ = true;
void enable_auto_reconnect(bool enable = true) {
enable_reconnect_ = enable;
}

void enable_auto_heartbeat(bool enable = true) {
if (enable) {
reset_deadline_timer(5);
}
else {
deadline_.cancel();
}
}

void update_addr(const std::string& host, unsigned short port) {
Expand Down Expand Up @@ -294,10 +303,14 @@ namespace rest_rpc {
using message_type = std::pair<string_view, std::uint64_t>;
void reset_deadline_timer(size_t timeout) {
deadline_.expires_from_now(std::chrono::seconds(timeout));
deadline_.async_wait([this](const boost::system::error_code& ec) {
deadline_.async_wait([this, timeout](const boost::system::error_code& ec) {
if (!ec) {
socket_.close();
if (has_connected_) {
write(0, buffer_type(0));
}
}

reset_deadline_timer(timeout);
});
}

Expand Down

0 comments on commit 315d47a

Please sign in to comment.