Speed ​​up the use of streambuf and accomplish what it is

I cannot find a good explanation of what consumes () and commit () really means, in fact, I don’t understand streambuf at all.

I understand that streambuf is just an array of characters. But why is this in the documentation,

basic_streambuf::data Get a list of buffers that represents the input sequence. 

so there really are many buffers? And what is an "input sequence" and an "output sequence"? Are these two more buffers?

What does the following code do?

 streambuf b; size_t size; size = read( socket, b.prepare( 1024 ) ); b.commit( size ); size = write( socket, b.data() ); b.consume( size ); 

when i call b.prepare () does it allocate a new read buffer () to hold the data? Then, when data is transferred from this buffer to the streambuf base buffer? I thought it was commit (), but

 basic_streambuf::commit Move characters from the output sequence to the input sequence. 

therefore, it seems that commit actually moves data from the "output sequence" to the "input sequence" without mentioning the underlying buffer used to store the data!

+6
source share
1 answer

Boost ASIO streambuf is more than just an array of characters. From the basic_streambuf documentation:

The basic_streambuf class is derived from std :: streambuf for associating streambuf input and output sequences with one or more character arrays. These character arrays are internal to the basic_streambuf object, but provide direct access to the array elements so that they can be used effectively with I / O operations. Characters written to the output sequence of the basic_streambuf object are added to the input sequence of the same object.

The documentation refers to possible implementation strategies. A streambuf object can simply use a single contiguous array of characters with pointers to control input and output sequences. But the interface allows the use of more complex schemes.

You asked what the code snippet actually does, but it depends on the underlying implementation. In short, prepare () ensures that the main buffer is large enough to hold what you are trying to insert into it. It also gives you buffer access through the mutuable_buffers object. After the data has been written to streambuf (presumably when the read handler is called), commit () makes that data available to the input sequence. You can access these bytes with data (). After you finish with the data in the buffer (because you copied it, processed it, or something else), consumume () removes the data from the input sequence. A subsequent data call () will not contain the bytes of the previous call.

You also argued that the underlying buffer used to store data was never mentioned, and this is true. ASIO authors can decide how to store the actual data.

+6
source

Source: https://habr.com/ru/post/975955/


All Articles