So much buffers!


I think I just grew a whole mess of buffers in your head by this point. But don't panic! Everything's under control. Really.

Let's summarize everything we've learned about buffers so far.

All Boost.Asio I/O operations work on some data. That data is stored somewhere.

To pass a data into I/O operations we use buffer views. Buffer view is a non-owning pair of a pointer and a size. So, underlying memory block should be valid as long as the buffer view is in use. Use boost::asio::buffer function to create a buffer view. It has overloads for arrays, several STL and Boost containers as well as for raw pointer and size.

There are two types of buffer views: const and mutable. We use const buffer view to read a data from it and mutable buffer view to write a data into it.

Buffer views can be chained into a buffer sequence. There are different Boost.Asio I/O functions which operate on buffer sequences. It is convenient to use buffer sequence when you need to send (or receive) some data scatterend around as a single data packet. See “Buffer sequence” lesson.

There are several Boost.Asio free functions provided to operate on buffer views and buffer sequences. You can read and write data, iterate over the data content, calculate its size or copy its content. See “Buffer sequence, part 2”.

There is DynamicBuffer concept which gives you higher level abstraction over the memory buffers. It consists of input and output sequences and provides data committing and consumption. Dynamic buffer like a buffer view doesn't own the underlying container. Use boost::asio::dynamic_buffer function to create a dynamic buffer from std::vector or std::string. See “Dynamic buffers” and “Dynamic buffers, part 2” lessons.

boost::asio::streambuf is STL-compatible streambuf which is provided by Boost.Asio and answers DynamicBuffer requirement. Also, it does own the underlying memory buffer.

Now you know almost everything you need about Boost.Asio buffers. In the next lesson we will learn how to read and write data in details.

Share this page:

Learning plan

How to operate on the underlying buffer sequence data with Boost.Asio free functions
How to read data from Boost.Asio dynamic buffers
How to work with Boost.Asio dynamic buffers manually
24. So much buffers!
Let's briefly summarize everything we've learned about different Boost.Asio buffers
How to deal with read and write functions properly to gain desired I/O behavior
How to deal with Boost.Asio I/O free functions: async_read, async_read_until and async_write
Several additional tips on dealing with Boost.Asio I/O free functions