Read data into a circular buffer
Asked Answered
V

1

8

Is it possible to use boost::circular_buffer with boost::asio?

Specifically I want to read a fixed number of bytes with boost::asio::async_write and store them directly in the circular buffer without copying.

Some example code would be very nice!

Vassaux answered 8/11, 2013 at 13:6 Comment(6)
yes, you can. Some example code would be nice.Sapsucker
Look at following members of circular_buffer: array_one, array_two, rotate and linearize. You can use array_one() and array_two() to get internal buffers(slices of one big buffer) and feed boost::asio::buffer with them.Siliceous
Thanks @EvgenyPanasyuk for these hints. I will try on monday if I can get it to work with a mutable buffer sequence consisting of array_one and array_two.Vassaux
Doesn't look like there's an acceptable answer for this one.Mezereon
Hi @RobertHegner did you ever get this figured outPender
You sure you want to read using async_write?Darr
D
2

As of right now (Boost 1.66), it is not possible to read data into boost::circular_buffer because it doesn't expose any way to reserve space in the underlying buffer, which is a requirement for creating a mutable_buffer needed to call asio::read.

But it is possible to write from 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);
Darr answered 31/12, 2017 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.