#include #include #include #include #include #include #include namespace ls::aries { constexpr static auto MAX_PAYLOAD_SIZE_IN_MB = 1; constexpr static auto MAX_PAYLOAD_SIZE_IN_BYTES = MAX_PAYLOAD_SIZE_IN_MB * (1024 * 1024); /// Raw read aries massage. struct RawAriesMessage { AriesMessageHeader header; std::vector tagPayload; }; namespace errors { struct TagPayloadTooLarge : std::exception { TagPayloadTooLarge(u32 size) : payloadSize(size) { whatStr = std::format("Tag payload over {} MB (Max is {}MB).", (static_cast(payloadSize) / 1024 / 1024), MAX_PAYLOAD_SIZE_IN_MB); } const char* what() const noexcept override { return whatStr.c_str(); } private: u32 payloadSize; std::string whatStr; }; } // namespace errors /// Reads an Aries message from an Boost.Asio async read stream. template base::Awaitable AsyncReadAriesMessage(AsyncReadStream& stream) { RawAriesMessage res; // Read the header first co_await asio::async_read(stream, asio::buffer(&res.header, sizeof(res.header)), asio::deferred); auto realPayloadSize = res.header.messageSize - sizeof(res.header); // Read tag payload (if there is one) if(res.header.messageSize != sizeof(res.header)) { // Sanity check. I don't expect game payloads to ever reach this large, but who knows. if(realPayloadSize > MAX_PAYLOAD_SIZE_IN_BYTES) throw errors::TagPayloadTooLarge(realPayloadSize); res.tagPayload.resize(realPayloadSize); co_await asio::async_read(stream, asio::buffer(res.tagPayload), asio::deferred); } co_return res; } } // namespace ls::aries