Rename io_service_ variables to io_context_

As of Boost 1.66 these are of type io_context, so it makes it clearer.
This commit is contained in:
Charles Dang 2020-12-06 10:39:56 +11:00
parent 73b18717d3
commit b18503359f
4 changed files with 28 additions and 24 deletions

View File

@ -56,9 +56,9 @@ namespace network_asio
using boost::system::system_error;
connection::connection(const std::string& host, const std::string& service)
: io_service_()
, resolver_(io_service_)
, socket_(io_service_)
: io_context_()
, resolver_(io_context_)
, socket_(io_context_)
, done_(false)
, write_buf_()
, read_buf_()
@ -69,8 +69,8 @@ connection::connection(const std::string& host, const std::string& service)
, bytes_to_read_(0)
, bytes_read_(0)
{
resolver_.async_resolve(
boost::asio::ip::tcp::resolver::query(host, service), std::bind(&connection::handle_resolve, this, std::placeholders::_1, std::placeholders::_2));
resolver_.async_resolve(boost::asio::ip::tcp::resolver::query(host, service),
std::bind(&connection::handle_resolve, this, std::placeholders::_1, std::placeholders::_2));
LOG_NW << "Resolving hostname: " << host << '\n';
}
@ -130,7 +130,7 @@ void connection::handle_handshake(const boost::system::error_code& ec)
void connection::transfer(const config& request, config& response)
{
io_service_.reset();
io_context_.reset();
done_ = false;
write_buf_.reset(new boost::asio::streambuf);
@ -147,10 +147,12 @@ void connection::transfer(const config& request, config& response)
bufs.push_front(boost::asio::buffer(reinterpret_cast<const char*>(&payload_size_), 4));
boost::asio::async_write(socket_, bufs, std::bind(&connection::is_write_complete, this, std::placeholders::_1, std::placeholders::_2),
boost::asio::async_write(socket_, bufs,
std::bind(&connection::is_write_complete, this, std::placeholders::_1, std::placeholders::_2),
std::bind(&connection::handle_write, this, std::placeholders::_1, std::placeholders::_2));
boost::asio::async_read(socket_, *read_buf_, std::bind(&connection::is_read_complete, this, std::placeholders::_1, std::placeholders::_2),
boost::asio::async_read(socket_, *read_buf_,
std::bind(&connection::is_read_complete, this, std::placeholders::_1, std::placeholders::_2),
std::bind(&connection::handle_read, this, std::placeholders::_1, std::placeholders::_2, std::ref(response)));
}

View File

@ -73,7 +73,7 @@ public:
std::size_t poll()
{
try {
return io_service_.poll();
return io_context_.poll();
} catch(const boost::system::system_error& err) {
if(err.code() == boost::asio::error::operation_aborted) {
return 1;
@ -90,7 +90,7 @@ public:
*/
void run()
{
io_service_.run();
io_context_.run();
}
void cancel();
@ -122,7 +122,8 @@ public:
}
private:
boost::asio::io_service io_service_;
// TODO: make this of type io_context once we require Boost 1.66 or later
boost::asio::io_service io_context_;
typedef boost::asio::ip::tcp::resolver resolver;
resolver resolver_;

View File

@ -52,9 +52,9 @@ using boost::system::system_error;
// main thread
wesnothd_connection::wesnothd_connection(const std::string& host, const std::string& service)
: worker_thread_()
, io_service_()
, resolver_(io_service_)
, socket_(io_service_)
, io_context_()
, resolver_(io_context_)
, socket_(io_context_)
, last_error_()
, last_error_mutex_()
, handshake_finished_()
@ -76,7 +76,7 @@ wesnothd_connection::wesnothd_connection(const std::string& host, const std::str
// Starts the worker thread. Do this *after* the above async_resolve call or it will just exit immediately!
worker_thread_ = std::thread([this]() {
try {
io_service_.run();
io_context_.run();
} catch(const boost::system::system_error&) {
try {
// Attempt to pass the exception on to the handshake promise.
@ -203,11 +203,11 @@ void wesnothd_connection::send_data(const configr_of& request)
std::ostream os(buf_ptr.get());
write_gz(os, request);
// No idea why io_service::post doesn't like this lambda while asio::post does.
// No idea why io_context::post doesn't like this lambda while asio::post does.
#if BOOST_VERSION >= 106600
boost::asio::post(io_service_, [this, buf_ptr = std::move(buf_ptr)]() mutable {
boost::asio::post(io_context_, [this, buf_ptr = std::move(buf_ptr)]() mutable {
#else
io_service_.post([this, buf_ptr]() {
io_context_.post([this, buf_ptr]() {
#endif
DBG_NW << "In wesnothd_connection::send_data::lambda\n";
send_queue_.push(std::move(buf_ptr));
@ -247,7 +247,7 @@ void wesnothd_connection::stop()
{
// TODO: wouldn't cancel() have the same effect?
MPTEST_LOG;
io_service_.stop();
io_context_.stop();
}
// worker thread
@ -262,7 +262,7 @@ std::size_t wesnothd_connection::is_write_complete(const boost::system::error_co
LOG_NW << __func__ << " Error: " << ec << "\n";
io_service_.stop();
io_context_.stop();
return bytes_to_write_ - bytes_transferred;
}
@ -286,7 +286,7 @@ void wesnothd_connection::handle_write(const boost::system::error_code& ec, std:
LOG_NW << __func__ << " Error: " << ec << "\n";
io_service_.stop();
io_context_.stop();
return;
}
@ -308,7 +308,7 @@ std::size_t wesnothd_connection::is_read_complete(const boost::system::error_cod
LOG_NW << __func__ << " Error: " << ec << "\n";
io_service_.stop();
io_context_.stop();
return bytes_to_read_ - bytes_transferred;
}
@ -349,7 +349,7 @@ void wesnothd_connection::handle_read(const boost::system::error_code& ec, std::
LOG_NW << __func__ << " Error: " << ec << "\n";
io_service_.stop();
io_context_.stop();
return;
}

View File

@ -124,7 +124,8 @@ public:
private:
std::thread worker_thread_;
boost::asio::io_service io_service_;
// TODO: make this of type io_context once we require Boost 1.66 or later
boost::asio::io_service io_context_;
typedef boost::asio::ip::tcp::resolver resolver;
resolver resolver_;