I'm implementing a very simple protocol using Boost Asio. I send a simple query and I get back a variable-length response. The async send appears to work, and the write handler is called. Since I don't know how long the response is, I start by reading the fixed 8 byte header. This will always be present, and it contains the length of the remainder. Relevant call:
char input[256]; // Large enough to also hold the variable part.
async_read(socket, buffer(input), transfer_at_least(8), callback);
In callback
, I get a boost::system::error_code
which says "End of file". Sure enough, the socket is no longer open. But it's a TCP socket. What is the point of failing at end-of-input and closing the socket? More input will arrive. I know the remote side doesn't close the socket, that's production code which is known to implement this protocol correctly.
The whole reason for the async read is of course to not block waiting for the response. So how do I get a callback after the first 8 bytes, without Asio closing the socket on me?
Existing questions are similar but different: either they don't use async reads, or they don't do fixed-size reads, or they have issues only after the first read, or they have issues with io_service::run
(I don't, it returns as expected after the EOF error happens)
std::shared_ptr
but somewhere between boost::asio and std::shared_ptr the use count is corrupted. Weird.) – MSalters