I use boost::asio as a network framework. As a read/write medium it uses boost::asio::streambuf. I want to:
- read some message in one buffer
- append second buffer at the beginning of the first one
- send new composite message
What are the possible efficient (zero-copy) options to do this?
The principle is called
scatter/gather IO. Basically a way of transmitting multiple buffers at once (in order), without expensive memory-copying. It is well supported under boost::asio with the very flexible and powerful, (but also difficult to grasp) buffers-concept, and buffer-sequence concept.A simple (untested, but I believe correct) example to get you started, would be:
Of course, one simple option would be to just read both messages into the streambuf and simply sending the entire streambuf, but from your question I’m guessing it’s not an option for some reason?