|
1 | 1 | #pragma once |
2 | 2 |
|
| 3 | +#include <chrono> // milliseconds |
3 | 4 | #include <cstddef> // byte, size_t |
| 5 | +#include <cstdint> // uint8_t |
4 | 6 |
|
5 | 7 | namespace databento { |
6 | 8 | // An abstract class for readable objects to allow for runtime polymorphism |
7 | 9 | // around DBN decoding. |
8 | 10 | class IReadable { |
9 | 11 | public: |
| 12 | + enum class Status : std::uint8_t { |
| 13 | + Ok, // Data read successfully |
| 14 | + Timeout, // Timeout reached before any data available |
| 15 | + Closed, // Stream is closed/EOF |
| 16 | + }; |
| 17 | + |
| 18 | + struct Result { |
| 19 | + std::size_t read_size; // Number of bytes read |
| 20 | + Status status; // Status of the read operation |
| 21 | + }; |
| 22 | + |
10 | 23 | virtual ~IReadable() = default; |
11 | 24 |
|
12 | 25 | // Read exactly `length` bytes into `buffer`. |
13 | 26 | virtual void ReadExact(std::byte* buffer, std::size_t length) = 0; |
14 | | - // Read at most `length` bytes. Returns the number of bytes read. Will only |
15 | | - // return 0 if the end of the stream is reached. |
| 27 | + // Read at most `length` bytes. Returns the number of bytes read. Will only return 0 |
| 28 | + // if the end of the stream is reached. |
16 | 29 | virtual std::size_t ReadSome(std::byte* buffer, std::size_t max_length) = 0; |
| 30 | + // Read at most `max_length` bytes with timeout support. Returns Result with bytes |
| 31 | + // read and status. Status will be Timeout if no data available within timeout period, |
| 32 | + // Closed if stream is closed, or Ok if data was read. A timeout of 0 means wait |
| 33 | + // indefinitely (same as the no-timeout overload). |
| 34 | + virtual Result ReadSome(std::byte* buffer, std::size_t max_length, |
| 35 | + std::chrono::milliseconds timeout) = 0; |
17 | 36 | }; |
18 | 37 | } // namespace databento |
0 commit comments