/* 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 : pdu_stream_object {}; class pdu; using Pdu = std::shared_ptr; /** * @brief The PDU::Block struct */ 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"); } /** * @brief Messages contained in the block */ std::shared_ptr>> members = std::make_shared>>(); /** * @brief setParent * @param parent */ void setParent(Pdu parent) { for (auto &p : *members) p->parent = parent; } /** * @brief streamSize * @return */ size_t streamSize() const override { size_t s = 0; for (auto &p : *members) s += p->streamSize(); return s; } /** * @brief iStream * @param 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 } } /** * @brief oStream * @param s */ 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; //!< data Message uint16_t vector = 0; //!< vector Pdu parent; //!< parent Message Stream stream(); virtual size_t streamSize() const override; virtual void iStream(Stream) override; virtual void oStream(Stream) const override; /** * @brief createData */ template void createData() { data = new T(); if (stream_ && stream_->good()) data->iStream(stream_); } protected: Stream stream_; //!< buffer }; } // namespace PDU } // namespace OTP