148 lines
3.9 KiB
C++
148 lines
3.9 KiB
C++
/*
|
|
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 "uuid/uuid.h"
|
|
|
|
#include <memory>
|
|
#include <iostream>
|
|
|
|
namespace ACN::PDU {
|
|
|
|
struct pdu_stream_object;
|
|
|
|
/**
|
|
* @brief Input/Output stream of nested PDU
|
|
*/
|
|
class pdu_stream
|
|
: public std::enable_shared_from_this<pdu_stream>
|
|
, private std::basic_streambuf<uint8_t>
|
|
, public std::basic_iostream<uint8_t>
|
|
{
|
|
public:
|
|
pdu_stream(uint8_t * p, std::streamsize l);
|
|
|
|
// input sequence
|
|
uint32_t available();
|
|
uint8_t * data();
|
|
|
|
// output sequence
|
|
uint32_t size();
|
|
uint8_t * base();
|
|
|
|
// stream objects
|
|
pdu_stream& operator>> (pdu_stream_object& obj);
|
|
pdu_stream& operator<< (const pdu_stream_object& obj);
|
|
|
|
// unsigned integer
|
|
pdu_stream& operator>> (uint8_t& val);
|
|
pdu_stream& operator<< (const uint8_t& val);
|
|
pdu_stream& operator>> (uint16_t& val);
|
|
pdu_stream& operator<< (const uint16_t& val);
|
|
pdu_stream& operator>> (uint32_t& val);
|
|
pdu_stream& operator<< (const uint32_t& val);
|
|
pdu_stream& operator>> (uint64_t& val);
|
|
pdu_stream& operator<< (const uint64_t& val);
|
|
|
|
// signed integer
|
|
pdu_stream& operator>> (int8_t& val);
|
|
pdu_stream& operator<< (const int8_t& val);
|
|
pdu_stream& operator>> (int16_t& val);
|
|
pdu_stream& operator<< (const int16_t& val);
|
|
pdu_stream& operator>> (int32_t& val);
|
|
pdu_stream& operator<< (const int32_t& val);
|
|
pdu_stream& operator>> (int64_t& val);
|
|
pdu_stream& operator<< (const int64_t& val);
|
|
|
|
// uuid
|
|
pdu_stream& operator>> (UUID::uuid& uuid);
|
|
pdu_stream& operator<< (const UUID::uuid& uuid);
|
|
|
|
// strings
|
|
void readString(std::string& str, const int fixed_length = 0);
|
|
void writeString(const std::string& str, const size_t fixed_length = 0,
|
|
const bool terminated = true);
|
|
|
|
// reinterpreted i/o
|
|
/**
|
|
* @brief readType
|
|
* @return T
|
|
*/
|
|
template<typename T>
|
|
T readType()
|
|
{
|
|
if (in_avail() < sizeof(T))
|
|
setstate(std::ios_base::failbit);
|
|
if (!good())
|
|
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;
|
|
}
|
|
/**
|
|
* @brief writeType
|
|
* @param val
|
|
*/
|
|
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>;
|
|
|
|
|
|
/**
|
|
* @brief The pdu_stream_object struct
|
|
*/
|
|
struct pdu_stream_object
|
|
{
|
|
virtual ~pdu_stream_object() {};
|
|
/**
|
|
* @brief streamSize
|
|
* @return length (count of octets) on the wire
|
|
*/
|
|
virtual size_t streamSize() const = 0;
|
|
/**
|
|
* @brief fill structure data from input stream
|
|
*/
|
|
virtual void iStream(Stream) = 0;
|
|
/**
|
|
* @brief write structure data to output stream
|
|
*/
|
|
virtual void oStream(Stream) const = 0;
|
|
};
|
|
|
|
|
|
} // ACN::PDU
|
|
|