.. _program_listing_file_eshet_unpack.hpp: Program Listing for File unpack.hpp =================================== |exhale_lsh| :ref:`Return to documentation for file ` (``eshet/unpack.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include "parse.hpp" #include #include namespace eshet { namespace detail { // accept a stream of data in arbitrary chunks, and produce complete messages class Unpacker { std::vector buffer; public: void push(std::vector buf) { for (uint8_t chr : buf) buffer.push_back(chr); } std::optional> read() { if (buffer.size() < 3) return std::nullopt; Parser p(buffer.data(), 3); uint8_t magic = p.read8(); uint16_t length = p.read16(); p.check_empty(); if (magic != 0x47) throw ProtocolError(); if (length > (buffer.size() - 3)) return std::nullopt; std::vector message(buffer.data() + 3, buffer.data() + 3 + length); for (size_t i = length + 3; i < buffer.size(); i++) buffer[i - (length + 3)] = buffer[i]; buffer.resize(buffer.size() - (length + 3)); return message; } }; } // namespace detail } // namespace eshet