Re: [Boost-users] Boost.Asio -- combining sync read with async read

Right. The most simple way to accomplish this is to use asio::streambuf as a buffer. When you process the input data, you call consume() function only for the amout you've really processed, and the rest of the data remains unused in the streambuf - until the next time you get something from the server (look at the asio examples with streambuf).
Yes, I'm using the streambuf, and then constructing an istream from the streambuf, and then calling getline on the istream. (The chat client/server was the primary example that I used to build my app.) If I have 2.5 lines in the buffer then the first two calls to getline will give me the first two lines, but won't the third call to getline get me the half line, and consume that half line from the streambuf? Do I need to process the streambuf in a more manual fashion rather than calling getline on the istream? Also to make sure I understand it how it works with the streambuf, that I am understanding you correctly: if I was just processing one line in the async_handler, and the first time the server sends me 3 lines, then the handler is called and will process the first line sent. Then the server sends another 3 lines, the async handler is called and the client will process the 2nd line that it received, not the 4th line, right? Thanks for your help, -Sameer

If I have 2.5 lines in the buffer then the first two calls to getline will give me the first two lines, but won't the third call to getline get me the half line, and consume that half line from the streambuf? Do I need to process the streambuf in a more manual fashion rather than calling getline on the istream?
if I was just processing one line in the async_handler, and the first time
If this is the case in your application (i.e. the server might send few lines in a bunch), then I think yes, you have to inspect the sterambuf manually. Actually, this is the reason I never used read_until - in most cases it doesn't make your code simpler. The one case where it does is when your protocol has a text header and a body (like in HTTP). the server sends me 3 lines, then the handler is called and will process the first line sent. Then the server sends another 3 lines, the async handler is called and the client will process the 2nd line that it received, not the 4th line, right? Right, the data is added to the "get" area of the streambuf, you don't loose anything. But if your server would not send anything more, there would not be any trigger to continue processing the remained data.
participants (2)
-
Igor R
-
Sameer Parekh