I'm using asio::async_read_until
with '\n'
delimiter to support a TCP client that fetches character data from a server.
This server continuously sends '\n'
terminated lines; precisely, it can write at once either single lines or a concatenated string of multiple lines.
From the doc, I understand that asio::async_read_until
could read:
- One
'\n'
terminated line, like"some_data\n"
. This is the simplest case, handled with a call thestd::getline
on the stream associated with theasio::streambuf
- One
'\n'
terminated line plus the beginning of a next line, like"some_data1\nbla"
. This can be handled with astd::getline
; the rest of the second line will be handled at the next completion handler call. - Many lines; in this case, the newly read data could contain 2 or more
'\n'
. How can I know how manystd::getline
calls I should do, knowing that I don't want to risk callingstd::getline
on an incomplete line (which I will eventually get in a future packet)? Should I peek at the stream buffer to check the existence of multiple'\n'
? Is it even possible without doing many copies?