How to clean boost::asio::streambuf before writing to it?
Sorry, this is really a silly question, but I could not find a clear API in streambuf reference, 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()); ....... } Thank you. Kind regards, - jh -- "A man can fail many times, but he isn't a failure until he begins to blame somebody else." -- John Burroughs
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...
Thank you very much Gavin.
On 6/10/20, Gavin Lambert via Boost
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...
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- "A man can fail many times, but he isn't a failure until he begins to blame somebody else." -- John Burroughs
Op 10-06-2020 om 10:57 schreef Jupiter via Boost:
Thank you very much Gavin.
Be aware of this edge-case in some versions fo boost: https://stackoverflow.com/a/47018260/85371
On 11/06/2020 03:18, Seth wrote:
Be aware of this edge-case in some versions fo boost: https://stackoverflow.com/a/47018260/85371
That's not really a bug, that's a case of "garbage in, garbage out".
participants (3)
-
Gavin Lambert
-
Jupiter
-
Seth