Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read data into a circular buffer

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!

like image 422
Robert Hegner Avatar asked Nov 08 '13 13:11

Robert Hegner


People also ask

How would you implement a circular buffer?

Circular Buffers can be implemented in two ways, using an array or a linked list. An empty object array along with its capacity is initialized inside the constructor as the type of elements added is unknown. Two pointers namely head and tail are maintained for insertion and deletion of elements.

What is circular buffer data structure?

August 2022) In computer science, a circular buffer, circular queue, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end. This structure lends itself easily to buffering data streams. There were early circular buffer implementations in hardware.

Is circular buffer the same as ring buffer?

A ring buffer (also known as a circular buffer or a circular queue) is a buffer data structure that behaves as if it had a circular shape, in which the last element in the buffer is connected to the first element. Like standard buffers, ring buffers typically have a fixed size.

What is circular buffer in operating system?

A circular buffer is a utility used to transfer successive data values from a producer thread to a consumer thread, who retrieves the data in FIFO (first in first out) order. This kind of data structure will be used when pipelining threads, a subject discussed in detail in Chapter 15.


1 Answers

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);
like image 159
rustyx Avatar answered Sep 22 '22 12:09

rustyx