简体   繁体   English

Boost.Asio的async_read_some消息是否完整?

[英]Will Boost.Asio's async_read_some the whole message?

I am trying to receive a package via http in my C++ program. 我试图在我的C ++程序中通过http接收一个软件包。 I am using Boost.Asio for that purpose. 我正在为此使用Boost.Asio。

In my handleAccept method I have this code to read the package: 在我的handleAccept方法中,我有以下代码来读取程序包:

    // m_buffer is declared as: std::array<char, 8192> m_buffer;
    auto buf = boost::asio::buffer(m_buffer);
    newConnection->getSocket().async_read_some(
        buf,
        boost::bind(&TcpServer::handleRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)
    );

Is this enough to get any package? 这足以获得任何包裹吗? How do I make sure that it will get packages that are bigger than 8192 bytes? 如何确保它将获得大于8192字节的软件包?

async_read_some reads a received message up to the size of the buffer that you give it. async_read_some读取接收到的消息, 直到达到您提供给它的缓冲区的大小。 The number of bytes in a received message is returned in the bytes_transferred placeholder of the callback function, see basic_stream_socket::async_read_some 在回调函数的bytes_transferred占位符中返回接收到的消息中的字节数,请参阅basic_stream_socket :: async_read_some

If you know the maximum size of the messages that you expect to receive, you can simply use a buffer that is large enough for that message. 如果知道您希望接收的最大消息大小,则可以使用足够大的缓冲区来容纳该消息。 Otherwise, you'll have to copy the contents of the async_read_some buffer elsewhere and build up the whole received message there. 否则,您将不得不将async_read_some缓冲区的内容复制到其他位置,并在此构建整个接收到的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM