在Boost.Asio中使用
SO_RCVTIMEO和
SO_SNDTIMEO套接字选项很少会产生所需的行为。考虑使用以下两种模式之一:
组成操作 async_wait()
可以使用Boost.Asio计时器和具有该
async_wait()操作的
async_receive()操作来构成具有超时的异步读取操作。Boost.Asio
超时示例中演示了这种方法,类似于:
// Start a timeout for the read.boost::asio::deadline_timer timer(io_service);timer.expires_from_now(boost::posix_time::seconds(1));timer.async_wait( [&socket, &timer](const boost::system::error_pre& error) { // On error, such as cancellation, return early. if (error) return; // Timer has expired, but the read operation's completion handler // may have already ran, setting expiration to be in the future. if (timer.expires_at() > boost::asio::deadline_timer::traits_type::now()) { return; } // The read operation's completion handler has not ran. boost::system::error_pre ignored_ec; socket.close(ignored_ec); });// Start the read operation.socket.async_receive(buffer, [&socket, &timer](const boost::system::error_pre& error, std::size_t bytes_transferred) { // Update timeout state to indicate the handler has ran. This // will cancel any pending timeouts. timer.expires_at(boost::posix_time::pos_infin); // On error, such as cancellation, return early. if (error) return; // At this point, the read was successful and buffer is populated. // However, if the timeout occurred and its completion handler ran first, // then the socket is closed (!socket.is_open()). });请注意,两个异步操作都可能在同一迭代中完成,这使得两个完成处理程序都可以成功运行。因此,这是两个完成处理程序都需要更新和检查状态的原因。有关如何管理状态的更多详细信息,请参见此答案。
采用 std::future
Boost.Asio 支持C ++
11期货。当
boost::asio::use_future提供作为异步操作的完成处理程序时,初始化函数将返回a
std::future,该操作将在操作完成后实现。由于
std::future支持定时等待,因此可以利用它来使操作超时。请注意,由于调用线程将被阻塞以等待将来,因此至少另一个线程必须正在处理,
io_service以允许
async_receive()操作进行并实现诺言:
// Use an asynchronous operation so that it can be cancelled on timeout.std::future<std::size_t> read_result = socket.async_receive( buffer, boost::asio::use_future);// If timeout occurs, then cancel the read operation.if (read_result.wait_for(std::chrono::seconds(1)) == std::future_status::timeout){ socket.cancel();}// Otherwise, the operation completed (with success or error).else{ // If the operation failed, then read_result.get() will throw a // boost::system::system_error. auto bytes_transferred = read_result.get(); // process buffer}为什么SO_RCVTIMEO
不起作用
系统行为
该
SO_RCVTIMEO文件指出,该选项仅影响系统调用执行套接字I /
O,如
read()和
recvmsg()。它不会影响事件多路分解器(例如
select()和)
poll(),它们仅监视文件描述符来确定何时可以发生I
/ O而不会阻塞。此外,当确实发生超时时,I / O调用将无法返回
-1并设置
errno为
EAGAIN或
EWOULDBLOCK。
指定接收或发送超时,直到报告错误。[…]如果没有数据被传输和达到超时然后
-1返回,并将errno设置EAGAIN或EWOULDBLOCK[…]超时只对系统调用执行套接字I
/ O(效应,例如,read(),recvmsg(),[。
..];超时有任何效果select(),poll(),epoll_wait(),等。
当底层的文件描述符设置为非阻塞,系统调用执行套接字I /
O将立即返回
EAGAIN或者
EWOULDBLOCK如果资源不立即可用。对于非阻塞套接字,
SO_RCVTIMEO将不会有任何影响,因为调用成功或失败都会立即返回。因此,为了
SO_RCVTIMEO影响系统I
/ O调用,套接字必须处于阻塞状态。
Boost.Asio行为
首先,Boost.Asio中的异步I /
O操作将使用事件多路分解器,例如
select()或
poll()。因此,
SO_RCVTIMEO将不会影响异步操作。
接下来,Boost.Asio的套接字具有两种非阻塞模式的概念(两种模式均默认为false):
native_non_blocking()
模式大致对应于文件描述符的非阻塞状态。此模式影响系统I / O调用。例如,如果调用socket.native_non_blocking(true)
,则设置为或recv(socket.native_handle(), ...)
可能会失败。每当在套接字上启动异步操作时,Boost.Asio都会启用此模式。errno``EAGAIN``EWOULDBLOCK
non_blocking()
模式会影响Boost.Asio的同步套接字操作。设置true
为时,Boost.Asio会将基础文件描述符设置为非阻塞,并且同步的Boost.Asio套接字操作可能会失败boost::asio::error::would_block
(或等效的系统错误)。当设置为false
,Boost.Asio的将阻塞,即使底层的文件描述符是无阻塞,通过轮询文件描述符和重新尝试的系统I / O操作是否EAGAIN
或EWOULDBLOCK
返回。
的行为
non_blocking()防止
SO_RCVTIMEO从产生所需的行为。假设
socket.receive()调用并且数据既不可用也不接收:
- 如果
non_blocking()
为false,则系统I / O调用将超时SO_RCVTIMEO
。但是,Boost.Asio随后将立即阻止对文件描述符的轮询以使其可读,这不受的影响SO_RCVTIMEO
。最终结果是呼叫者被阻塞,socket.receive()
直到接收到数据或发生故障为止,例如远程对等方关闭连接。 - 如果
non_blocking()
为true,则基础文件描述符也是非阻塞的。因此,系统I / O调用将忽略SO_RCVTIMEO
,立即通过EAGAIN
或返回EWOULDBLOCK
,并导致socket.receive()
失败boost::asio::error::would_block
。
理想情况下,
SO_RCVTIMEO要与Boost.Asio配合使用,需要
native_non_blocking()将其设置为false以使其
SO_RCVTIMEO生效,但也需要
non_blocking()将其设置为true以防止对描述符进行轮询。但是,Boost.Asio不支持此功能:
socket::native_non_blocking(bool mode)如果mode为
false,但is
的当前值为,non_blocking()则true此功能将以失败boost::asio::error::invalid_argument,因为组合没有意义。



