title: async_read_some to emulate syncronous timeout receive
body: <p>My program always used the platform-dependent synchronous receive, which blocked the execution until timeout or receive event, something like:</p>
  
  <p><code>recv(buf, size, timeout);</code> </p>
  
  <p>Now I want to replace this code with boost to make it cross-platform. I found the solution, but it is quite ugly I think (in comparison with single function call). I wrote this:</p>
  
  <pre><code>void IPV4_HOST::recv_timer_handler(const boost::system::error_code &amp; e)
  {
      if (e.value() == boost::system::errc::success) {
          f_recv_timeout = true;
          asio_socket.cancel();
      }
  }
  
  void IPV4_HOST::recv_handler(const boost::system::error_code &amp; , size_t bytes)
  {
      recv_timer.cancel();
      bytes_received = bytes;
  }
  
  int IPV4_HOST::receive(void * buf, size_t buf_size, TIME::INTERVAL timeout)
  {
      f_recv_timeout = false;
      bytes_received = 0;
  
      recv_timer.expires_from_now(timeout.get_boost_milli());
      recv_timer.async_wait(boost::bind(&amp;IPV4_HOST::recv_timer_handler, this, boost::asio::placeholders::error));
  
      asio_socket.async_read_some(boost::asio::buffer(buf, buf_size),
                                  boost::bind(&amp;IPV4_HOST::recv_handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
  
      io_service.run();
  
      if (f_recv_timeout)
          throw IO::TimeoutReceiveException();
  
      return bytes_received;
  }
  </code></pre>
  
  <p>Could you please advise, am I doing it right or not? Is there simpler way to do this?</p>