Subject: Re: [boost] Will the boost::asio::streambuf keep in sequency order when ostream write to boost::asio::streambuf multiple time?
From: Gavin Lambert (boost_at_[hidden])
Date: 2018-12-10 00:16:59
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_ptr<boost::asio::streambuf> buffer =
> 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).
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk