I have a general question about using boost asio's async_read_until. The documentation says there might be more data inside the buffer when the handler is called. Is there any way to work around this and stop the buffer from consuming bytes from the socket right after the sequence condition was matched?
Asked
Active
Viewed 534 times
1 Answers
1
Q. Is there any way to work around this
Not directly, because of the way network traffic works (it's packet oriented).
Of course, you might get things on the protocol boundary if the sending party actively ensures it, but that's unusual for stream protocols.
Q. and stop the buffer from consuming bytes from the socket right after the sequence condition was matched?
No, but you can stop consuming the buffer. So, e.g. this is a valid pattern:
boost::asio::streambuf sb;
auto bytes = boost::asio::read_until(socket, sb, "\r\n\r\n");
std::istream is(&sb);
std::string line;
while (getline(is, line) && !line.empty()) {
std::cout << "Received: '" << line << "'\n";
}
// sb still contains un-consumed data, if any
Simply use the same streambuf
for any subsequent reads and it will manage the "stream position" for you.

sehe
- 374,641
- 47
- 450
- 633