prepare pdu_stream class for output buffering too
This commit is contained in:
parent
ce28e5c645
commit
84476d33c7
37
acn/pdu.h
37
acn/pdu.h
|
@ -25,7 +25,7 @@
|
|||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <istream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
|
@ -54,30 +54,26 @@ struct pdu_header { virtual ~pdu_header() {} };
|
|||
struct pdu_data { virtual ~pdu_data() {} };
|
||||
|
||||
|
||||
/**
|
||||
Memory buffer of uint8_t data.
|
||||
*/
|
||||
class pdu_buffer
|
||||
: public std::basic_streambuf<uint8_t>
|
||||
{
|
||||
public:
|
||||
pdu_buffer(uint8_t * p, size_t l) { setg(p, p, p + l); };
|
||||
uint8_t * in_ptr() { return gptr(); };
|
||||
};
|
||||
/**
|
||||
Input/Output stream of nested PDU
|
||||
*/
|
||||
|
||||
class pdu_stream
|
||||
: public std::basic_istream<uint8_t>
|
||||
: private std::basic_streambuf<uint8_t>
|
||||
, public std::basic_iostream<uint8_t>
|
||||
{
|
||||
public:
|
||||
pdu_stream(uint8_t * p, size_t l)
|
||||
: std::basic_istream<uint8_t>(&_buffer)
|
||||
, _buffer(p, l) { rdbuf(&_buffer); }
|
||||
uint32_t available() { return _buffer.in_avail(); }
|
||||
uint8_t * data() { return _buffer.in_ptr(); };
|
||||
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(); };
|
||||
template<typename T> T readType() {
|
||||
if (available() < sizeof(T)) {
|
||||
if (in_avail() < sizeof(T)) {
|
||||
setstate(std::ios_base::failbit);
|
||||
return 0;
|
||||
}
|
||||
|
@ -85,13 +81,10 @@ public:
|
|||
auto data = reinterpret_cast<uint8_t*>(&ret);
|
||||
for (int i = sizeof(T); --i >= 0; )
|
||||
data[i] = get();
|
||||
if (!available())
|
||||
if (!in_avail())
|
||||
setstate(std::ios_base::eofbit);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private:
|
||||
pdu_buffer _buffer;
|
||||
};
|
||||
using Stream = shared_ptr<pdu_stream>;
|
||||
|
||||
|
|
Loading…
Reference in New Issue