gpt4 book ai didi

c++ - async_read_some 模拟同步超时接收

转载 作者:搜寻专家 更新时间:2023-10-31 00:40:11 25 4
gpt4 key购买 nike

我的程序总是使用依赖于平台的同步接收,它会阻塞执行直到超时或接收事件,例如:

recv(buf, size, timeout);

现在我想用 boost 替换这段代码,使其跨平台。我找到了解决方案,但我认为它很难看(与单个函数调用相比)。我写了这个:

void IPV4_HOST::recv_timer_handler(const boost::system::error_code & 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 & , 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(&IPV4_HOST::recv_timer_handler, this, boost::asio::placeholders::error));

asio_socket.async_read_some(boost::asio::buffer(buf, buf_size),
boost::bind(&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;
}

请问我这样做对不对?有没有更简单的方法来做到这一点?

最佳答案

方向是对的,但有一些微妙的地方需要考虑:

  • 如果将任何其他工作发布到 io_service,则 IPV4_HOST::receive() 将开始处理该工作。
  • 何时 io_service::run()正常情况下返回,暗示io_service已经停止。对 run()run_one()poll()poll_one() 的后续调用将立即返回除非 io_service 是 reset() .
  • 两个异步操作有可能同时完成,使两个完成处理程序准备好成功运行。 deadline_timer::cancel() 的备注部分强调了此行为;但是,所有异步操作都显示此 behavior .在现有代码中,当 bytes_received 大于零时,这可能导致抛出 IO::TimeoutReceiveException

处理 io_service 细节以及完成处理程序的非确定性执行顺序的一种解决方案可能类似于:

void IPV4_HOST::recv_timer_handler(const boost::system::error_code & e)
{
timer_handled = true;
if (!e) {
f_recv_timeout = true;
asio_socket.cancel();
}
}

void IPV4_HOST::recv_handler(const boost::system::error_code &,
size_t bytes)
{
recv_handled = true;
recv_timer.cancel();
bytes_received = bytes;
}

int IPV4_HOST::receive(void * buf, size_t buf_size, TIME::INTERVAL timeout)
{
timer_handled = false;
recv_handled = false;
f_recv_timeout = false;
bytes_received = 0;

recv_timer.expires_from_now(timeout.get_boost_milli());
recv_timer.async_wait(
boost::bind(&IPV4_HOST::recv_timer_handler, this,
boost::asio::placeholders::error));

asio_socket.async_read_some(
boost::asio::buffer(buf, buf_size),
boost::bind(&IPV4_HOST::recv_handler, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));

// If a handler has not ran, then keep processing work on the io_service.
// We need to consume both handlers so that old handlers are not in the
// io_service the next time receive is called.
while (!timer_handled || !recv_handled)
{
io_service.run_one();
}

// If the io_service has stopped (due to running out of work), then reset
// it so that it can be run on next call to receive.
if (io_service.stopped())
io_service.reset();

// If no bytes were received and the timeout occurred, then throw. This
// handles the case where both a timeout and receive occurred at the
// same time.
if (!bytes_received && f_recv_timeout)
throw IO::TimeoutReceiveException();

return bytes_received;
}

此外,当您尝试获得跨平台行为时,请阅读 basic_stream_socket::cancel() 的评论.需要注意一些特定于平台的行为。

关于c++ - async_read_some 模拟同步超时接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15158198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com