libdrmconf 0.15.0
A library to program DMR radios.
Loading...
Searching...
No Matches
packetstream.hh
1#ifndef PACKETSTREAM_HH
2#define PACKETSTREAM_HH
3
4#include <QObject>
5#include "errorstack.hh"
6
7class QIODevice;
8
12class PacketStream : public QObject
13{
14 Q_OBJECT
15
16protected:
18 explicit PacketStream(QObject *parent = nullptr);
19
20public:
22 virtual ~PacketStream();
23
25 virtual bool isOpen() const = 0;
27 virtual void close() = 0;
28
30 virtual bool receive(QByteArray &buffer, int timeout=-1, const ErrorStack &err=ErrorStack()) = 0;
32 virtual bool send(const QByteArray& buffer, int timeout=-1, const ErrorStack &err=ErrorStack()) = 0;
33};
34
35
39{
40 Q_OBJECT
41
42public:
44 SlipStream(QIODevice *device, QObject *parent=nullptr);
45
46public:
47 bool isOpen() const override;
48 void close() override;
49 bool receive(QByteArray &buffer, int timeout=-1, const ErrorStack &err=ErrorStack()) override;
50 bool send(const QByteArray& buffer, int timeout=-1, const ErrorStack &err=ErrorStack()) override;
51
52protected:
53 QIODevice *_device;
54 QByteArray _buffer;
55
56private:
57 static constexpr char END_OF_PACKET = '\xC0';
58 static constexpr char ESCAPE = '\xDB';
59 static constexpr char ESCAPED_C0 = '\xDC';
60 static constexpr char ESCAPED_DB = '\xDD';
61};
62
63
64
65#endif // PACKETSTREAM_HH
Implements a stack of error messages to provide a pretty formatted error traceback.
Definition errorstack.hh:43
PacketStream(QObject *parent=nullptr)
Hidden constructor.
Definition packetstream.cc:8
virtual void close()=0
Closes the stream.
virtual ~PacketStream()
Destructor.
Definition packetstream.cc:14
virtual bool isOpen() const =0
Returns true if the stream is open.
virtual bool send(const QByteArray &buffer, int timeout=-1, const ErrorStack &err=ErrorStack())=0
Receives a datagram.
virtual bool receive(QByteArray &buffer, int timeout=-1, const ErrorStack &err=ErrorStack())=0
Receives a datagram.
bool receive(QByteArray &buffer, int timeout=-1, const ErrorStack &err=ErrorStack()) override
Receives a datagram.
Definition packetstream.cc:45
void close() override
Closes the stream.
Definition packetstream.cc:38
SlipStream(QIODevice *device, QObject *parent=nullptr)
Constructor, takes ownership of the device.
Definition packetstream.cc:23
bool send(const QByteArray &buffer, int timeout=-1, const ErrorStack &err=ErrorStack()) override
Receives a datagram.
Definition packetstream.cc:85
bool isOpen() const override
Returns true if the stream is open.
Definition packetstream.cc:32