title: Boost ASIO asynchronous socket with timeout
body: <p>I am trying to find the proper / canonical way to implement the code below that provides a synchronous wrapper around async asio methods in order to have a timeout.  The code appears to work, but none of the examples I have looked at use the boolean in the lambda to terminate the do/while loop running i/o service, so I'm not sure if this is the proper form or if it will have unintended consequences down the road.  Some do things like 
      while(IOService.run_one);
  but that never terminates.</p>
  
  <p>Edit:
  I'm trying to follow this example:
  <a href="http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/timeouts/blocking_tcp_client.cpp" rel="nofollow noreferrer">http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/example/timeouts/blocking_tcp_client.cpp</a></p>
  
  <p>But in this code they avoid needing the number of bytes read by using a \n terminator.  I need the number of bytes read, hence the callback.</p>
  
  <p>I have seen many other solutions that use boost async futures as well as other methods, but they do not seem to compile with the versions of gcc / boost standard for Ubuntu 16.04 and I would like to stay with those versions.  </p>
  
  <pre><code>ByteArray SessionInfo::Read(const boost::posix_time::time_duration &amp;timeout)
  {
  
    Deadline.expires_from_now(timeout);
    auto bytes_received = 0lu;
    auto got_callback = false;
  
    SessionSocket-&gt;async_receive(boost::asio::buffer(receive_buffer_,
                                 1024),
                                 [&amp;bytes_received, &amp;got_callback](const boost::system::error_code &amp;error, std::size_t bytes_transferred) {
                               bytes_received = bytes_transferred;
                               got_callback = true;
                             });
    do
    {
      IOService.run_one();
    }while (!got_callback);
  
    auto bytes = ByteArray(receive_buffer_, receive_buffer_ + bytes_received);
    return bytes;
  }
  </code></pre>