問題描述
通過閱讀其他 Stack Overflow 條目和 boost::asio
文檔,我確認(rèn)沒有同步 ASIO 讀/寫調(diào)用也提供易于使用的超時(shí)作為調(diào)用的參數(shù).
From reading other Stack Overflow entries and the boost::asio
documentation, I've confirmed that there is no synchronous ASIO read/write calls that also provide an easy-to-use timeout as a parameter to the call.
我正在使用使用超時(shí)的 select(2) 調(diào)用轉(zhuǎn)換老式 Linux 套接字應(yīng)用程序,我需要或多或少地做同樣的事情.
I'm in the middle of converting an old-school Linux socket application with select(2) calls that employs timeouts, and I need to do more-or-less the same.
那么在 boost::asio
中執(zhí)行此操作的最佳方法是什么?查看 asio 文檔,有許多與計(jì)時(shí)器有關(guān)的各種事情的令人困惑的示例,但我很困惑.
So what is the best way to do this in boost::asio
? Looking at the asio documentation, there are many confusing examples of various things to do with timers, but I'm quite confused.
我很想看到一個(gè)簡(jiǎn)單易讀的例子:從套接字讀取,但最多等待 X 秒,之后函數(shù)要么不返回任何內(nèi)容,要么返回任何內(nèi)容能夠在超時(shí)到期之前從套接字讀取.
I'd love to see a simple-to-read example of this: Read from a socket, but wait for a maximum of X seconds after which the function either returns with nothing, or returns with whatever it was able to read from the socket before the timeout expired.
推薦答案
這已在 asio 郵件列表中提出,有一個(gè) ticket 也請(qǐng)求該功能.總而言之,如果您需要超時(shí)和可取消性,建議使用異步方法.
This has been brought up on the asio mailing lists, there's a ticket requesting the feature as well. To summarize, it is suggested to use asynchronous methods if you desire timeouts and cancellability.
如果您無法轉(zhuǎn)換為異步方法,您可以嘗試SO_RCVTIMEO
和SO_SNDTIMEO
套接字選項(xiàng).它們可以用 setsockopt
設(shè)置,描述符可以用 boost::asio::ip::tcp::socket::native 方法.man 7 socket
手冊(cè)頁說
If you cannot convert to asynchronous methods, you might try the SO_RCVTIMEO
and SO_SNDTIMEO
socket options. They can be set with setsockopt
, the descriptor can be obtained with the boost::asio::ip::tcp::socket::native method. The man 7 socket
man page says
SO_RCVTIMEO 和 SO_SNDTIMEO指定接收或發(fā)送超時(shí),直到報(bào)告錯(cuò)誤.參數(shù)是一個(gè)結(jié)構(gòu)體時(shí)間.如果輸入或輸出這個(gè)時(shí)期的功能塊時(shí)間,并且數(shù)據(jù)已經(jīng)發(fā)送或收到,該函數(shù)的返回值將是傳輸?shù)臄?shù)據(jù)量;如果不數(shù)據(jù)已傳輸并且已達(dá)到超時(shí)然后 -1 是返回 errno 設(shè)置為EAGAIN 或 EWOULDBLOCK 就像套接字被指定為是非阻塞的.如果超時(shí)是設(shè)置為零(默認(rèn))然后操作永遠(yuǎn)不會(huì)超時(shí).超時(shí)僅有效對(duì)于執(zhí)行套接字 I/O 的系統(tǒng)調(diào)用(例如,read(2),recvmsg(2), send(2), sendmsg(2));超時(shí)對(duì) select(2) 沒有影響,poll(2)、epoll_wait(2) 等
SO_RCVTIMEO and SO_SNDTIMEO Specify the receiving or sending timeouts until reporting an error. The argument is a struct timeval. If an input or output function blocks for this period of time, and data has been sent or received, the return value of that function will be the amount of data transferred; if no data has been transferred and the timeout has been reached then -1 is returned with errno set to EAGAIN or EWOULDBLOCK just as if the socket was specified to be non-blocking. If the timeout is set to zero (the default) then the operation will never timeout. Timeouts only have effect for system calls that perform socket I/O (e.g., read(2), recvmsg(2), send(2), sendmsg(2)); timeouts have no effect for select(2), poll(2), epoll_wait(2), etc.
這篇關(guān)于C++ Boost ASIO:如何讀/寫超時(shí)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!