Skip to content

Commit

Permalink
Comms: Update UdpIODevice
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Oct 25, 2024
1 parent 2095344 commit cf66c9f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 28 deletions.
82 changes: 64 additions & 18 deletions src/Comms/UdpIODevice.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,84 @@
****************************************************************************/

#include "UdpIODevice.h"
#include "QGCLoggingCategory.h"

QGC_LOGGING_CATEGORY(UdpIODeviceLog, "qgc.comms.udpiodevice")

UdpIODevice::UdpIODevice(QObject *parent) : QUdpSocket(parent)
UdpIODevice::UdpIODevice(QObject *parent)
: QUdpSocket(parent)
{
// this might cause data to be available only after a second readyRead() signal
connect(this, &QUdpSocket::readyRead, this, &UdpIODevice::_readAvailableData);
// qCDebug(UdpIODeviceLog) << Q_FUNC_INFO << this;

(void) connect(this, &QUdpSocket::readyRead, this, &UdpIODevice::_readAvailableData);
}

UdpIODevice::~UdpIODevice()
{
// qCDebug(UdpIODeviceLog) << Q_FUNC_INFO << this;
}

bool UdpIODevice::canReadLine() const
{
return _buffer.indexOf('\n') > -1;
return _buffer.contains('\n');
}

qint64 UdpIODevice::readLineData(char *data, qint64 maxSize)
{
int length = _buffer.indexOf('\n') + 1; // add 1 to include the '\n'
if (length == 0) {
return 0;
if (_buffer.isEmpty()) {
return -1; // No data available
}

const qsizetype newlineIndex = _buffer.indexOf('\n');
qsizetype length;
if (newlineIndex != -1) {
length = newlineIndex + 1; // Include the newline character
} else {
// No newline character found
length = qMin(_buffer.size(), static_cast<qsizetype>(maxSize));
}

const qsizetype bytesToCopy = qMin(length, static_cast<qsizetype>(maxSize));
(void) memcpy(data, _buffer.constData(), bytesToCopy);
_buffer.remove(0, bytesToCopy);

return bytesToCopy;
}

qint64 UdpIODevice::readData(char* data, qint64 maxSize)
{
if (_buffer.isEmpty()) {
return -1; // No data available
}
length = std::min(length, static_cast<int>(maxSize));
// copy lines to output
std::copy(_buffer.data(), _buffer.data() + length, data);
// trim buffer to remove consumed line
_buffer = _buffer.right(_buffer.size() - length);
// return number of bytes read
return length;

const qsizetype bytesToCopy = qMin(static_cast<qsizetype>(maxSize), _buffer.size());
(void) memcpy(data, _buffer.constData(), bytesToCopy);
_buffer.remove(0, bytesToCopy);

return bytesToCopy;
}

qint64 UdpIODevice::bytesAvailable() const
{
return (_buffer.size() + QIODevice::bytesAvailable());
}

void UdpIODevice::_readAvailableData() {
void UdpIODevice::_readAvailableData()
{
while (hasPendingDatagrams()) {
int previousSize = _buffer.size();
_buffer.resize(static_cast<int>(_buffer.size() + pendingDatagramSize()));
readDatagram((_buffer.data() + previousSize), pendingDatagramSize());
QByteArray datagram;
datagram.resize(static_cast<int>(pendingDatagramSize()));
QHostAddress sender;
quint16 senderPort;
const qint64 bytesRead = readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
if (bytesRead > 0) {
(void) _buffer.append(datagram.left(bytesRead));
} else {
qCWarning(UdpIODeviceLog) << "Error reading datagram:" << errorString();
}
}

if (!_buffer.isEmpty()) {
emit readyRead();
}
}
23 changes: 13 additions & 10 deletions src/Comms/UdpIODevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@
* COPYING.md in the root of the source code directory.
*
****************************************************************************/

#pragma once

#include <QtCore/QByteArray>
#include <QtCore/QLoggingCategory>
#include <QtNetwork/QUdpSocket>

/**
* @brief QUdpSocket implementation of canReadLine() readLineData() in server mode.
* The UdpIODevice class works almost exactly as a QUdpSocket, but
* also implements canReadLine() and readLineData() while in the bound state.
* Regular QUdpSocket only allows to use these QIODevice interfaces when using
* connectToHost(), which means it is working as a client instead of server.
*
**/
Q_DECLARE_LOGGING_CATEGORY(UdpIODeviceLog)

/// UdpIODevice provides a QIODevice interface over a QUdpSocket in server mode.
/// It allows line-based reading using canReadLine() and readLineData() even when the socket is in bound mode.
class UdpIODevice: public QUdpSocket
{
Q_OBJECT

public:
UdpIODevice(QObject *parent = nullptr);
bool canReadLine() const;
qint64 readLineData(char *data, qint64 maxSize);
~UdpIODevice();

bool canReadLine() const override;
qint64 readLineData(char* data, qint64 maxSize) override;
qint64 readData(char* data, qint64 maxSize) override;
qint64 bytesAvailable() const override;
bool isSequential() const override { return true; }

private slots:
void _readAvailableData();
Expand Down

0 comments on commit cf66c9f

Please sign in to comment.