Will the boost::asio::streambuf keep in sequency order when ostream write to boost::asio::streambuf multiple time?
Hi, Are there any problems in following statements? The receiver works a few minutes, then got wrong number in header field. std::shared_ptrboost::asio::streambuf buffer = std::make_sharedboost::asio::streambuf(); std::ostream os(buffer.get()); os.write((char *) &sendHeader, sizeof(sendHeader)); os.write((char *) sb.data(), sb.size()); boost::asio::async_write(socket_, *buffer.get(), .....); Thank you. - JHH
On 8/12/2018 19:59, hh h wrote:
Are there any problems in following statements? The receiver works a few minutes, then got wrong number in header field.
std::shared_ptrboost::asio::streambuf buffer = std::make_sharedboost::asio::streambuf(); std::ostream os(buffer.get()); os.write((char *) &sendHeader, sizeof(sendHeader)); os.write((char *) sb.data(), sb.size()); boost::asio::async_write(socket_, *buffer.get(), .....);
There is nothing inherently wrong in that code (at least as long as you ensure that you do not have more than one async_write in flight at any given moment -- make sure that the callback has been called before you start another async_write). But as noted in your other thread, while for TCP/IP streams the receiver will receive all the bytes in the same order that they were sent, they may not be received in the same chunks. As such your wire protocol must have some way to detect message boundaries, and (depending on the method used to do that) in many cases you may need to tolerate receiving part of the next message at the same time as the end of a previous message (which mostly means being careful to reuse your receive streambuf rather than discarding it).
participants (2)
-
Gavin Lambert
-
hh h