I have a serial interface that I intend to handle with a C++ stream buffer, but I am not sure if this is really the right tool for that task. I am looking at this example specifically: How to create stream which handles both input and output in C++?
If I understand correctly, I am supposed to poll new data in the underflow
function. I am wondering how to handle the case where I request data, but no data is available. The behavior I expect is that underflow
is called every time I try to read data (e.g. through read
) and no data is available.
Technically, I have to return EOF in underflow
, but this means that it would never be called again, and if data is available at a later point, it would never be polled. I guess alternatively, I could just block in underflow
until data is available, but I would prefer to not block the whole execution and make use of indicating this to the caller.
Essentially, my question is, how to handle such infinite datastreams where the stream can be interrupted by EOF properly? Is there a way to get this to work using stream buffers?