使用 boost::asio 丢弃数据
我在异步模式下使用 boost::asio,并且我想跳过/丢弃/删除通过 TCP 发送给我的消息。我想这样做是因为我已经阅读了该消息的标题,并且我知道我对此不感兴趣。该消息可能很大,因此我宁愿不为其分配空间,甚至最好根本不将其传输到用户空间。
我看到 boost::asio::null_buffers 但它似乎不适用于此处(请参阅 https ://svn.boost.org/trac/boost/ticket/3627)。
I'm using boost::asio in asynchronous mode and I'd like to skip/discard/drop a message that has been sent to me over TCP. I want to do this because I've already read the header for the message and I know that it is of no interest to me. The message may be large so it I would prefer not to allocate space for it and even better not to transfer it into user space at all.
I see boost::asio::null_buffers but it does not appear to be applicable here (see https://svn.boost.org/trac/boost/ticket/3627).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,BSD 套接字接口不提供此功能。你总是必须读入缓冲区。现在,为了不分配巨大的缓冲区,您可以做的就是在循环中读入较小的缓冲区。像这样的事情:
这还没有通过编译器传递,因此可能存在愚蠢的拼写错误,但它应该说明这一点。
As far as I know, the BSD socket interface doesn't give you this functionality. You always have to read into a buffer. Now, what you can do in order to not allocate a huge buffer is to read into a smaller buffer in a loop. Something like this:
This has not been passed through a compiler, so there might be silly typos, but it should illustrate the point.
Boost的Asio是一个我自己还没有使用过的库。所以我不知道是否任何带有
Sink
接口的东西都可以插入。但如果是这样,boost 的null_sink
是否足以适合您的情况......?http://www.boost.org/doc /libs/1_46_1/libs/iostreams/doc/classes/null.html
(它只会丢弃数据,所以你仍然在用户空间中。如果还有一种方法可以做到这一点,但如果可以的话那就太酷了。)
Boost's Asio is a library I haven't used myself yet. So I don't know if just anything with a
Sink
interface can plug in. But if so, would boost'snull_sink
work well enough for your situation...?http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/classes/null.html
(It would just throw the data away, so you're still in user space. I'd be surprised if there's a way to do otherwise, but would be cool if you could.)