How copy or reuse boost::asio::streambuf?

丶灬走出姿态 提交于 2019-12-05 14:24:44
Tanner Sansbury

One can use boost::asio::buffer_copy() to copy the contents of Asio buffers. This can be convenient if, for example, one wishes to copy the contents of one streambuf to another streambuf.

boost::asio::streambuf source, target;
...
std::size_t bytes_copied = buffer_copy(
  target.prepare(source.size()), // target's output sequence
  source.data());                // source's input sequence
// Explicitly move target's output sequence to its input sequence.
target.commit(bytes_copied);

A similar approach can be used to copy from a streambuf into any type for which Asio supports mutable buffers. For example, copying content into a std::vector<char>:

boost::asio::streambuf source;
...
std::vector<char> target(source.size());
buffer_copy(boost::asio::buffer(target), source.data());

One notable exception is that Asio does not support returning a mutable buffer for std::string. However, one can still accomplish copying into std::string via iterators:

boost::asio::streambuf source;
...
std::string target{
  buffers_begin(source.data()),
  buffers_end(source.data())
};

Here is an example demonstrating copying contents from boost::asio::streambuf into various other types:

#include <iostream>
#include <string>
#include <vector>
#include <boost/asio.hpp>

int main()
{
  const std::string expected = "This is a demo";

  // Populate source's input sequence.
  boost::asio::streambuf source;
  std::ostream ostream(&source);
  ostream << expected;

  // streambuf example
  {
    boost::asio::streambuf target;
    target.commit(buffer_copy(
      target.prepare(source.size()), // target's output sequence
      source.data()));               // source's input sequence

    // Verify the contents are equal.
    assert(std::equal(
      buffers_begin(target.data()),
      buffers_end(target.data()),
      begin(expected)
    ));
  }

  // std::vector example
  {
    std::vector<char> target(source.size());
    buffer_copy(boost::asio::buffer(target), source.data());

    // Verify the contents are equal.
    assert(std::equal(begin(target), end(target), begin(expected)));
  }

  // std::string example
  {
    // Copy directly into std::string.  Asio does not support
    // returning a mutable buffer for std::string.
    std::string target{
      buffers_begin(source.data()),
      buffers_end(source.data())
    };

    // Verify the contents are equal.
    assert(std::equal(begin(target), end(target), begin(expected)));
  }
}

First, you cannot copy streambuf - it has deleted copy constructor.

I should suggest using your own buffer using asio::buffer and parse it. But with this you will not be able to use async_read_until. Also, you can do consuming-copy into your own buffer, and then send it if needed.

Lifehack: you can get the data without consuming it using buffer_cast, but note its not safe way since its not documented and may broke (but it worked for me many times and many boost upgrades):

// req_buf is a streambuf
const char *req_data = boost::asio::buffer_cast<const char *>( req_buf.data() );
auto sz = req_buf.size();

Note again: this cast is working because streambuf implementing some concepts, but its not documented directly. Also, remember req_data can be invalidated after any req_buf modify.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!