Reading data into a circular buffer

Can i use boost::circular_bufferwith boost::asio?

In particular, I want to read a fixed number of bytes with boost::asio::async_writeand store them directly in a ring buffer without copying.

Some sample code would be very nice!

+4
source share
1 answer

As it is now (Boost 1.66) it is impossible to read the data in boost::circular_buffer, because it does not provide any opportunity to reserve a place in the base buffer, which is a requirement for the creation mutable_buffernecessary for the call asio::read.

boost::circular_buffer:

  boost::circular_buffer<char> cir_buf;

  FillBuffer(cir_buf);

  // Construct a buffer sequence with either 1 or 2 data chunks
  std::vector<boost::asio::const_buffer> buffer_sequence;

  auto arr1 = cir_buf.array_one();
  buffer_sequence.push_back(boost::asio::buffer(arr1.first, arr1.second));

  auto arr2 = cir_buf.array_two();
  if (arr2.second != 0) {
    buffer_sequence.push_back(boost::asio::buffer(arr2.first, arr2.second));
  }

  boost::asio::write(socket_, buffer_sequence);
0

All Articles