QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
UdpIODevice.cc
Go to the documentation of this file.
1#include "UdpIODevice.h"
3
4QGC_LOGGING_CATEGORY(UdpIODeviceLog, "Comms.UdpIODevice")
5
6UdpIODevice::UdpIODevice(QObject *parent)
7 : QUdpSocket(parent)
8{
9 // qCDebug(UdpIODeviceLog) << Q_FUNC_INFO << this;
10
11 (void) connect(this, &QUdpSocket::readyRead, this, &UdpIODevice::_readAvailableData);
12}
13
15{
16 // qCDebug(UdpIODeviceLog) << Q_FUNC_INFO << this;
17}
18
20{
21 return _buffer.contains('\n');
22}
23
24qint64 UdpIODevice::readLineData(char *data, qint64 maxSize)
25{
26 const qint64 newlinePos = _buffer.indexOf('\n');
27 if (newlinePos < 0) {
28 return 0;
29 }
30
31 const qint64 length = std::min(newlinePos + 1, maxSize);
32 (void) std::copy_n(_buffer.constData(), length, data);
33
34 (void) _buffer.remove(0, length);
35 return length;
36}
37
38qint64 UdpIODevice::readData(char *data, qint64 maxSize)
39{
40 const qint64 length = std::min<qint64>(_buffer.size(), maxSize);
41 (void) std::copy_n(_buffer.constData(), length, data);
42
43 (void) _buffer.remove(0, length);
44 return length;
45}
46
47void UdpIODevice::_readAvailableData()
48{
49 while (hasPendingDatagrams()) {
50 const qint64 size = pendingDatagramSize();
51 const int oldSize = _buffer.size();
52 _buffer.resize(oldSize + static_cast<int>(size));
53 (void) readDatagram(_buffer.data() + oldSize, size);
54 }
55}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
qint64 readLineData(char *data, qint64 maxSize) override
bool canReadLine() const override
qint64 readData(char *data, qint64 maxSize) override