1
0
Fork 0

move pdu_stream class to it's own .h/.cpp files

This commit is contained in:
Kevin Matz 2021-07-26 21:15:20 -04:00
parent 7e58ce5562
commit c6aef08e55
5 changed files with 228 additions and 154 deletions

View File

@ -15,6 +15,8 @@ set(SOURCE_FILES
acn/component.h
acn/dmp.cpp
acn/dmp.h
acn/pdu-stream.cpp
acn/pdu-stream.h
acn/pdu.cpp
acn/pdu.h
acn/rlp-tcp.cpp

125
acn/pdu-stream.cpp Normal file
View File

@ -0,0 +1,125 @@
/*
pdu-stream.cpp
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.
*/
#include "pdu-stream.h"
namespace ACN {
namespace PDU {
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint8_t& val)
{
val = readType<uint8_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint16_t& val)
{
val = readType<uint16_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint32_t& val)
{
val = readType<uint32_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint64_t& val)
{
val = readType<uint64_t>();
return *this;
};
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint8_t& val)
{
writeType<uint8_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint16_t& val)
{
writeType<uint16_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint32_t& val)
{
writeType<uint32_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint64_t& val)
{
writeType<uint64_t>(val);
return *this;
}
} // PDU
} // ACN

84
acn/pdu-stream.h Normal file
View File

@ -0,0 +1,84 @@
/*
pdu-stream.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 <memory>
#include <iostream>
namespace ACN {
namespace PDU {
/**
Input/Output stream of nested PDU
*/
class pdu_stream
: private std::basic_streambuf<uint8_t>
, public std::basic_iostream<uint8_t>
{
public:
pdu_stream(uint8_t * p, std::streamsize l)
: std::basic_streambuf<uint8_t>()
, std::basic_iostream<uint8_t>(this)
{
setg(p, p, p + l);
setp(p, p + l);
}
uint32_t available() { return in_avail(); }
uint8_t * data() { return gptr(); };
pdu_stream& operator>> (uint8_t& val);
pdu_stream& operator>> (uint16_t& val);
pdu_stream& operator>> (uint32_t& val);
pdu_stream& operator>> (uint64_t& val);
pdu_stream& operator<< (const uint8_t& val);
pdu_stream& operator<< (const uint16_t& val);
pdu_stream& operator<< (const uint32_t& val);
pdu_stream& operator<< (const uint64_t& val);
template<typename T> T readType()
{
if (in_avail() < sizeof(T)) {
setstate(std::ios_base::failbit);
return 0;
}
T ret = 0;
auto data = reinterpret_cast<uint8_t*>(&ret);
for (int i = sizeof(T); --i >= 0; )
data[i] = get();
if (!in_avail())
setstate(std::ios_base::eofbit);
return ret;
}
template<typename T> void writeType (const T& val)
{
auto data = reinterpret_cast<const uint8_t*>(&val);
for (int i = sizeof(T); --i >= 0; )
put(data[i]);
}
};
using Stream = std::shared_ptr<pdu_stream>;
} // PDU
} // ACN

View File

@ -28,102 +28,6 @@
namespace ACN {
namespace PDU {
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint8_t& val)
{
val = readType<uint8_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint16_t& val)
{
val = readType<uint16_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint32_t& val)
{
val = readType<uint32_t>();
return *this;
};
/**
* @brief pdu_stream::operator >>
* @param val
* @return
*/
pdu_stream& pdu_stream::operator>> (uint64_t& val)
{
val = readType<uint64_t>();
return *this;
};
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint8_t& val)
{
writeType<uint8_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint16_t& val)
{
writeType<uint16_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint32_t& val)
{
writeType<uint32_t>(val);
return *this;
}
/**
* @brief pdu_stream::operator <<
* @param val
* @return
*/
pdu_stream& pdu_stream::operator<< (const uint64_t& val)
{
writeType<uint64_t>(val);
return *this;
}
/**
* @brief Pdu::Pdu
* @param stream
@ -180,7 +84,8 @@ Pdu::Pdu(Stream stream, size_t vector_size)
/**
* @brief Pdu::~Pdu
*/
Pdu::~Pdu() {
Pdu::~Pdu()
{
if (header_) delete header_;
if (data_) delete data_;
}
@ -190,7 +95,8 @@ Pdu::~Pdu() {
* @brief Pdu::vector
* @return
*/
const uint32_t Pdu::vector() {
const uint32_t Pdu::vector()
{
if (flags_.hasVector)
return vector_;
if (inherit_ != nullptr)
@ -203,7 +109,8 @@ const uint32_t Pdu::vector() {
* @brief Pdu::header
* @return
*/
pdu_header * Pdu::header() {
pdu_header * Pdu::header()
{
if (flags_.hasHeader)
return header_;
if (inherit_ != nullptr)
@ -216,7 +123,8 @@ pdu_header * Pdu::header() {
* @brief Pdu::data
* @return
*/
pdu_data * Pdu::data() {
pdu_data * Pdu::data()
{
if (flags_.hasData)
return data_;
if (inherit_ != nullptr)
@ -229,7 +137,8 @@ pdu_data * Pdu::data() {
* @brief Pdu::readLength
* @param stream
*/
void Pdu::readLength(Stream stream) {
void Pdu::readLength(Stream stream)
{
length_ = stream->readType<uint16_t>() & 0x0fff; // high 4 bytes are flags
if (flags_.hasLength)
length_ = (length_ << 8 ) | stream->readType<uint8_t>();
@ -240,7 +149,8 @@ void Pdu::readLength(Stream stream) {
* @brief Pdu::readVector
* @param vector_size
*/
void Pdu::readVector(uint8_t vector_size) {
void Pdu::readVector(uint8_t vector_size)
{
vector_ = 0;
for (int o = vector_size - 1; o >= 0; o--)
vector_ |= (stream_->readType<uint8_t>() << (8 * o));
@ -251,7 +161,8 @@ void Pdu::readVector(uint8_t vector_size) {
* @brief pdu_flags::pdu_flags
* @param val
*/
pdu_flags::pdu_flags(uint8_t val) {
pdu_flags::pdu_flags(uint8_t val)
{
hasLength = (val >> 7) & 0b1;
hasVector = (val >> 6) & 0b1;
hasHeader = (val >> 5) & 0b1;

View File

@ -25,9 +25,9 @@
#include <cstdint>
#include <functional>
#include <iostream>
#include <memory>
#include <vector>
#include "pdu-stream.h"
namespace ACN {
namespace PDU {
@ -54,53 +54,6 @@ struct pdu_header { virtual ~pdu_header() {} };
struct pdu_data { virtual ~pdu_data() {} };
/**
Input/Output stream of nested PDU
*/
class pdu_stream
: private std::basic_streambuf<uint8_t>
, public std::basic_iostream<uint8_t>
{
public:
pdu_stream(uint8_t * p, std::streamsize l)
: std::basic_streambuf<uint8_t>()
, std::basic_iostream<uint8_t>(this)
{
setg(p, p, p + l);
setp(p, p + l);
}
uint32_t available() { return in_avail(); }
uint8_t * data() { return gptr(); };
pdu_stream& operator>> (uint8_t& val);
pdu_stream& operator>> (uint16_t& val);
pdu_stream& operator>> (uint32_t& val);
pdu_stream& operator>> (uint64_t& val);
pdu_stream& operator<< (const uint8_t& val);
pdu_stream& operator<< (const uint16_t& val);
pdu_stream& operator<< (const uint32_t& val);
pdu_stream& operator<< (const uint64_t& val);
template<typename T> T readType() {
if (in_avail() < sizeof(T)) {
setstate(std::ios_base::failbit);
return 0;
}
T ret = 0;
auto data = reinterpret_cast<uint8_t*>(&ret);
for (int i = sizeof(T); --i >= 0; )
data[i] = get();
if (!in_avail())
setstate(std::ios_base::eofbit);
return ret;
}
template<typename T> void writeType (const T& val) {
auto data = reinterpret_cast<const uint8_t*>(&val);
for (int i = sizeof(T); --i >= 0; )
put(data[i]);
}
};
using Stream = shared_ptr<pdu_stream>;
/**
Base class PDU
@ -151,7 +104,6 @@ private:
void readVector(uint8_t);
};
template <typename T>
using Handler = std::function<void(std::shared_ptr<T>)>;
@ -165,7 +117,7 @@ using Block = std::shared_ptr<std::vector<std::shared_ptr<T>>>;
@return std::shared_ptr<std::vector<std::shared_ptr<T>>> A block of PDU
*/
template<typename T>
Block<T> readBlock(Stream buffer, shared_ptr<PDU::Pdu> parent = 0) {
Block<T> readBlock(Stream buffer, shared_ptr<PDU::Pdu> parent = nullptr) {
auto block = Block<T>(new vector<shared_ptr<T>>);
while(buffer->good()) {
shared_ptr<T> pdu(new T(buffer));
@ -173,7 +125,7 @@ Block<T> readBlock(Stream buffer, shared_ptr<PDU::Pdu> parent = 0) {
break;
if (pdu->stream()->fail()) // pdu buffer errors
continue;
if (parent != 0) // set parent
if (parent) // set parent
pdu->setParent(parent);
if (!block->empty()) // set inheritee
pdu->setInherit(block->back());