boost::asio::async_read 和 boost::asio::streambuf

发布于 2024-09-24 15:16:26 字数 110 浏览 1 评论 0原文

我将 async_read 与 Streambuf 一起使用。但是,我想将读取的数据量限制为 4,这样我就可以在进入正文之前正确处理标头。

我该如何使用 async_read 来做到这一点?

I am using async_read with streambuf. However, I would like to limit the amount of data read to 4, so I can properly handle header before going to body.

How can I do that using async_read?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过气美图社 2024-10-01 15:16:26

使用两个 async_read 操作,其中第一个读取 4 字节标头,第二个读取消息正文。第一个 async_read 的处理程序应启动消息正文的 async_read

asio 示例在几个地方使用了这种技术,序列化示例就是其中之一。我还回答了类似的问题,尽管它使用同步读取,但概念是相同的。

Use two async_read operations where the first reads a 4 byte header, and the second reads the message body. Your handler to the first async_read should start the async_read for the message body.

The asio examples use this technique in a couple of places, the serialization example is one. I also answered a similar question, though it uses synchronous reads, but the concept is the same.

任谁 2024-10-01 15:16:26

您可以使用 transfer_at_least< 保证标头可用/a> as async_read 的完成条件

一旦处理了初始标头,就可以处理任何多余的正文数据(或其他标头)。

You can guarantee the header is available using transfer_at_least as CompletionCondition on async_read.

Any superfluous body data (or further headers) can be processed once you handle the initial header.

爱*していゐ 2024-10-01 15:16:26
boost::asio::transfer_exactly(streambuf.size()) 

就是你所需要的。尝试像这样使用:

boost::asio::async_read(socket_, 
                        buf,boost::asio::transfer_exactly(size_),
                        boost::bind(callback,
                        boost::asio::placeholders::error));
boost::asio::transfer_exactly(streambuf.size()) 

is what you need.just try using like this:

boost::asio::async_read(socket_, 
                        buf,boost::asio::transfer_exactly(size_),
                        boost::bind(callback,
                        boost::asio::placeholders::error));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文