update to asio-1.36.0

This commit is contained in:
qicosmos
2025-09-02 14:17:48 +08:00
parent 233913192d
commit b1ee446fe3
4 changed files with 28 additions and 27 deletions
+2 -2
View File
@@ -23,7 +23,7 @@ struct ssl_configure {
class connection : public std::enable_shared_from_this<connection>,
private asio::noncopyable {
public:
connection(asio::io_service &io_service, std::size_t timeout_seconds,
connection(asio::io_context &io_service, std::size_t timeout_seconds,
router &router)
: socket_(io_service), body_(INIT_BUF_SIZE), timer_(io_service),
timeout_seconds_(timeout_seconds), has_closed_(false), router_(router) {
@@ -357,7 +357,7 @@ private:
}
auto self(this->shared_from_this());
timer_.expires_from_now(std::chrono::seconds(timeout_seconds_));
timer_.expires_after(std::chrono::seconds(timeout_seconds_));
timer_.async_wait([this, self](const asio::error_code &ec) {
if (has_closed()) {
return;
+6 -6
View File
@@ -14,10 +14,10 @@ public:
throw std::runtime_error("io_service_pool size is 0");
for (std::size_t i = 0; i < pool_size; ++i) {
io_service_ptr io_service(new asio::io_context);
work_ptr work(new asio::io_context::work(*io_service));
io_services_.push_back(io_service);
work_.push_back(work);
io_service_ptr io_ctx(new asio::io_context);
auto work = asio::make_work_guard(*io_ctx);
io_services_.push_back(io_ctx);
works_.push_back(std::move(work));
}
}
@@ -48,13 +48,13 @@ public:
private:
typedef std::shared_ptr<asio::io_context> io_service_ptr;
typedef std::shared_ptr<asio::io_context::work> work_ptr;
/// The pool of io_services.
std::vector<io_service_ptr> io_services_;
/// The work that keeps the io_services running.
std::vector<work_ptr> work_;
std::vector<asio::executor_work_guard<asio::io_context::executor_type>>
works_;
/// The next io_service to use for a connection.
std::size_t next_io_service_;
+18 -19
View File
@@ -67,17 +67,16 @@ const constexpr size_t DEFAULT_TIMEOUT = 5000; // milliseconds
class rpc_client : private asio::noncopyable {
public:
rpc_client()
: socket_(ios_), work_(std::make_shared<asio::io_context::work>(ios_)),
deadline_(ios_), body_(INIT_BUF_SIZE) {
: socket_(ios_), work_(asio::make_work_guard(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_(std::make_shared<asio::io_context::work>(ios_)),
deadline_(ios_), body_(INIT_BUF_SIZE),
client_language_(client_language),
: socket_(ios_), work_(asio::make_work_guard(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(); });
}
@@ -89,20 +88,20 @@ public:
std::function<void(long, const std::string &)>
on_result_received_callback,
std::string host, unsigned short port)
: 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),
: socket_(ios_), work_(asio::make_work_guard(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(); });
}
~rpc_client() {
std::promise<void> promise;
ios_.post([this, &promise] {
asio::post(ios_, [this, &promise] {
close();
stop_client_ = true;
std::error_code ec;
deadline_.cancel(ec);
// std::error_code ec;
deadline_.cancel();
promise.set_value();
});
promise.get_future().wait();
@@ -326,7 +325,7 @@ public:
void stop() {
if (thd_ != nullptr) {
work_ = nullptr;
work_.reset();
if (thd_->joinable()) {
thd_->join();
}
@@ -386,7 +385,7 @@ public:
private:
void async_connect() {
assert(port_ != 0);
auto addr = asio::ip::address::from_string(host_);
auto addr = asio::ip::make_address(host_);
socket_.async_connect({addr, port_}, [this](const asio::error_code &ec) {
if (has_connected_ || stop_client_) {
return;
@@ -433,7 +432,7 @@ private:
return;
}
deadline_.expires_from_now(std::chrono::seconds(timeout));
deadline_.expires_after(std::chrono::seconds(timeout));
deadline_.async_wait([this, timeout](const asio::error_code &ec) {
if (!ec) {
if (has_connected_) {
@@ -697,7 +696,7 @@ private:
class call_t : asio::noncopyable,
public std::enable_shared_from_this<call_t> {
public:
call_t(asio::io_service &ios,
call_t(asio::io_context &ios,
std::function<void(asio::error_code, string_view)> cb,
size_t timeout)
: timer_(ios), cb_(std::move(cb)), timeout_(timeout) {}
@@ -707,7 +706,7 @@ private:
return;
}
timer_.expires_from_now(std::chrono::milliseconds(timeout_));
timer_.expires_after(std::chrono::milliseconds(timeout_));
auto self = this->shared_from_this();
timer_.async_wait([this, self](asio::error_code ec) {
if (ec) {
@@ -727,8 +726,8 @@ private:
return;
}
asio::error_code ec;
timer_.cancel(ec);
// asio::error_code ec;
timer_.cancel();
}
private:
@@ -841,7 +840,7 @@ private:
std::unique_ptr<asio::ssl::stream<asio::ip::tcp::socket &>> ssl_stream_;
std::function<void(asio::ssl::context &)> ssl_context_callback_;
#endif
std::shared_ptr<asio::io_context::work> work_;
asio::executor_work_guard<asio::io_context::executor_type> work_;
std::shared_ptr<std::thread> thd_ = nullptr;
std::string host_;
+2
View File
@@ -5,6 +5,8 @@
#include <asio/ssl.hpp>
#endif
#include <asio/detail/noncopyable.hpp>
#include <asio/executor_work_guard.hpp>
#include <asio/post.hpp>
#include <asio/steady_timer.hpp>
using tcp_socket = asio::ip::tcp::socket;