Skip to content

Commit 9b11205

Browse files
authored
Refactor io_service to io_context across the codebase (#2)
* Refactor io_service to io_context across the codebase for Boost compatibility * Test against Boost 1.65 * Replace io_context.hpp with Boost's asio/io_context.hpp and update references across the codebase * Remove testing against versions lower than 1.68 * Refactor documentation to reflect use of io_context * Refactor code and documentation to reflect use of io_context
1 parent 069b451 commit 9b11205

File tree

21 files changed

+208
-272
lines changed

21 files changed

+208
-272
lines changed

.github/workflows/cmake-superbuild/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ endif ()
2424
include(ExternalProject)
2525

2626

27-
set(BOOST_189 "https://archives.boost.io/release/1.87.0/source/boost_1_87_0.tar.bz2")
27+
set(BOOST_189 "https://archives.boost.io/release/1.89.0/source/boost_1_89_0.tar.bz2")
2828
set(BOOST_187 "https://archives.boost.io/release/1.87.0/source/boost_1_87_0.tar.bz2")
2929
set(BOOST_186 "https://archives.boost.io/release/1.86.0/source/boost_1_86_0.tar.bz2")
3030
set(BOOST_184 "https://archives.boost.io/release/1.84.0/source/boost_1_84_0.tar.bz2")
31+
set(BOOST_181 "https://archives.boost.io/release/1.81.0/source/boost_1_81_0.tar.bz2")
3132
set(BOOST_177 "https://archives.boost.io/release/1.77.0/source/boost_1_77_0.tar.bz2")
3233
set(BOOST_176 "https://archives.boost.io/release/1.76.0/source/boost_1_76_0.tar.bz2")
3334
set(BOOST_174 "https://archives.boost.io/release/1.74.0/source/boost_1_74_0.tar.bz2")

.github/workflows/cmake.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ jobs:
5252
system_packages_ubuntu:
5353
system_packages_macos:
5454
extra_cmake_args: -DCMAKE_POLICY_DEFAULT_CMP0167=NEW
55+
- url: BOOST_181
56+
system_packages_ubuntu:
57+
system_packages_macos:
58+
extra_cmake_args: -DCMAKE_POLICY_DEFAULT_CMP0167=NEW
5559
- url: BOOST_176
5660
system_packages_ubuntu:
5761
system_packages_macos:

AzmqCPack.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ in general, and Asio in particular.
88
99
The main abstraction exposed by the library is azmq::socket which
1010
provides an Asio style socket interface to the underlying zeromq socket
11-
and interfaces with Asio's io_service(). The socket implementation
12-
participates in the io_service's reactor for asynchronous IO and
11+
and interfaces with Asio's io_context(). The socket implementation
12+
participates in the io_context's reactor for asynchronous IO and
1313
may be freely mixed with other Asio socket types (raw TCP/UDP/Serial/etc.).")
1414
set(CPACK_PACKAGE_VERSION "1.1.0")
1515

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ in general, and Asio in particular.
99

1010
The main abstraction exposed by the library is azmq::socket which
1111
provides an Asio style socket interface to the underlying zeromq socket
12-
and interfaces with Asio's io_service(). The socket implementation
13-
participates in the io_service's reactor for asynchronous IO and
12+
and interfaces with Asio's io_context(). The socket implementation
13+
participates in the io_context's reactor for asynchronous IO and
1414
may be freely mixed with other Asio socket types (raw TCP/UDP/Serial/etc.).
1515

1616
## Building and installation
@@ -82,13 +82,13 @@ http://zeromq.org/intro:read-the-manual
8282
namespace asio = boost::asio;
8383

8484
int main(int argc, char** argv) {
85-
asio::io_service ios;
86-
azmq::sub_socket subscriber(ios);
85+
asio::io_context ioc;
86+
azmq::sub_socket subscriber(ioc);
8787
subscriber.connect("tcp://192.168.55.112:5556");
8888
subscriber.connect("tcp://192.168.55.201:7721");
8989
subscriber.set_option(azmq::socket::subscribe("NASDAQ"));
9090

91-
azmq::pub_socket publisher(ios);
91+
azmq::pub_socket publisher(ioc);
9292
publisher.bind("ipc://nasdaq-feed");
9393

9494
std::array<char, 256> buf;

azmq/actor.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "socket.hpp"
1313
#include "detail/actor_service.hpp"
1414

15-
#include "io_service.hpp"
15+
#include <boost/asio/io_context.hpp>
1616

1717
#include <functional>
1818

@@ -25,7 +25,7 @@ AZMQ_V1_INLINE_NAMESPACE_BEGIN
2525
using last_error = detail::actor_service::last_error;
2626

2727
/** \brief create an actor bound to one end of a pipe (pair of inproc sockets)
28-
* \param peer io_service to associate the peer (caller) end of the pipe
28+
* \param peer io_context to associate the peer (caller) end of the pipe
2929
* \param f Function accepting socket& as the first parameter and a
3030
* number of additional args
3131
* \returns peer socket
@@ -35,15 +35,15 @@ AZMQ_V1_INLINE_NAMESPACE_BEGIN
3535
* will be attached to the lifetime of the returned socket and will run
3636
* until it is destroyed.
3737
*
38-
* \remark Each actor has an associated io_service and the supplied socket
39-
* will be created on this io_service. The actor may access this by calling
40-
* get_io_service() on the supplied socket.
38+
* \remark Each actor has an associated io_context and the supplied socket
39+
* will be created on this io_context. The actor may access this by calling
40+
* get_io_context() on the supplied socket.
4141
*
42-
* \remark The associated io_service is configured to stop the spawned actor
42+
* \remark The associated io_context is configured to stop the spawned actor
4343
* on SIG_KILL and SIG_TERM.
4444
*
4545
* \remark Termination:
46-
* well behaved actors should ultimately call run() on the io_service
46+
* well behaved actors should ultimately call run() on the io_context
4747
* associated with the supplied socket. This allows the 'client' end of
4848
* the socket's lifetime to cleanly signal termination. If for some
4949
* reason, this is not possible, the caller should set the 'detached'
@@ -54,20 +54,20 @@ AZMQ_V1_INLINE_NAMESPACE_BEGIN
5454
* message.
5555
*
5656
* Also note, the default signal handling for the background thread is
57-
* designed to call stop() on the associated io_service, so not calling
57+
* designed to call stop() on the associated io_context, so not calling
5858
* run() in your handler means you are responsible for catching these
5959
* signals in some other way.
6060
*/
6161
template<typename Function, typename... Args>
62-
socket spawn(boost::asio::io_service & peer, bool defer_start, Function && f, Args&&... args) {
62+
socket spawn(boost::asio::io_context & peer, bool defer_start, Function && f, Args&&... args) {
6363
auto& t = boost::asio::use_service<detail::actor_service>(peer);
6464
return t.make_pipe(defer_start, std::bind(std::forward<Function>(f),
6565
std::placeholders::_1,
6666
std::forward<Args>(args)...));
6767
}
6868

6969
template<typename Function, typename... Args>
70-
socket spawn(boost::asio::io_service & peer, Function && f, Args&&... args) {
70+
socket spawn(boost::asio::io_context & peer, Function && f, Args&&... args) {
7171
auto& t = boost::asio::use_service<detail::actor_service>(peer);
7272
return t.make_pipe(false, std::bind(std::forward<Function>(f),
7373
std::placeholders::_1,

azmq/context.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "detail/socket_service.hpp"
1313
#include "option.hpp"
1414
#include "error.hpp"
15-
#include "io_service.hpp"
15+
#include <boost/asio/io_context.hpp>
1616
#include <zmq.h>
1717

1818
namespace azmq {
@@ -27,10 +27,10 @@ AZMQ_V1_INLINE_NAMESPACE_BEGIN
2727
* \remark Must be called before any sockets are created
2828
*/
2929
template<typename Option>
30-
boost::system::error_code set_option(boost::asio::io_service & io_service,
30+
boost::system::error_code set_option(boost::asio::io_context & io_context,
3131
const Option & option,
3232
boost::system::error_code & ec) {
33-
return boost::asio::use_service<detail::socket_service>(io_service).set_option(option, ec);
33+
return boost::asio::use_service<detail::socket_service>(io_context).set_option(option, ec);
3434
}
3535

3636
/** \brief set options on the zeromq context.
@@ -39,9 +39,9 @@ AZMQ_V1_INLINE_NAMESPACE_BEGIN
3939
* \remark Must be called before any sockets are created
4040
*/
4141
template<typename Option>
42-
void set_option(boost::asio::io_service & io_service, const Option & option) {
42+
void set_option(boost::asio::io_context & io_context, const Option & option) {
4343
boost::system::error_code ec;
44-
if (set_option(io_service, option, ec))
44+
if (set_option(io_context, option, ec))
4545
throw boost::system::system_error(ec);
4646
}
4747

@@ -51,10 +51,10 @@ AZMQ_V1_INLINE_NAMESPACE_BEGIN
5151
* \param ec boost::system::error_code
5252
*/
5353
template<typename Option>
54-
boost::system::error_code get_option(boost::asio::io_service & io_service,
54+
boost::system::error_code get_option(boost::asio::io_context & io_context,
5555
Option & option,
5656
boost::system::error_code & ec) {
57-
return boost::asio::use_service<detail::socket_service>(io_service).get_option(option, ec);
57+
return boost::asio::use_service<detail::socket_service>(io_context).get_option(option, ec);
5858
}
5959

6060
/** \brief get option from zeromq context
@@ -63,9 +63,9 @@ AZMQ_V1_INLINE_NAMESPACE_BEGIN
6363
* \param ec boost::system::error_code
6464
*/
6565
template<typename Option>
66-
void get_option(boost::asio::io_service & io_service, Option & option) {
66+
void get_option(boost::asio::io_context & io_context, Option & option) {
6767
boost::system::error_code ec;
68-
if (get_option(io_service, option))
68+
if (get_option(io_context, option))
6969
throw boost::system::system_error(ec);
7070
}
7171
AZMQ_V1_INLINE_NAMESPACE_END

azmq/detail/actor_service.hpp

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
#include <boost/asio/signal_set.hpp>
2525
#include <boost/container/flat_map.hpp>
2626

27-
#if BOOST_VERSION < 107000
28-
# define AZMQ_DETAIL_USE_IO_SERVICE 1
29-
#endif
3027

3128
#include <string>
3229
#include <vector>
@@ -42,12 +39,9 @@ namespace detail {
4239
public:
4340
inline static std::string get_uri(const char* pfx);
4441

45-
#ifdef AZMQ_DETAIL_USE_IO_SERVICE
46-
actor_service(boost::asio::io_service & ios)
47-
#else
48-
actor_service(boost::asio::io_context & ios)
49-
#endif
50-
: azmq::detail::service_base<actor_service>(ios)
42+
actor_service(boost::asio::io_context & ioc)
43+
44+
: azmq::detail::service_base<actor_service>(ioc)
5145
{ }
5246

5347
void shutdown() override { }
@@ -59,21 +53,13 @@ namespace detail {
5953

6054
template<typename T>
6155
socket make_pipe(bool defer_start, T&& data) {
62-
#ifdef AZMQ_DETAIL_USE_IO_SERVICE
63-
return make_pipe(get_io_service(), defer_start, std::forward<T>(data));
64-
#else
6556
return make_pipe(get_io_context(), defer_start, std::forward<T>(data));
66-
#endif
6757
}
6858

6959
template<typename T>
70-
#ifdef AZMQ_DETAIL_USE_IO_SERVICE
71-
static socket make_pipe(boost::asio::io_service & ios, bool defer_start, T&& data) {
72-
#else
73-
static socket make_pipe(boost::asio::io_context & ios, bool defer_start, T&& data) {
74-
#endif
60+
static socket make_pipe(boost::asio::io_context & ioc, bool defer_start, T&& data) {
7561
auto p = std::make_shared<model<T>>(std::forward<T>(data));
76-
auto res = p->peer_socket(ios);
62+
auto res = p->peer_socket(ioc);
7763
associate_ext(res, handler(std::move(p), defer_start));
7864
return std::move(res);
7965
}
@@ -82,7 +68,7 @@ namespace detail {
8268
struct concept_ {
8369
using ptr = std::shared_ptr<concept_>;
8470

85-
boost::asio::io_service io_service_;
71+
boost::asio::io_context io_context_;
8672
boost::asio::signal_set signals_;
8773
pair_socket socket_;
8874
thread_t thread_;
@@ -95,8 +81,8 @@ namespace detail {
9581
std::exception_ptr last_error_;
9682

9783
concept_()
98-
: signals_(io_service_, SIGINT, SIGTERM)
99-
, socket_(io_service_)
84+
: signals_(io_context_, SIGINT, SIGTERM)
85+
, socket_(io_context_)
10086
, ready_(false)
10187
, stopped_(true)
10288
{
@@ -105,11 +91,7 @@ namespace detail {
10591

10692
virtual ~concept_() = default;
10793

108-
#ifdef AZMQ_DETAIL_USE_IO_SERVICE
109-
pair_socket peer_socket(boost::asio::io_service & peer) {
110-
#else
11194
pair_socket peer_socket(boost::asio::io_context & peer) {
112-
#endif
11395
pair_socket res(peer);
11496
auto uri = socket_.endpoint();
11597
BOOST_ASSERT_MSG(!uri.empty(), "uri empty");
@@ -121,7 +103,7 @@ namespace detail {
121103

122104
void stop() {
123105
if (!joinable()) return;
124-
io_service_.stop();
106+
io_context_.stop();
125107
thread_.join();
126108
}
127109

@@ -165,7 +147,7 @@ namespace detail {
165147
static void run(ptr p) {
166148
lock_type l { p->mutex_ };
167149
p->signals_.async_wait([p](boost::system::error_code const&, int) {
168-
p->io_service_.stop();
150+
p->io_context_.stop();
169151
});
170152
p->stopped_ = false;
171153
p->thread_ = thread_t([p] {
@@ -200,7 +182,7 @@ namespace detail {
200182
, defer_start_(defer_start)
201183
{ }
202184

203-
void on_install(boost::asio::io_service&, void*) {
185+
void on_install(boost::asio::io_context&, void*) {
204186
if (defer_start_) return;
205187
defer_start_ = false;
206188
concept_::run(p_);

azmq/detail/basic_io_object.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef AZMQ_DETAIL_BASIC_IO_OBJECT_HPP__
1010
#define AZMQ_DETAIL_BASIC_IO_OBJECT_HPP__
1111

12-
#include "../io_service.hpp"
12+
#include <boost/asio/io_context.hpp>
1313
#include <boost/asio/basic_io_object.hpp>
1414

1515
namespace azmq {
@@ -41,8 +41,8 @@ namespace detail {
4141
friend class core_access<Service>;
4242

4343
public:
44-
basic_io_object(boost::asio::io_service& ios)
45-
: boost::asio::basic_io_object<Service>(ios)
44+
basic_io_object(boost::asio::io_context& ioc)
45+
: boost::asio::basic_io_object<Service>(ioc)
4646
{ }
4747
};
4848
} // namespace detail

azmq/detail/reactor_op.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
#ifndef AZMQ_DETAIL_REACTOR_OP_HPP_
1010
#define AZMQ_DETAIL_REACTOR_OP_HPP_
1111

12-
#include "../io_service.hpp"
1312
#include "../message.hpp"
1413
#include "socket_ops.hpp"
1514

15+
#include <boost/asio/io_context.hpp>
1616
#include <boost/optional.hpp>
1717

1818
namespace azmq {

azmq/detail/receive_op.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
#define AZMQ_DETAIL_RECEIVE_OP_HPP_
1111

1212
#include "../error.hpp"
13-
#include "../io_service.hpp"
1413
#include "../message.hpp"
1514
#include "socket_ops.hpp"
1615
#include "reactor_op.hpp"
1716

1817
#include <boost/version.hpp>
1918
#include <boost/asio/dispatch.hpp>
2019
#include <boost/asio/executor_work_guard.hpp>
20+
#include <boost/asio/io_context.hpp>
2121
#if BOOST_VERSION >= 107900
2222
#include <boost/asio/recycling_allocator.hpp>
2323
#include <boost/asio/bind_allocator.hpp>

0 commit comments

Comments
 (0)