gpt4 book ai didi

c++ - `stream.async_read_some(null_buffers,handler)` 是否应该立即完成?

转载 作者:太空宇宙 更新时间:2023-11-04 12:56:06 27 4
gpt4 key购买 nike

上下文:我正致力于在与 Boost ASIO 集成的 RDMA 包装器库之上实现一个简单的流适配器。

我遇到的问题是,客户端代码调用的 boost::asio::async_read 聚合器挂起,即使流已返回足够的数据来填充缓冲区,如果内部接收缓冲区中不再有未决数据。调试似乎表明它正在使用大小为 0 的单个缓冲区调用我的流适​​配器的 async_read_some 方法。

我发现的文档似乎在该操作是否应立即完成方面存在冲突。一方面,the AsyncReadStream concept specification说:

If the total size of all buffers in the sequence mb is 0, the asynchronous read operation shall complete immediately and pass 0 as the argument to the handler that specifies the number of bytes read.

另一方面,the overview of boost::asio::null_buffers说:

A null_buffers operation doesn't return until the I/O object is "ready" to perform the operation.

(事实上,在其他地方,我依靠它来注册处理程序,以便在 rdma_cm 和 ibverbs 完成 channel FD 指示可用事件时调用。)但是查看 null_buffers 的实现,它看起来它只是一个不包含缓冲区的静态对象,因此这似乎满足所有缓冲区的总大小为 0 的序列条件。

所以,我对我的 async_read_some 方法应该如何处理尝试读取 0 字节的情况感到困惑。作为一个疯狂的猜测,也许它应该是这样的:在像 null_buffers 这样的真正空序列上它应该只在接收缓冲区中有可用数据时完成,而如果它有一个非空序列缓冲区的长度等于 0 那么无论接收缓冲区的状态如何,它都应该立即完成?

最佳答案

注意 null_buffers 自 Boost 1.66.0 以来已被弃用:"(Deprecated: Use the socket/descriptor wait() and async_wait() member functions.)"

As a wild guess, maybe it should be something like: on a truly empty sequence like null_buffers it should complete only when there's data available in the receive buffer, while if it has a non-empty sequence with total length of the buffers equal to 0 then it should complete immediately regardless of the state of the receive buffer?

没有。 null_buffers 不是“空缓冲区”或“零长度缓冲区”。它是“无缓冲区”,向 ASIO 例程发出信号,表明没有缓冲区(因此大小在逻辑上不能被认为是零)。

所有与缓冲区大小有关的完成说明都是无关紧要的,因为不存在缓冲区。相关文档是 Reactor Style Operations .

您说得对:传递 null_buffers 表示您希望将 react 器式操作编织到 asio 事件子系统中(意思是,您可以异步等待套接字准备好进行读/写,然后执行实际的 IO,例如将底层套接字句柄传递给本身不支持异步 IO 的第三方 API。

关于c++ - `stream.async_read_some(null_buffers,handler)` 是否应该立即完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46611466/

27 4 0