当 boost.asio 流上有一些数据需要读取而不将其读入缓冲区时,如何获得回调?
似乎自 boost 1.40.0 以来, async_read_some()
调用有效。
以前,您可以传入 null_buffer
当有数据要读取时,您会收到回调,但框架不会将数据读入任何缓冲区(因为没有数据!)。这基本上允许您编写类似于 select()
< 的代码/a> 调用,当您的套接字上有一些数据时,您会被告知。
在新代码中,行为已更改为按以下方式工作:
如果序列mb中所有缓冲区的总大小为0,则异步读取操作应立即完成,并将0作为参数传递给指定读取字节数的处理程序。
这意味着我的旧方法(顺便说一下, 这个官方示例)检测套接字上数据的方式不再有效。对我来说,问题是我需要一种方法来检测这一点,因为我已经将自己的流类分层在 asio 套接字流之上,因此,我不能只从我的流期望存在的套接字读取数据。我现在能想到的唯一解决方法是读取单个字节,存储它,然后当我的流类请求一些字节时,如果设置了该字节,则返回该字节:不太漂亮。
有谁知道在最新的 boost.asio 代码下实现这种行为的更好方法?
It seems that since boost 1.40.0 there has been a change to the way that the the async_read_some()
call works.
Previously, you could pass in a null_buffer
and you would get a callback when there was data to read, but without the framework reading the data into any buffer (because there wasn't one!). This basically allowed you to write code that acted like a select()
call, where you would be told when your socket had some data on it.
In the new code the behaviour has been changed to work in the following way:
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.
This means that my old (and incidentally, the method shown in this official example) way of detecting data on the socket no longer works. The problem for me is that I need a way detecting this because I've layered my own streaming classes on-top of the asio socket streams and as such, I cannot just read data off the sockets that my streams will expect to be there. The only workaround I can think of right now is to read a single byte, store it and when my stream classes then request some bytes, return that byte if one is set: not pretty.
Does anyone know of a better way to implement this kind of behaviour under the latest boost.asio code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用 boost-1.41 的官方示例进行了快速测试...所以我认为它仍然应该有效(如果您使用 null_buffers)
My quick test with an official example with boost-1.41 works... So I think it still should work (if you use null_buffers)