m/market labResearch, with a record.
Source on GitHub ↗

reader.cpp

1#include "itch/feed/binaryfile/reader.hpp"2#include <stdexcept>3#include <string>45namespace itch {6std::optional<Bytes> read_binary_frame(std::istream& input, uint64_t& frames, uint64_t& byte_offset,7                                       bool& last_end_messages, bool allow_implicit_eof) {8    unsigned char prefix[2];9    input.read(reinterpret_cast<char*>(prefix), 2);10    if (input.gcount() == 0 && input.eof() && allow_implicit_eof && last_end_messages) {11        return std::nullopt;12    }13    if (input.gcount() == 0 && input.eof()) {14        throw std::runtime_error("missing BinaryFILE end marker after frame " +15                                 std::to_string(frames) + " (incomplete session)");16    }17    if (input.gcount() != 2) {18        throw std::runtime_error("truncated frame length after frame " + std::to_string(frames));19    }20    const size_t length = (static_cast<size_t>(prefix[0]) << 8) | prefix[1];21    if (length == 0) {22        if (input.peek() != std::char_traits<char>::eof()) {23            throw std::runtime_error("bytes after BinaryFILE end marker after frame " +24                                     std::to_string(frames));25        }26        if (input.bad()) {27            throw std::runtime_error("BinaryFILE input read failed");28        }29        return std::nullopt;30    }31    ++frames;32    Bytes bytes(length);33    input.read(reinterpret_cast<char*>(bytes.data()), static_cast<std::streamsize>(length));34    if (input.gcount() != static_cast<std::streamsize>(length)) {35        throw std::runtime_error("frame " + std::to_string(frames) + ": truncated payload");36    }37    byte_offset += 2 + length;38    last_end_messages =39        bytes.size() == 12 && bytes[0] == 'S' && bytes[1] == 0 && bytes[2] == 0 && bytes[11] == 'C';40    return bytes;41}42} // namespace itch