1818 */
1919#include " ExecutorService.h"
2020
21+ #ifdef USE_ASIO
22+ #include < asio/post.hpp>
23+ #else
24+ #include < boost/asio/post.hpp>
25+ #endif
26+
2127#include " LogUtils.h"
2228#include " TimeUtils.h"
2329DECLARE_LOG_OBJECT ()
@@ -31,18 +37,13 @@ ExecutorService::~ExecutorService() { close(0); }
3137void ExecutorService::start () {
3238 auto self = shared_from_this ();
3339 std::thread t{[this , self] {
34- LOG_DEBUG (" Run io_service in a single thread" );
35- ASIO_ERROR ec;
40+ LOG_DEBUG (" Run io_context in a single thread" );
3641 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" );
42+ io_context_.restart ();
43+ auto work{ASIO::make_work_guard (io_context_)};
44+ io_context_.run ();
4545 }
46+ LOG_DEBUG (" Event loop of ExecutorService exits successfully" );
4647 {
4748 std::lock_guard<std::mutex> lock{mutex_};
4849 ioServiceDone_ = true ;
@@ -63,12 +64,12 @@ ExecutorServicePtr ExecutorService::create() {
6364}
6465
6566/*
66- * factory method of ASIO::ip::tcp::socket associated with io_service_ instance
67+ * factory method of ASIO::ip::tcp::socket associated with io_context_ instance
6768 * @ returns shared_ptr to this socket
6869 */
6970SocketPtr ExecutorService::createSocket () {
7071 try {
71- return SocketPtr (new ASIO::ip::tcp::socket (io_service_ ));
72+ return SocketPtr (new ASIO::ip::tcp::socket (io_context_ ));
7273 } catch (const ASIO_SYSTEM_ERROR &e) {
7374 restart ();
7475 auto error = std::string (" Failed to create socket: " ) + e.what ();
@@ -82,12 +83,12 @@ TlsSocketPtr ExecutorService::createTlsSocket(SocketPtr &socket, ASIO::ssl::cont
8283}
8384
8485/*
85- * factory method of Resolver object associated with io_service_ instance
86+ * factory method of Resolver object associated with io_context_ instance
8687 * @returns shraed_ptr to resolver object
8788 */
8889TcpResolverPtr ExecutorService::createTcpResolver () {
8990 try {
90- return TcpResolverPtr (new ASIO::ip::tcp::resolver (io_service_ ));
91+ return TcpResolverPtr (new ASIO::ip::tcp::resolver (io_context_ ));
9192 } catch (const ASIO_SYSTEM_ERROR &e) {
9293 restart ();
9394 auto error = std::string (" Failed to create resolver: " ) + e.what ();
@@ -97,36 +98,36 @@ TcpResolverPtr ExecutorService::createTcpResolver() {
9798
9899DeadlineTimerPtr ExecutorService::createDeadlineTimer () {
99100 try {
100- return DeadlineTimerPtr (new ASIO::steady_timer (io_service_ ));
101+ return DeadlineTimerPtr (new ASIO::steady_timer (io_context_ ));
101102 } catch (const ASIO_SYSTEM_ERROR &e) {
102103 restart ();
103104 auto error = std::string (" Failed to create steady_timer: " ) + e.what ();
104105 throw std::runtime_error (error);
105106 }
106107}
107108
108- void ExecutorService::restart () { io_service_ .stop (); }
109+ void ExecutorService::restart () { io_context_ .stop (); }
109110
110111void ExecutorService::close (long timeoutMs) {
111112 bool expectedState = false ;
112113 if (!closed_.compare_exchange_strong (expectedState, true )) {
113114 return ;
114115 }
115116 if (timeoutMs == 0 ) { // non-blocking
116- io_service_ .stop ();
117+ io_context_ .stop ();
117118 return ;
118119 }
119120
120121 std::unique_lock<std::mutex> lock{mutex_};
121- io_service_ .stop ();
122+ io_context_ .stop ();
122123 if (timeoutMs > 0 ) {
123124 cond_.wait_for (lock, std::chrono::milliseconds (timeoutMs), [this ] { return ioServiceDone_; });
124125 } else { // < 0
125126 cond_.wait (lock, [this ] { return ioServiceDone_; });
126127 }
127128}
128129
129- void ExecutorService::postWork (std::function<void (void )> task) { io_service_. post (task); }
130+ void ExecutorService::postWork (std::function<void (void )> task) { ASIO:: post (io_context_, task); }
130131
131132// ///////////////////
132133
0 commit comments