0

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?

Jan B.
  • 77
  • 1
  • 7
  • There is no built-in support for timeouts in stream buffers. Have you looked at boost::asio streams? There are fundamentl differe nces between file I/O and device I/O. – Michaël Roy Nov 22 '21 at 03:12
  • I have not, but it seems like stream buffers are designed for device I/O though according to the docs though: https://en.cppreference.com/w/cpp/io/basic_streambuf ("This may be an entity that is accessed through OS API (file, TCP socket, serial port, other character device)") – Jan B. Nov 22 '21 at 08:43
  • Well. That's what they say, but the lack of support for timeout nor other common I/O errors is flagrant. – Michaël Roy Nov 22 '21 at 08:59

0 Answers0