/* pdu.h Copyright (c) 2020 Kevin Matz (kevin.matz@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #pragma once #include #include #include #include "pdu-stream.h" namespace ACN { namespace PDU { /** * @brief 2.4.1. Flags * * Flags is a 4-bit field containing flags L, V, H and D which declare how * the PDU is packed. */ struct pdu_flags { bool hasLength : 1; bool hasVector : 1; bool hasHeader : 1; bool hasData : 1; pdu_flags() {}; pdu_flags(uint8_t); }; /** * @brief The pdu_base_member struct */ struct pdu_base_member { virtual ~pdu_base_member() {}; virtual size_t streamSize() = 0; virtual void iStream(Stream) = 0; virtual void oStream(Stream) const = 0; }; /** * @brief The pdu_header struct */ struct pdu_header : public pdu_base_member {}; /** * @brief The pdu_data struct */ struct pdu_data : public pdu_base_member {}; /** * @brief The Pdu class * * All PDU share common structure of: * flags, length, vector, * and protocol specific header/data. * * Flag values indicate if lenght, vector, header or data * are present in the PDU, or if they should be inherited from the * preceding PDU. */ class Pdu : public pdu_base_member { public: Pdu(size_t vector_size); ~Pdu(); // getters const pdu_flags flags() {return flags_;} const uint32_t length() {return length_;} const uint32_t vector(); // may inherit pdu_header * header(); // may inherit pdu_data * data(); // may inherit std::shared_ptr parent() {return parent_;} Stream stream() {return stream_;} virtual size_t streamSize() override; virtual void iStream(Stream) override; virtual void oStream(Stream) const override; // setters void setParent (std::shared_ptr pdu) {parent_ = pdu;} void setInherit(std::shared_ptr pdu) {inherit_ = pdu;} protected: pdu_flags flags_; uint32_t length_; uint32_t vector_; size_t vector_size_; std::shared_ptr parent_ = nullptr; std::shared_ptr inherit_ = nullptr; pdu_header * header_ = nullptr; pdu_data * data_ = nullptr; Stream stream_; }; /** * @brief Callback that understands how to proccess a PDU type. * @tparam T PDU decendant subclass */ template using Handler = std::function)>; /** * @brief PDU::pdu_data subclass that encapsulates other PDU. * @tparam T PDU decendant subclass */ template struct Block : public pdu_data { std::shared_ptr>> pdu = std::shared_ptr>>( new std::vector>); size_t streamSize() { size_t s = 0; for (auto &child : *pdu) s += child->streamSize(); return s; } void iStream(Stream s) { //!< you should call readBlock() directly. readBlock(s, nullptr); }; void oStream(Stream s) const { for ( const auto & child : *pdu ) child->oStream(s); }; void readBlock(Stream s, std::shared_ptr parent = nullptr) { while(s->good()) { std::shared_ptr p(new T()); p->iStream(s); if (s->fail()) // stream failed during pdu read break; if (p->stream()->fail()) // pdu buffer failed continue; if (parent) // set parent p->setParent(parent); if (!pdu->empty()) // set inheritee p->setInherit(pdu->back()); pdu->push_back(p); // add to block } } }; } // PDU } // ACN