LED-BOARD/simulation/VirtualLedBoard/udpserver.cpp

36 lines
852 B
C++
Raw Normal View History

2023-08-13 22:38:07 +02:00
#include "udpserver.h"
#include <QUdpSocket>
#include <QNetworkDatagram>
#define UDP_IMAGE_PORT 4242
2023-08-14 13:53:34 +02:00
UdpLedServer ::UdpLedServer (QObject *parent)
: QObject(parent)
2023-08-13 22:38:07 +02:00
{
initSocket();
}
2023-08-14 13:53:34 +02:00
void UdpLedServer ::initSocket()
2023-08-13 22:38:07 +02:00
{
this->mUdpSocket = new QUdpSocket(this);
this->mUdpSocket->bind(QHostAddress::LocalHost, UDP_IMAGE_PORT);
connect(this->mUdpSocket, &QUdpSocket::readyRead,
2023-08-14 13:53:34 +02:00
this, &UdpLedServer ::readPendingDatagrams);
2023-08-13 22:38:07 +02:00
}
2023-08-14 13:53:34 +02:00
void UdpLedServer ::readPendingDatagrams()
2023-08-13 22:38:07 +02:00
{
while (this->mUdpSocket->hasPendingDatagrams()) {
QNetworkDatagram datagram = this->mUdpSocket->receiveDatagram();
processTheDatagram(datagram);
}
}
2023-08-14 13:53:34 +02:00
void UdpLedServer::processTheDatagram(QNetworkDatagram datagram) {
2023-08-13 22:38:07 +02:00
if (datagram.isValid()) {
qDebug() << "Received datagram:" << datagram.data().size();
}
}