I'm trying to write a wrapper synchronous method around async_read
to allow non blocking reads on a socket. Following several examples around internet I have developed a solution that seems to be almost right but which is not working.
The class declares these relevant attributes and methods:
class communications_client
{
protected:
boost::shared_ptr<boost::asio::io_service> _io_service;
boost::shared_ptr<boost::asio::ip::tcp::socket> _socket;
boost::array<boost::uint8_t, 128> _data;
boost::mutex _mutex;
bool _timeout_triggered;
bool _message_received;
boost::system::error_code _error;
size_t _bytes_transferred;
void handle_read(const boost::system::error_code & error, size_t bytes_transferred);
void handle_timeout(const boost::system::error_code & error);
size_t async_read_helper(unsigned short bytes_to_transfer, const boost::posix_time::time_duration & timeout, boost::system::error_code & error);
...
}
The method async_read_helper
is the one that encapsulates all the complexity, while the other two handle_read
and handle_timeout
are just the event handlers. Here is the implementation of the three methods:
void communications_client::handle_timeout(const boost::system::error_code & error)
{
if (!error)
{
_mutex.lock();
_timeout_triggered = true;
_error.assign(boost::system::errc::timed_out, boost::system::system_category());
_mutex.unlock();
}
}
void communications_client::handle_read(const boost::system::error_code & error, size_t bytes_transferred)
{
_mutex.lock();
_message_received = true;
_error = error;
_bytes_transferred = bytes_transferred;
_mutex.unlock();
}
size_t communications_client::async_read_helper(unsigned short bytes_to_transfer, const boost::posix_time::time_duration & timeout, boost::system::error_code & error)
{
_timeout_triggered = false;
_message_received = false;
boost::asio::deadline_timer timer(*_io_service);
timer.expires_from_now(timeout);
timer.async_wait(
boost::bind(
&communications_client::handle_timeout,
this,
boost::asio::placeholders::error));
boost::asio::async_read(
*_socket,
boost::asio::buffer(_data, 128),
boost::asio::transfer_exactly(bytes_to_transfer),
boost::bind(
&communications_client::handle_read,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
while (true)
{
_io_service->poll_one();
if (_message_received)
{
timer.cancel();
break;
}
else if (_timeout_triggered)
{
_socket->cancel();
break;
}
}
return _bytes_transferred;
}
The main question I have is: why this works with a loop on _io_service->poll_one()
and no without a loop and calling _io_service->run_one()
? Also, I would like to know if it looks correct to anyone who is more used to work with Boost and Asio. Thank you!
FIX PROPOSAL #1
According to the comments done by Jonathan Wakely the loop could be replaced using _io_service->run_one()
with a call to _io_service->reset()
after the operations have finished. It should look like:
_io_service->run_one();
if (_message_received)
{
timer.cancel();
}
else if (_timeout_triggered)
{
_socket->cancel();
}
_io_service->reset();
After some testing, I have checked that this kind of solution alone is not working. The handle_timeout
method is being called continuously with the error code operation_aborted
. How can these calls be stopped?
FIX PROPOSAL #2
The answer by twsansbury is accurate and based onto solid documentation basis. That implementation leads to the following code within the async_read_helper
:
while (_io_service->run_one())
{
if (_message_received)
{
timer.cancel();
}
else if (_timeout_triggered)
{
_socket->cancel();
}
}
_io_service->reset();
and the following change to the handle_read
method:
void communications_client::handle_read(const boost::system::error_code & error, size_t bytes_transferred)
{
if (error != boost::asio::error::operation_aborted)
{
...
}
}
This solution has proved solid and correct during testing.
See Question&Answers more detail:os