Boost logo

Boost :

From: Christopher Kohlhoff (chris_at_[hidden])
Date: 2007-04-26 09:18:31

On Tue, 24 Apr 2007 23:44:35 -0700, "Dean Michael Berris"
<mikhailberis_at_[hidden]> said:
> For a myriad of reasons, we're using blocking API calls
> (boost::asio::read_until and boost::asio::write) but these API calls
> don't seem to readily have a way of setting a timeout value -- one
> that says "try reading until either the response matches this... or
> throw an error after 1 second if you don't get a response from the
> server".
> Would there be a different way of going about it, aside from using
> asynchronous operations?

No, async operations would be the way to go. Something like (warning,
untested code):

  void set_result(optional<error_code>* a, error_code b)

  template <typename MutableBufferSequence>
  void read_with_timeout(tcp::socket& sock,
      const MutableBufferSequence& buffers)
    optional<error_code> timer_result;
    deadline_timer timer(sock.io_service());
    timer.async_wait(boost::bind(set_result, &timer_result, _1));

    optional<error_code> read_result;
    async_read(sock, buffers,
        boost::bind(set_result, &read_result, _1));
    while (sock.io_service().run_one())
      if (read_result)
      else if (timer_result)

    if (*read_result)
      throw system_error(*read_result);


Boost list run by bdawes at, gregod at, cpdaniel at, john at