108 lines
2.4 KiB
C++
108 lines
2.4 KiB
C++
|
/*
|
||
|
otp/pdu.cpp
|
||
|
|
||
|
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.
|
||
|
*/
|
||
|
#include "pdu.h"
|
||
|
|
||
|
namespace OTP::PDU {
|
||
|
|
||
|
/**
|
||
|
* @brief pdu::pdu
|
||
|
*/
|
||
|
pdu::pdu()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief pdu::~pdu
|
||
|
*/
|
||
|
pdu::~pdu()
|
||
|
{
|
||
|
if (data) delete data;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief pdu::streamSize
|
||
|
* @return
|
||
|
*/
|
||
|
size_t pdu::streamSize() const
|
||
|
{
|
||
|
size_t s = 4; // vector + length
|
||
|
|
||
|
if (data)
|
||
|
s += data->streamSize();
|
||
|
|
||
|
return s;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief pdu::iStream
|
||
|
* @param stream
|
||
|
*/
|
||
|
void pdu::iStream(Stream stream)
|
||
|
{
|
||
|
uint16_t length;
|
||
|
|
||
|
*stream >> vector;
|
||
|
*stream >> length;
|
||
|
|
||
|
// length includes the vector and length.
|
||
|
// the remainder of the length is data
|
||
|
length -= 4;
|
||
|
|
||
|
// abort if the remaining PDU length isn't available
|
||
|
if (!stream->good() || stream->available() < length)
|
||
|
return stream->setstate(std::ios_base::failbit);
|
||
|
|
||
|
// create a stream buffer for the data
|
||
|
stream_ = Stream(new ACN::PDU::pdu_stream(stream->data(), length));
|
||
|
if (stream_->available() != length)
|
||
|
return stream->setstate(std::ios_base::failbit);
|
||
|
|
||
|
// fast-forward the input stream
|
||
|
stream->ignore(length);
|
||
|
|
||
|
if (!stream->available())
|
||
|
return stream->setstate(std::ios_base::eofbit);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief pdu::oStream
|
||
|
* @param stream
|
||
|
*/
|
||
|
void pdu::oStream(Stream stream) const
|
||
|
{
|
||
|
uint16_t length = streamSize();
|
||
|
|
||
|
*stream << vector;
|
||
|
*stream << length;
|
||
|
|
||
|
data->oStream(stream);
|
||
|
}
|
||
|
|
||
|
} // namespace OTP::PDU
|