Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand the boost::beast multibuffer

The Beast websocket example stores the data in a multibuffer:

The implementation uses a sequence of one or more character arrays of varying sizes. Additional character array objects are appended to the sequence to accommodate changes in the size of the character sequence.

When looking at the interface it is not completely clear to me how it works. If I read the descriptions it can be seen as an array of buffers. But it seems the output is only a single chunk of data. Does this mean the "one or more arrays" are only applicable to the internal structure ?

In the example code the data is read into the buffer as follows: m_websocketStream.async_read(m_buffer.....

Does each async_read operation creates a new internal buffer.

If this is the case, how to interpret it at the other end. E.G. how to read it into a std::string or std::vector.

When looking into the sources data() returns const_buffer_type, which is a forward declaration.

For the data member the help information provides the following info, which is not of much help:

The type used to represent the input sequence as a list of buffers. using const_buffers_type = implementation_defined;

The definition seems to come from the header file boost/asio/buffer.hpp which is included as well. The overall structure however is somewhat obfuscating to me.

I just try to understand how to handle the data as bytes or convert it to as std::string.

Tried the following, but this is also not allowed:

std::string( boost::asio::buffer_cast<const char*>(m_buffer.data()) ,boost::asio::buffer_size(m_buffer.data()) );

Anyone who can enlighten me a little ?

like image 415
Waldorf Avatar asked Dec 06 '22 13:12

Waldorf


1 Answers

In the latest versions of Beast, there is now the function buffers_to_string which will do this for you in a single function call.

like image 53
Vinnie Falco Avatar answered Jan 02 '23 09:01

Vinnie Falco