我有以下一点 ASIO 代码,它同步读取 UDP 数据包。问题是,如果在给定的时间范围(30 秒)内没有给定大小的数据包到达,我希望 recieve_from 函数返回某种错误以指定超时。
for (;;)
{
boost::array<char, 1000> recv_buf;
udp::endpoint remote_endpoint;
asio::error_code error;
socket.receive_from(asio::buffer(recv_buf), // <-- require timeout
remote_endpoint, 0, error);
if (error && error != asio::error::message_size)
throw asio::system_error(error);
std::string message = make_daytime_string();
asio::error_code ignored_error;
socket.send_to(asio::buffer(message),
remote_endpoint, 0, ignored_error);
}
查看文档,非面向 UDP 的调用支持超时机制。
在 ASIO 中同步 UDP 调用超时的正确方法是什么(如果可能,也可以移植)?