Skip to content

Commit

Permalink
fix stop
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jan 20, 2024
1 parent 3822441 commit 2f5b3d4
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions include/rest_rpc/rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ const constexpr size_t DEFAULT_TIMEOUT = 5000; // milliseconds
class rpc_client : private asio::noncopyable {
public:
rpc_client()
: socket_(ios_), work_(ios_), deadline_(ios_), body_(INIT_BUF_SIZE) {
: socket_(ios_), work_(std::make_shared<asio::io_context::work>(ios_)),
deadline_(ios_), body_(INIT_BUF_SIZE) {
thd_ = std::make_shared<std::thread>([this] { ios_.run(); });
}

rpc_client(client_language_t client_language,
std::function<void(long, const std::string &)>
on_result_received_callback)
: socket_(ios_), work_(ios_), deadline_(ios_), body_(INIT_BUF_SIZE),
: socket_(ios_), work_(std::make_shared<asio::io_context::work>(ios_)),
deadline_(ios_), body_(INIT_BUF_SIZE),
client_language_(client_language),
on_result_received_callback_(std::move(on_result_received_callback)) {
thd_ = std::make_shared<std::thread>([this] { ios_.run(); });
Expand All @@ -87,8 +89,9 @@ class rpc_client : private asio::noncopyable {
std::function<void(long, const std::string &)>
on_result_received_callback,
std::string host, unsigned short port)
: socket_(ios_), work_(ios_), deadline_(ios_), host_(std::move(host)),
port_(port), body_(INIT_BUF_SIZE), client_language_(client_language),
: socket_(ios_), work_(std::make_shared<asio::io_context::work>(ios_)),
deadline_(ios_), host_(std::move(host)), port_(port),
body_(INIT_BUF_SIZE), client_language_(client_language),
on_result_received_callback_(std::move(on_result_received_callback)) {
thd_ = std::make_shared<std::thread>([this] { ios_.run(); });
}
Expand All @@ -97,6 +100,9 @@ class rpc_client : private asio::noncopyable {
std::promise<void> promise;
ios_.post([this, &promise] {
close();
stop_timer_ = true;
std::error_code ec;
deadline_.cancel(ec);
promise.set_value();
});
promise.get_future().wait();
Expand Down Expand Up @@ -320,7 +326,7 @@ class rpc_client : private asio::noncopyable {

void stop() {
if (thd_ != nullptr) {
ios_.stop();
work_ = nullptr;
if (thd_->joinable()) {
thd_->join();
}
Expand Down Expand Up @@ -423,6 +429,10 @@ class rpc_client : private asio::noncopyable {
}

void reset_deadline_timer(size_t timeout) {
if (stop_timer_) {
return;
}

deadline_.expires_from_now(std::chrono::seconds(timeout));
deadline_.async_wait([this, timeout](const asio::error_code &ec) {
if (!ec) {
Expand Down Expand Up @@ -825,7 +835,7 @@ class rpc_client : private asio::noncopyable {
std::unique_ptr<asio::ssl::stream<asio::ip::tcp::socket &>> ssl_stream_;
std::function<void(asio::ssl::context &)> ssl_context_callback_;
#endif
asio::io_context::work work_;
std::shared_ptr<asio::io_context::work> work_;
std::shared_ptr<std::thread> thd_ = nullptr;

std::string host_;
Expand All @@ -838,6 +848,7 @@ class rpc_client : private asio::noncopyable {
bool has_wait_ = false;

asio::steady_timer deadline_;
bool stop_timer_ = false;

struct client_message_type {
std::uint64_t req_id;
Expand Down

0 comments on commit 2f5b3d4

Please sign in to comment.