How to determine with mio::net::TcpStream
that all data has been read?
I understand that that I have to register the mio::net::TcpStream
with my Poll
and then poll()
for "read" events. Whenever a "read" event is received, I have to call TcpStream:read()
in a loop until it fails with an WouldBlock
error. Then, once again, I can poll()
for more "read" events...
Now here is the thing: How do I know when all data has been read? Specifically, how do I know that the peer has transmitted all data, e.g. the HTTP request data has been received completely?
The problem is: After loop'ing the read()
until it failed with WouldBlock
error, I do not know whether the last read has failed because the peer is actually finished sending data, or because we have read all data that was currently available in the receive buffers and more data may become available later...
In other words: If, after the WouldBlock
error, I go back to poll()
'ing, then I don't know whether more "read" events are going to pop up, or whether poll()
waits forever as there won't be any more events.
Thanks for any suggestions!
I know that I can add a timeout to poll()
call. But it is unclear which timeout value would be big enough to be sure that we are not missing any input data that may arrive "delayed". Furthermore, this adds an unnecessary delay in the case that the data is already complete when we enter poll()
Also, I have looked at some mio
example code. They seem to expect that there will be a successful zero size read()
to indicate the end of the data. But this not the case in the real world, as I have tested!
In my test, after I get the very first "read" event for my TcpStream
, I can do a single successful read()
, which returns a size of (for example) 78 bytes. The next read()
then immediately fails with a WouldBlock
error. If, at this point, I go back to poll()
'ing, then no further events are ever received for the stream...
Never ever was there a read()
that succeeded with size zero.
Meanwhile, assuming that the first successful read()
after the first "read" event already gave us all the data, may be wrong in the general case. So I'm a bit lost how this is supposed to be solved