/* otp/pdu.h Copyright (c) 2021 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 "acn/pdu-stream.h" #include namespace OTP { namespace PDU { using ACN::PDU::Stream; using ACN::PDU::pdu_stream_object; /** * @brief The pdu_data struct */ struct pdu_data : public pdu_stream_object {}; class pdu; using Pdu = std::shared_ptr; template struct Block : public pdu_data { Block() { static_assert(std::is_base_of::value, "type parameter of ACN::PDU::Block must derive from OTP::PDU::Pdu"); } std::shared_ptr>> members = std::shared_ptr>> (new std::vector>); void setParent(Pdu parent) { for (auto &p : *members) p->parent = parent; } size_t streamSize() const override { size_t s = 0; for (auto &p : *members) s += p->streamSize(); return s; } void iStream(Stream s) override { 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; members->push_back(p); // add to block } } void oStream(Stream s) const override { for ( const auto& p : *members ) p->oStream(s); }; }; /** * @brief The OTP::PDU::Pdu class */ class pdu : public std::enable_shared_from_this , public pdu_data { public: pdu(); ~pdu(); pdu_data* data = nullptr; uint16_t vector = 0; Pdu parent; Stream stream() { return stream_; } virtual size_t streamSize() const override; virtual void iStream(Stream) override; virtual void oStream(Stream) const override; template void createData() { data = new T(); if (stream_ && stream_->good()) data->iStream(stream_); } template void createDataBlock() { auto block = new PDU::Block(); if (stream_) block->iStream(stream_); block->setParent(shared_from_this()); data = block; } private: Stream stream_; }; } // namespace PDU } // namespace OTP