mirror of
https://github.com/azaion/autopilot.git
synced 2026-04-22 22:46:33 +00:00
Added minimal UDP client and server examples
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QUdpSocket>
|
||||||
|
#include <QHostAddress>
|
||||||
|
#include <QThread>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
QCoreApplication a(argc, argv);
|
||||||
|
|
||||||
|
if (argc != 2) {
|
||||||
|
qDebug() << "Give port number of server as only parameter";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
QUdpSocket udpSocket;
|
||||||
|
quint16 receiverPort = atoi(argv[1]);
|
||||||
|
|
||||||
|
for (int i = 0; i < 10000000 ; i++) {
|
||||||
|
QJsonObject json;
|
||||||
|
json["value"] = i;
|
||||||
|
QJsonDocument jsonDoc(json);
|
||||||
|
QByteArray datagram = jsonDoc.toJson();
|
||||||
|
|
||||||
|
qint64 bytesSent = udpSocket.writeDatagram(datagram, QHostAddress("127.0.0.1"), receiverPort);
|
||||||
|
|
||||||
|
if (bytesSent == -1) {
|
||||||
|
qWarning("Failed to send the datagram: %s", qPrintable(udpSocket.errorString()));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
qDebug("Datagram sent successfully");
|
||||||
|
}
|
||||||
|
|
||||||
|
QThread::msleep(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
QT = core network
|
||||||
|
|
||||||
|
CONFIG += c++17 cmdline
|
||||||
|
|
||||||
|
SOURCES += \
|
||||||
|
main.cpp
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#include <QCoreApplication>
|
||||||
|
#include "udpserver.h"
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QCoreApplication a(argc, argv);
|
||||||
|
UdpServer server(5678);
|
||||||
|
return a.exec();
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
QT = core network
|
||||||
|
|
||||||
|
CONFIG += c++17 cmdline
|
||||||
|
|
||||||
|
# You can make your code fail to compile if it uses deprecated APIs.
|
||||||
|
# In order to do so, uncomment the following line.
|
||||||
|
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||||
|
|
||||||
|
SOURCES += \
|
||||||
|
main.cpp \
|
||||||
|
udpserver.cpp
|
||||||
|
|
||||||
|
# Default rules for deployment.
|
||||||
|
qnx: target.path = /tmp/$${TARGET}/bin
|
||||||
|
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||||
|
!isEmpty(target.path): INSTALLS += target
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
udpserver.h
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include "udpserver.h"
|
||||||
|
|
||||||
|
UdpServer::UdpServer(int portNumber, QObject *parent)
|
||||||
|
: QObject{parent}
|
||||||
|
{
|
||||||
|
mUdpSocket = new QUdpSocket(this);
|
||||||
|
connect(mUdpSocket, &QUdpSocket::readyRead, this, &UdpServer::readPendingDatagrams);
|
||||||
|
mUdpSocket->bind(QHostAddress::Any, portNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void UdpServer::readPendingDatagrams()
|
||||||
|
{
|
||||||
|
while (mUdpSocket->hasPendingDatagrams()) {
|
||||||
|
QByteArray datagram;
|
||||||
|
datagram.resize(mUdpSocket->pendingDatagramSize());
|
||||||
|
mUdpSocket->readDatagram(datagram.data(), datagram.size());
|
||||||
|
|
||||||
|
QJsonParseError jsonError;
|
||||||
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(datagram, &jsonError);
|
||||||
|
if (jsonError.error != QJsonParseError::NoError) {
|
||||||
|
qWarning() << "Error parsing JSON:" << jsonError.errorString();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (jsonDoc.isObject()) {
|
||||||
|
emit newJsonDocument(jsonDoc);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test code to see that valid value was in the doc.
|
||||||
|
QJsonObject jsonObj = jsonDoc.object();
|
||||||
|
if (jsonObj.contains("value") && jsonObj["value"].isDouble()) {
|
||||||
|
int value = jsonObj["value"].toInt();
|
||||||
|
qDebug() << "Received value:" << value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
qWarning() << "Invalid or missing 'value' field in JSON";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QUdpSocket>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
|
||||||
|
class UdpServer : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit UdpServer(int portNumber, QObject *parent = nullptr);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void readPendingDatagrams();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QUdpSocket *mUdpSocket;
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void newJsonDocument(QJsonDocument);
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user