Skip to content

Commit adfe953

Browse files
mbr0wnjoergho
andcommitted
uhd: Update ASIO usage to modern style, compatible with Boost 1.87
Starting with Boost 1.66 and the corresponding ASIO version, there were some changes introduced based on the C++ Networking TS. This includes changes like replacing io_service with io_context, deprecating some functions, etc. Starting with Boost 1.87, the old style is no longer supported. This commit updates all usage of ASIO in a way that makes UHD compatible with future versions of ASIO. However, this makes UHD no longer compatible with Boost 1.65 and below. Summary of changes: - Replace asio::io_service with asio::io_context - Replace asio::io_service::strand with asio::strand<asio::io_context::executor_type> - This implies using asio::post() instead of asio::strand::post() - Replace asio::buffer_cast<T>(buf) with static_cast<T>(buf.data()) - Update resolve(query) with new API - Replace references to endpoint_iterator with resolver::results_type - Replace ip::address::from_string() with ip::make_address() Co-authored-by: Jörg Hofrichter <[email protected]>
1 parent 8c56d97 commit adfe953

24 files changed

+164
-134
lines changed

host/examples/network_relay.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ class udp_relay_type
7878
: _port(port)
7979
{
8080
{
81-
asio::ip::udp::resolver resolver(_io_service);
82-
asio::ip::udp::resolver::query query(asio::ip::udp::v4(), server_addr, port);
83-
asio::ip::udp::endpoint endpoint = *resolver.resolve(query);
81+
asio::ip::udp::resolver resolver(_io_context);
82+
asio::ip::udp::endpoint endpoint =
83+
*resolver.resolve(asio::ip::udp::v4(), server_addr, port).begin();
8484

8585
_server_socket = std::shared_ptr<asio::ip::udp::socket>(
86-
new asio::ip::udp::socket(_io_service, endpoint));
86+
new asio::ip::udp::socket(_io_context, endpoint));
8787
resize_buffs(_server_socket, server_rx_size, server_tx_size);
8888
}
8989
{
90-
asio::ip::udp::resolver resolver(_io_service);
91-
asio::ip::udp::resolver::query query(asio::ip::udp::v4(), client_addr, port);
92-
asio::ip::udp::endpoint endpoint = *resolver.resolve(query);
90+
asio::ip::udp::resolver resolver(_io_context);
91+
asio::ip::udp::endpoint endpoint =
92+
*resolver.resolve(asio::ip::udp::v4(), client_addr, port).begin();
9393

9494
_client_socket = std::shared_ptr<asio::ip::udp::socket>(
95-
new asio::ip::udp::socket(_io_service));
95+
new asio::ip::udp::socket(_io_context));
9696
_client_socket->open(asio::ip::udp::v4());
9797
_client_socket->connect(endpoint);
9898
resize_buffs(_client_socket, client_rx_size, client_tx_size);
@@ -173,7 +173,7 @@ class udp_relay_type
173173

174174
const std::string _port;
175175
boost::thread_group _thread_group;
176-
asio::io_service _io_service;
176+
asio::io_context _io_context;
177177
asio::ip::udp::endpoint _endpoint;
178178
std::mutex _endpoint_mutex;
179179
socket_type _server_socket, _client_socket;

host/include/uhd/transport/nirio/rpc/rpc_client.hpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,20 @@ class rpc_client : private uhd::noncopyable
5656
const boost::system::error_code& err, size_t transferred, size_t expected);
5757
void _wait_for_next_response_header();
5858

59-
inline void _stop_io_service()
59+
inline void _stop_io_context()
6060
{
61-
if (_io_service_thread.get()) {
61+
if (_io_context_thread.get()) {
6262
UHD_LOG_DEBUG("NIRIO", "rpc_client stopping...");
63-
_io_service.stop();
64-
_io_service_thread->join();
65-
_io_service_thread.reset();
63+
_io_context.stop();
64+
_io_context_thread->join();
65+
_io_context_thread.reset();
6666
UHD_LOG_DEBUG("NIRIO", "rpc_client stopped.");
6767
}
6868
}
6969

7070
// Services
71-
boost::asio::io_service _io_service;
72-
std::unique_ptr<boost::thread> _io_service_thread;
71+
boost::asio::io_context _io_context;
72+
std::unique_ptr<boost::thread> _io_context_thread;
7373
boost::asio::ip::tcp::socket _socket;
7474
// Handshake info
7575
hshake_args_t _hshake_args_client;

host/lib/deps/rpclib/include/rpc/detail/async_writer.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ namespace detail {
1919
//! \brief Common logic for classes that have a write queue with async writing.
2020
class async_writer : public std::enable_shared_from_this<async_writer> {
2121
public:
22-
async_writer(boost::asio::io_service *io,
22+
async_writer(boost::asio::io_context* io,
2323
boost::asio::ip::tcp::socket socket)
24-
: socket_(std::move(socket)), write_strand_(*io), exit_(false) {}
24+
: socket_(std::move(socket)), write_strand_(io->get_executor()), exit_(false) {}
2525

2626
void do_write() {
2727
if (exit_) {
@@ -31,9 +31,9 @@ class async_writer : public std::enable_shared_from_this<async_writer> {
3131
auto &item = write_queue_.front();
3232
// the data in item remains valid until the handler is called
3333
// since it will still be in the queue physically until then.
34-
boost::asio::async_write(
35-
socket_, boost::asio::buffer(item.data(), item.size()),
36-
write_strand_.wrap(
34+
boost::asio::async_write(socket_,
35+
boost::asio::buffer(item.data(), item.size()),
36+
boost::asio::bind_executor(write_strand_,
3737
[this, self](boost::system::error_code ec, std::size_t transferred) {
3838
(void)transferred;
3939
if (!ec) {
@@ -69,7 +69,7 @@ class async_writer : public std::enable_shared_from_this<async_writer> {
6969

7070
protected:
7171
boost::asio::ip::tcp::socket socket_;
72-
boost::asio::io_service::strand write_strand_;
72+
boost::asio::strand<boost::asio::io_context::executor_type> write_strand_;
7373
std::atomic_bool exit_{false};
7474
bool exited_ = false;
7575
std::mutex m_exit_;

host/lib/deps/rpclib/include/rpc/detail/server_session.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace detail {
2222

2323
class server_session : public async_writer {
2424
public:
25-
server_session(server *srv, boost::asio::io_service *io,
25+
server_session(server* srv, boost::asio::io_context* io,
2626
boost::asio::ip::tcp::socket socket,
2727
std::shared_ptr<dispatcher> disp, bool suppress_exceptions);
2828
void start();
@@ -34,8 +34,8 @@ class server_session : public async_writer {
3434

3535
private:
3636
server* parent_;
37-
boost::asio::io_service *io_;
38-
boost::asio::io_service::strand read_strand_;
37+
boost::asio::io_context* io_;
38+
boost::asio::strand<boost::asio::io_context::executor_type> read_strand_;
3939
std::shared_ptr<dispatcher> disp_;
4040
RPCLIB_MSGPACK::unpacker pac_;
4141
RPCLIB_MSGPACK::sbuffer output_buf_;

host/lib/deps/rpclib/lib/rpc/client.cc

+12-12
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct client::impl {
3838
impl(client *parent, std::string const &addr, uint16_t port)
3939
: parent_(parent),
4040
io_(),
41-
strand_(io_),
41+
strand_(io_.get_executor()),
4242
call_idx_(0),
4343
addr_(addr),
4444
port_(port),
@@ -50,11 +50,11 @@ struct client::impl {
5050
pac_.reserve_buffer(default_buffer_size);
5151
}
5252

53-
void do_connect(tcp::resolver::iterator endpoint_iterator) {
53+
void do_connect(const tcp::resolver::results_type& endpoints) {
5454
LOG_INFO("Initiating connection.");
5555
boost::asio::async_connect(
56-
writer_->socket_, endpoint_iterator,
57-
[this](boost::system::error_code ec, tcp::resolver::iterator) {
56+
writer_->socket_, endpoints,
57+
[this](boost::system::error_code ec, tcp::endpoint) {
5858
std::unique_lock<std::mutex> lock(mut_connection_finished_);
5959
if (!ec) {
6060
LOG_INFO("Client connected to {}:{}", addr_, port_);
@@ -101,7 +101,7 @@ struct client::impl {
101101
std::get<1>(current_call)
102102
.set_exception(std::current_exception());
103103
}
104-
strand_.post(
104+
boost::asio::post(strand_,
105105
[this, id]() { ongoing_calls_.erase(id); });
106106
}
107107

@@ -144,8 +144,8 @@ struct client::impl {
144144
std::pair<std::string, std::promise<RPCLIB_MSGPACK::object_handle>>;
145145

146146
client *parent_;
147-
boost::asio::io_service io_;
148-
boost::asio::io_service::strand strand_;
147+
boost::asio::io_context io_;
148+
boost::asio::strand<boost::asio::io_context::executor_type> strand_;
149149
std::atomic<int> call_idx_; /// The index of the last call made
150150
std::unordered_map<uint32_t, call_t> ongoing_calls_;
151151
std::string addr_;
@@ -164,9 +164,9 @@ struct client::impl {
164164
client::client(std::string const &addr, uint16_t port)
165165
: pimpl(new client::impl(this, addr, port)) {
166166
tcp::resolver resolver(pimpl->io_);
167-
auto endpoint_it =
168-
resolver.resolve({pimpl->addr_, std::to_string(pimpl->port_)});
169-
pimpl->do_connect(endpoint_it);
167+
auto endpoints =
168+
resolver.resolve(pimpl->addr_, std::to_string(pimpl->port_));
169+
pimpl->do_connect(endpoints);
170170
std::thread io_thread([this]() {
171171
RPCLIB_CREATE_LOG_CHANNEL(client)
172172
name_thread("client");
@@ -190,15 +190,15 @@ int client::get_next_call_idx() {
190190
void client::post(std::shared_ptr<RPCLIB_MSGPACK::sbuffer> buffer, int idx,
191191
std::string const &func_name,
192192
std::shared_ptr<rsp_promise> p) {
193-
pimpl->strand_.post([buffer, idx, func_name, p, this]() {
193+
boost::asio::post(pimpl->strand_, [buffer, idx, func_name, p, this]() {
194194
pimpl->ongoing_calls_.insert(
195195
std::make_pair(idx, std::make_pair(func_name, std::move(*p))));
196196
pimpl->write(std::move(*buffer));
197197
});
198198
}
199199

200200
void client::post(RPCLIB_MSGPACK::sbuffer *buffer) {
201-
pimpl->strand_.post([buffer, this]() {
201+
boost::asio::post(pimpl->strand_, [buffer, this]() {
202202
pimpl->write(std::move(*buffer));
203203
delete buffer;
204204
});

host/lib/deps/rpclib/lib/rpc/detail/server_session.cc

+11-12
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ namespace detail {
2222

2323
static constexpr std::size_t default_buffer_size = rpc::constants::DEFAULT_BUFFER_SIZE;
2424

25-
server_session::server_session(server *srv, boost::asio::io_service *io,
25+
server_session::server_session(server *srv, boost::asio::io_context *io,
2626
boost::asio::ip::tcp::socket socket,
2727
std::shared_ptr<dispatcher> disp,
2828
bool suppress_exceptions)
2929
: async_writer(io, std::move(socket)),
3030
parent_(srv),
3131
io_(io),
32-
read_strand_(*io),
32+
read_strand_(io->get_executor()),
3333
disp_(disp),
3434
pac_(),
3535
suppress_exceptions_(suppress_exceptions) {
@@ -42,7 +42,7 @@ void server_session::start() { do_read(); }
4242
void server_session::close() {
4343
LOG_INFO("Closing session.");
4444
exit_ = true;
45-
write_strand_.post([this]() {
45+
boost::asio::post(write_strand_, [this]() {
4646
try {
4747
socket_.close();
4848
} catch (const boost::system::system_error&) {
@@ -54,12 +54,11 @@ void server_session::close() {
5454
void server_session::do_read() {
5555
auto self(shared_from_this());
5656
constexpr std::size_t max_read_bytes = default_buffer_size;
57-
socket_.async_read_some(
58-
boost::asio::buffer(pac_.buffer(), default_buffer_size),
57+
socket_.async_read_some(boost::asio::buffer(pac_.buffer(), default_buffer_size),
5958
// I don't think max_read_bytes needs to be captured explicitly
6059
// (since it's constexpr), but MSVC insists.
61-
read_strand_.wrap([this, self, max_read_bytes](boost::system::error_code ec,
62-
std::size_t length) {
60+
boost::asio::bind_executor(read_strand_, [this, self, max_read_bytes](boost::system::error_code ec,
61+
std::size_t length) {
6362
if (!ec) {
6463
pac_.buffer_consumed(length);
6564
RPCLIB_MSGPACK::unpacked result;
@@ -69,7 +68,7 @@ void server_session::do_read() {
6968

7069
// any worker thread can take this call
7170
auto z = std::shared_ptr<RPCLIB_MSGPACK::zone>(result.zone().release());
72-
io_->post([
71+
boost::asio::post(io_->get_executor(), [
7372
this, msg, z
7473
]() {
7574
this_handler().clear();
@@ -102,10 +101,10 @@ void server_session::do_read() {
102101
if (!resp.is_empty()) {
103102
#ifdef _MSC_VER
104103
// doesn't compile otherwise.
105-
write_strand_.post(
104+
boost::asio::post(write_strand_,
106105
[=]() { write(resp.get_data()); });
107106
#else
108-
write_strand_.post(
107+
boost::asio::post(write_strand_,
109108
[this, resp, z]() { write(resp.get_data()); });
110109
#endif
111110
}
@@ -114,14 +113,14 @@ void server_session::do_read() {
114113
LOG_WARN("Session exit requested from a handler.");
115114
// posting through the strand so this comes after
116115
// the previous write
117-
write_strand_.post([this]() { exit_ = true; });
116+
boost::asio::post(write_strand_, [this]() { exit_ = true; });
118117
}
119118

120119
if (this_server().stopping_) {
121120
LOG_WARN("Server exit requested from a handler.");
122121
// posting through the strand so this comes after
123122
// the previous write
124-
write_strand_.post(
123+
boost::asio::post(write_strand_,
125124
[this]() { parent_->close_sessions(); });
126125
}
127126
});

host/lib/deps/rpclib/lib/rpc/server.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct server::impl {
2525
: parent_(parent),
2626
io_(),
2727
acceptor_(io_,
28-
tcp::endpoint(ip::address::from_string(address), port)),
28+
tcp::endpoint(ip::make_address(address), port)),
2929
socket_(io_),
3030
suppress_exceptions_(false) {}
3131

@@ -66,7 +66,7 @@ struct server::impl {
6666
}
6767

6868
server *parent_;
69-
io_service io_;
69+
io_context io_;
7070
ip::tcp::acceptor acceptor_;
7171
ip::tcp::socket socket_;
7272
rpc::detail::thread_group loop_workers_;

host/lib/include/uhdlib/transport/udp_boost_asio_link.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class udp_boost_asio_link : public recv_link_base<udp_boost_asio_link>,
146146
std::vector<udp_boost_asio_frame_buff> _recv_buffs;
147147
std::vector<udp_boost_asio_frame_buff> _send_buffs;
148148

149-
boost::asio::io_service _io_service;
149+
boost::asio::io_context _io_context;
150150
std::shared_ptr<boost::asio::ip::udp::socket> _socket;
151151
int _sock_fd;
152152
adapter_id_t _adapter_id;

host/lib/include/uhdlib/transport/udp_common.hpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,16 @@ UHD_INLINE bool wait_for_recv_ready(int sock_fd, int32_t timeout_ms)
8181
}
8282

8383
UHD_INLINE socket_sptr open_udp_socket(
84-
const std::string& addr, const std::string& port, boost::asio::io_service& io_service)
84+
const std::string& addr, const std::string& port, boost::asio::io_context& io_context)
8585
{
8686
using udp = boost::asio::ip::udp;
8787

8888
// resolve the address
89-
udp::resolver resolver(io_service);
90-
udp::resolver::query query(udp::v4(), addr, port);
91-
udp::endpoint receiver_endpoint = *resolver.resolve(query);
89+
udp::resolver resolver(io_context);
90+
udp::endpoint receiver_endpoint = *resolver.resolve(udp::v4(), addr, port).begin();
9291

9392
// create, open, and connect the socket
94-
socket_sptr socket = socket_sptr(new udp::socket(io_service));
93+
socket_sptr socket = socket_sptr(new udp::socket(io_context));
9594
socket->open(udp::v4());
9695
socket->connect(receiver_endpoint);
9796

host/lib/include/uhdlib/utils/eeprom_utils.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <uhd/types/dict.hpp>
1111
#include <uhd/types/mac_addr.hpp>
1212
#include <uhd/utils/log.hpp>
13-
#include <boost/asio/ip/address_v4.hpp>
1413
#include <string>
1514
#include <vector>
1615

@@ -29,7 +28,7 @@ std::string uint16_bytes_to_string(const uhd::byte_vector_t& bytes);
2928
* see if the resulting contents will contain duplicates. Useful error
3029
* messages are logged describing any duplicates.
3130
*
32-
* <field_type> must provide to_string() and from_string() functions
31+
* <field_type> must provide from_string() functions
3332
*
3433
* \param error_label Label to put on error messages
3534
* \param new_eeprom New EEPROM contents
@@ -38,12 +37,13 @@ std::string uint16_bytes_to_string(const uhd::byte_vector_t& bytes);
3837
* \param keys Keys to examine for duplicate values
3938
* \return true if duplicates are found, false if not
4039
*/
41-
template <typename field_type>
40+
template <typename str_normalizer_type>
4241
bool check_for_duplicates(const std::string& error_label,
4342
const uhd::dict<std::string, std::string>& new_eeprom,
4443
const uhd::dict<std::string, std::string>& curr_eeprom,
4544
const std::string& category,
46-
const std::vector<std::string>& keys)
45+
const std::vector<std::string>& keys,
46+
str_normalizer_type&& str_normalizer)
4747
{
4848
bool has_duplicates = false;
4949
for (size_t i = 0; i < keys.size(); i++) {
@@ -54,7 +54,7 @@ bool check_for_duplicates(const std::string& error_label,
5454
continue;
5555
}
5656

57-
auto value = field_type::from_string(new_eeprom[key]).to_string();
57+
auto value = str_normalizer(new_eeprom[key]);
5858

5959
// Check other values in new_eeprom for duplicate
6060
// Starting at key index i+1 so the same duplicate is not found twice
@@ -63,7 +63,7 @@ bool check_for_duplicates(const std::string& error_label,
6363
if (not new_eeprom.has_key(other_key)) {
6464
continue;
6565
}
66-
auto other_value = field_type::from_string(new_eeprom[other_key]).to_string();
66+
auto other_value = str_normalizer(new_eeprom[other_key]);
6767
if (value == other_value) {
6868
// Value is a duplicate of another supplied value
6969
UHD_LOG_ERROR(error_label,

host/lib/transport/dpdk_simple.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class dpdk_simple_impl : public dpdk_simple
9696
// Extract buff and sanity check
9797
const size_t nbytes = boost::asio::buffer_size(user_buff);
9898
UHD_ASSERT_THROW(nbytes <= _link->get_send_frame_size())
99-
const uint8_t* user_data = boost::asio::buffer_cast<const uint8_t*>(user_buff);
99+
const uint8_t* user_data = static_cast<const uint8_t*>(user_buff.data());
100100

101101
// Get send buff
102102
auto buff = _send_io->get_send_buff(SEND_TIMEOUT_MS);
@@ -120,7 +120,7 @@ class dpdk_simple_impl : public dpdk_simple
120120
size_t recv(const boost::asio::mutable_buffer& user_buff, double timeout) override
121121
{
122122
size_t user_buff_size = boost::asio::buffer_size(user_buff);
123-
uint8_t* user_data = boost::asio::buffer_cast<uint8_t*>(user_buff);
123+
uint8_t* user_data = static_cast<uint8_t*>(user_buff.data());
124124

125125
auto buff = _recv_io->get_recv_buff(static_cast<int32_t>(timeout * 1e3));
126126
if (!buff) {

0 commit comments

Comments
 (0)