mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-29 08:34:47 +08:00
fix reconnect
This commit is contained in:
@@ -100,7 +100,7 @@ public:
|
||||
std::promise<void> promise;
|
||||
ios_.post([this, &promise] {
|
||||
close();
|
||||
stop_timer_ = true;
|
||||
stop_client_ = true;
|
||||
std::error_code ec;
|
||||
deadline_.cancel(ec);
|
||||
promise.set_value();
|
||||
@@ -388,7 +388,7 @@ private:
|
||||
assert(port_ != 0);
|
||||
auto addr = asio::ip::address::from_string(host_);
|
||||
socket_.async_connect({addr, port_}, [this](const asio::error_code &ec) {
|
||||
if (has_connected_) {
|
||||
if (has_connected_ || stop_client_) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -429,7 +429,7 @@ private:
|
||||
}
|
||||
|
||||
void reset_deadline_timer(size_t timeout) {
|
||||
if (stop_timer_) {
|
||||
if (stop_client_) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -848,7 +848,7 @@ private:
|
||||
bool has_wait_ = false;
|
||||
|
||||
asio::steady_timer deadline_;
|
||||
bool stop_timer_ = false;
|
||||
bool stop_client_ = false;
|
||||
|
||||
struct client_message_type {
|
||||
std::uint64_t req_id;
|
||||
|
||||
+36
-5
@@ -1,17 +1,15 @@
|
||||
#include "doctest/doctest.h"
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <rest_rpc.hpp>
|
||||
#include "doctest/doctest.h"
|
||||
#include <thread>
|
||||
|
||||
using namespace rest_rpc;
|
||||
using namespace rpc_service;
|
||||
|
||||
struct dummy {
|
||||
int add(rpc_conn conn, int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
int add(rpc_conn conn, int a, int b) { return a + b; }
|
||||
};
|
||||
|
||||
std::string echo(rpc_conn conn, const std::string &src) { return src; }
|
||||
@@ -29,6 +27,39 @@ void hello(rpc_conn conn, const std::string &str) {
|
||||
std::cout << "hello " << str << std::endl;
|
||||
}
|
||||
|
||||
TEST_CASE("test_client_reconnect") {
|
||||
rpc_client client;
|
||||
client.enable_auto_reconnect(); // automatic reconnect
|
||||
client.enable_auto_heartbeat(); // automatic heartbeat
|
||||
client.set_error_callback([](asio::error_code ec) {
|
||||
std::cout << "line: " << __LINE__ << ", 11msg: " << ec.message()
|
||||
<< std::endl;
|
||||
});
|
||||
client.connect("127.0.0.1", 9000);
|
||||
|
||||
rpc_server server(9000, std::thread::hardware_concurrency());
|
||||
dummy d;
|
||||
server.register_handler("add", &dummy::add, &d);
|
||||
server.async_run();
|
||||
|
||||
int count = 0;
|
||||
while (true) {
|
||||
if (client.has_connected()) {
|
||||
try {
|
||||
auto result = client.call<int>("add", 1, 2);
|
||||
CHECK_EQ(result, 3);
|
||||
break;
|
||||
} catch (const std::exception &ex) {
|
||||
std::cout << ex.what() << std::endl;
|
||||
}
|
||||
} else {
|
||||
// count++;
|
||||
std::cout << "connected failed: " << count++ << "\n";
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("test_client_default_constructor") {
|
||||
rpc_server server(9000, std::thread::hardware_concurrency());
|
||||
dummy d;
|
||||
|
||||
Reference in New Issue
Block a user