asio::io_context::run_one() with while()

asio::io_context::run_one() with while()

本文关键字:while with run io context asio one      更新时间:2023-10-16

为什么在以下代码中使用asio::io_context::run_one()时需要while循环?如果我们不使用while循环怎么办,如下所示?

size_t numberOfHandler = sock.io_service().run_one();
void set_result(optional<error_code>* a, error_code b)
  {
    a->reset(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.expires_from_now(seconds(1));
    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));
    sock.io_service().reset();
    while (sock.io_service().run_one())
    {
      if (read_result)
        timer.cancel();
      else if (timer_result)
        sock.cancel();
    }
    if (*read_result)
      throw system_error(*read_result);
  }

您需要使用 while 循环来处理所有处理程序,以便在超时过后无法在此超时内读取数据时引发异常。

async_wait 的处理程序在 async_read 的处理程序之前执行的情况意味着套接字无法在 1 秒内读取数据。

1. run_one is called
2. handler passed into async_wait is called
3. timer_result is set
4. else if (timer_result) is processed
5. sock.cancel() is called [ cancel() queues handler with error code !! 
       which indicates that read operation was aborted]
6. handler passed into async_read is queued

传递到async_read的处理程序已排队,但是何时调用?它只能由 run_one 调用。如果未调用此处理程序,则无法设置read_result。那么,您希望如何发出信号,以便在一段时间内无法读取数据的情况? read_result只能通过在 run_one 中调用处理程序来设置。这就是为什么您需要处理内部循环run_one以调用处理程序,该处理程序read_result可选,错误代码指示中止的读取操作。