I use boost::asio
as a network framework. As a read/write medium it uses boost::asio::streambuf
. I want to:
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:
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 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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With