Skip to content

Commit 536a299

Browse files
committed
fix some io_context interface changes
1 parent c22a330 commit 536a299

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

lib/ClientConnection.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
#include <cstdint>
2727
#ifdef USE_ASIO
2828
#include <asio/bind_executor.hpp>
29-
#include <asio/io_service.hpp>
29+
#include <asio/io_context.hpp>
3030
#include <asio/ip/tcp.hpp>
3131
#include <asio/ssl/stream.hpp>
3232
#include <asio/strand.hpp>
3333
#else
3434
#include <boost/asio/bind_executor.hpp>
35-
#include <boost/asio/io_service.hpp>
35+
#include <boost/asio/io_context.hpp>
3636
#include <boost/asio/ip/tcp.hpp>
3737
#include <boost/asio/ssl/stream.hpp>
3838
#include <boost/asio/strand.hpp>
@@ -325,7 +325,7 @@ class PULSAR_PUBLIC ClientConnection : public std::enable_shared_from_this<Clien
325325
*/
326326
SocketPtr socket_;
327327
TlsSocketPtr tlsSocket_;
328-
ASIO::strand<ASIO::io_service::executor_type> strand_;
328+
ASIO::strand<ASIO::io_context::executor_type> strand_;
329329

330330
const std::string logicalAddress_;
331331
/*

lib/ExecutorService.cc

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,16 @@ ExecutorService::~ExecutorService() { close(0); }
3131
void ExecutorService::start() {
3232
auto self = shared_from_this();
3333
std::thread t{[this, self] {
34-
LOG_DEBUG("Run io_service in a single thread");
35-
ASIO_ERROR ec;
34+
LOG_DEBUG("Run io_context in a single thread");
3635
while (!closed_) {
37-
io_service_.restart();
38-
IOService::work work{getIOService()};
39-
io_service_.run(ec);
40-
}
41-
if (ec) {
42-
LOG_ERROR("Failed to run io_service: " << ec.message());
43-
} else {
44-
LOG_DEBUG("Event loop of ExecutorService exits successfully");
36+
io_context_.restart();
37+
auto work_guard = ASIO::make_work_guard(io_context_);
38+
try {
39+
io_context_.run();
40+
LOG_DEBUG("Event loop of ExecutorService exits successfully");
41+
} catch (const ASIO_ERROR &e) {
42+
LOG_ERROR("Failed to run io_context: " << e.message());
43+
}
4544
}
4645
{
4746
std::lock_guard<std::mutex> lock{mutex_};
@@ -63,12 +62,12 @@ ExecutorServicePtr ExecutorService::create() {
6362
}
6463

6564
/*
66-
* factory method of ASIO::ip::tcp::socket associated with io_service_ instance
65+
* factory method of ASIO::ip::tcp::socket associated with io_context_ instance
6766
* @ returns shared_ptr to this socket
6867
*/
6968
SocketPtr ExecutorService::createSocket() {
7069
try {
71-
return SocketPtr(new ASIO::ip::tcp::socket(io_service_));
70+
return SocketPtr(new ASIO::ip::tcp::socket(io_context_));
7271
} catch (const ASIO_SYSTEM_ERROR &e) {
7372
restart();
7473
auto error = std::string("Failed to create socket: ") + e.what();
@@ -82,12 +81,12 @@ TlsSocketPtr ExecutorService::createTlsSocket(SocketPtr &socket, ASIO::ssl::cont
8281
}
8382

8483
/*
85-
* factory method of Resolver object associated with io_service_ instance
84+
* factory method of Resolver object associated with io_context_ instance
8685
* @returns shraed_ptr to resolver object
8786
*/
8887
TcpResolverPtr ExecutorService::createTcpResolver() {
8988
try {
90-
return TcpResolverPtr(new ASIO::ip::tcp::resolver(io_service_));
89+
return TcpResolverPtr(new ASIO::ip::tcp::resolver(io_context_));
9190
} catch (const ASIO_SYSTEM_ERROR &e) {
9291
restart();
9392
auto error = std::string("Failed to create resolver: ") + e.what();
@@ -97,36 +96,36 @@ TcpResolverPtr ExecutorService::createTcpResolver() {
9796

9897
DeadlineTimerPtr ExecutorService::createDeadlineTimer() {
9998
try {
100-
return DeadlineTimerPtr(new ASIO::steady_timer(io_service_));
99+
return DeadlineTimerPtr(new ASIO::steady_timer(io_context_));
101100
} catch (const ASIO_SYSTEM_ERROR &e) {
102101
restart();
103102
auto error = std::string("Failed to create steady_timer: ") + e.what();
104103
throw std::runtime_error(error);
105104
}
106105
}
107106

108-
void ExecutorService::restart() { io_service_.stop(); }
107+
void ExecutorService::restart() { io_context_.stop(); }
109108

110109
void ExecutorService::close(long timeoutMs) {
111110
bool expectedState = false;
112111
if (!closed_.compare_exchange_strong(expectedState, true)) {
113112
return;
114113
}
115114
if (timeoutMs == 0) { // non-blocking
116-
io_service_.stop();
115+
io_context_.stop();
117116
return;
118117
}
119118

120119
std::unique_lock<std::mutex> lock{mutex_};
121-
io_service_.stop();
120+
io_context_.stop();
122121
if (timeoutMs > 0) {
123122
cond_.wait_for(lock, std::chrono::milliseconds(timeoutMs), [this] { return ioServiceDone_; });
124123
} else { // < 0
125124
cond_.wait(lock, [this] { return ioServiceDone_; });
126125
}
127126
}
128127

129-
void ExecutorService::postWork(std::function<void(void)> task) { io_service_.post(task); }
128+
void ExecutorService::postWork(std::function<void(void)> task) { ASIO::post(io_context_, std::move(task)); }
130129

131130
/////////////////////
132131

lib/ExecutorService.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
#include <atomic>
2525
#ifdef USE_ASIO
26-
#include <asio/io_service.hpp>
26+
#include <asio/io_context.hpp>
2727
#include <asio/ip/tcp.hpp>
2828
#include <asio/ssl.hpp>
2929
#else
30-
#include <boost/asio/io_service.hpp>
30+
#include <boost/asio/io_context.hpp>
3131
#include <boost/asio/ip/tcp.hpp>
3232
#include <boost/asio/ssl.hpp>
3333
#endif
@@ -46,7 +46,7 @@ typedef std::shared_ptr<ASIO::ssl::stream<ASIO::ip::tcp::socket &> > TlsSocketPt
4646
typedef std::shared_ptr<ASIO::ip::tcp::resolver> TcpResolverPtr;
4747
class PULSAR_PUBLIC ExecutorService : public std::enable_shared_from_this<ExecutorService> {
4848
public:
49-
using IOService = ASIO::io_service;
49+
using IOService = ASIO::io_context;
5050
using SharedPtr = std::shared_ptr<ExecutorService>;
5151

5252
static SharedPtr create();
@@ -67,14 +67,14 @@ class PULSAR_PUBLIC ExecutorService : public std::enable_shared_from_this<Execut
6767
// See TimeoutProcessor for the semantics of the parameter.
6868
void close(long timeoutMs = 3000);
6969

70-
IOService &getIOService() { return io_service_; }
70+
IOService &getIOService() { return io_context_; }
7171
bool isClosed() const noexcept { return closed_; }
7272

7373
private:
7474
/*
75-
* io_service is our interface to os, io object schedule async ops on this object
75+
* io_context is our interface to os, io object schedule async ops on this object
7676
*/
77-
IOService io_service_;
77+
IOService io_context_;
7878

7979
std::atomic_bool closed_{false};
8080
std::mutex mutex_;

0 commit comments

Comments
 (0)