Reputation: 18503
I use a small buffer (e.g. 128 bytes) and I want to use "async_read_until" with big incoming messages on the TCP connection (discarding all but last 128 bytes prior to the delimiter).
How can this be done? The ASIO docs are not very clear what happens when the provided buffer is not big enough.
Here is my code for the read initiazation
typedef boost::shared_ptr<boost::asio::streambuf >streambuf_ptr;
streambuf_ptr inBuf(new boost::asio::streambuf (128));
boost::asio::async_read_until(*sock, *inBuf, "\r\n\r\n", boost::bind(my_read_handler, sock, inBuf, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
Upvotes: 2
Views: 1826
Reputation: 47478
When the provided buffer is not big enough, async_read_until
fills it in completely and then invokes the read handler with the error code asio::error::not_found
, meaning that the delimiter was not found.
At that point you can .consume()
some (or all) data from the buffer and call async_read_until
again. It may be difficult to guarantee, with a 128-byte buffer, that when the delimiter is finally found, it is in the exact last position in the buffer (and even then, with a four-byte delimiter, you will only have the last 124 bytes prior to it). It may be best to use a larger buffer and buffer.consume(buffer.size()-128)
in the not_found
error handler, to make sure there's at least 128 bytes free at all time.
Upvotes: 5