On 10/06/2020 18:12, Jupiter wrote:
Sorry, this is really a silly question, but I could not find a clear API in streambuf reference,
https://www.boost.org/doc/libs/1_73_0/doc/html/boost_asio/reference/streambu...
I have following code to call PacketData in a loop, the buffer.get() looked to accumulate previous data in the buffer, so I should call buffer.clear() before calling buffer.get(), but apparently there no ‘clear’ in boost::asio::basic_streambuf.
PacketData(const msgpack::sbuffer &sb, boost::asio::streambuf &buffer)) { ....... std::ostream os(buffer.get()); os.write((char *)&header, sizeof(header)); os.write((char *)sb.data(), sb.size()); ....... }
Firstly, make sure you're using separate streambufs for read and write operations -- this is mandatory if you have async reads/writes concurrently pending, but is generally a good idea anyway. commit() [1] and consume() [2] are the methods that move or remove data. Which one you need depends on what you're doing with the buffer. Both of these require the number of characters written/read. As the examples show, for reading from a socket (or other I/O object) you will typically prepare() [3], read, then commit; whereas for writing to a socket you will write, then consume. Note that some of the ASIO methods that accept a streambuf parameter directly will do this for you, so you don't always need to call them yourself. [1]: https://www.boost.org/doc/libs/1_73_0/doc/html/boost_asio/reference/basic_st... [2]: https://www.boost.org/doc/libs/1_73_0/doc/html/boost_asio/reference/basic_st... [3]: https://www.boost.org/doc/libs/1_73_0/doc/html/boost_asio/reference/basic_st...