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!
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With