How to combine two boost :: asio :: streambuf's?

I am using boost::asio as a network structure. As a read / write environment, boost::asio::streambuf . I want to:

  • read the message in one buffer
  • add a second buffer at the beginning of the first
  • send a new compound message

What are the possible efficient (zero copies) for this?

+4
source share
2 answers

The principle is called scatter/gather IO . In principle, a way to transfer several buffers at once (in order), without expensive memory copying. It is well supported in boost :: asio with a very flexible and powerful (but also hard to understand) buffer concept and buffer sequence concept.

A simple (untested, but I think is correct) example to get you started would be:

 std::vector<boost::asio::streambuf::const_buffers_type> buffers; buffers.push_back( my_first_streambuf.data() ); buffers.push_back( my_second_streambuf.data() ); std::size_t length = boost::asio::write( mySocket, buffers ); 

Of course, one simple option would be to just read both posts in streambuf and just send the whole streambuf, but from your question, I assume this is not an option for some reason?

+5
source

The free asio::buffer function can accept a collection of buffers (for example this ), the resulting buffer is not a copy of the buffers, but the sequence will be sent.

0
source

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


All Articles