So, StreamFdMessageReader was designed to read one message from an FD. It doesn't really have any way to say "try to read a message, but let me know if it's EOF".
Things you could do instead:
- Use poll() to check for POLLHUP before constructing StreamFdMessageReader. If POLLHUP is indicated, use ioctl FIONREAD to check if there are any bytes waiting to be read. If there are not, then you're at EOF, so don't create a StreamFdMessageReader at all.
- Wrap the FD in an kj::FdInputStream, and wrap that in kj::BufferedInputStreamWrapper. That lets you call `tryGetReadBuffer()` which will return null on EOF. `tryGetReadBuffer` doesn't actually advance the read pointer, it "peeks" at the next bytes in the stream, so if it returns a non-empty array, you can then use InputStreamMessageReader parse the message.
- Use KJ's async I/O, in which case you can use capnp::tryReadMessage() from capnp/serialize-async.h. This attempts to read a message but returns null on EOF.
Admittedly, these are all work-arounds for a missing API. We really should have a way to use StreamFdMessageReader but have it tell you if the stream was EOF...
-Kenton