Qt应用处理高频UDP数据时出现无响应问题求助
高频UDP数据导致Qt应用无响应问题
我开发了一款处理高频数据的Qt应用,当前每30ms接收约2kb的UDP数据,但应用会出现无响应的情况。
注意:仅在UDP连接时出现该问题;若通过定时器循环更新相同数据(不使用UDP连接),应用运行完全正常。
UDP处理相关代码
#ifndef UDPHANDLER_H #define UDPHANDLER_H #include <QObject> #include <QUdpSocket> #include <QThread> #include <QHostAddress> #include <QElapsedTimer> class UdpSocketHandler : public QObject { Q_OBJECT public: explicit UdpSocketHandler(QObject *parent = nullptr); ~UdpSocketHandler(); void bindSocket(const QHostAddress &address, quint16 port); public slots: void onReadyRead(); // Slot to handle the datagrams private: QUdpSocket *udpSocket; QThread *workerThread; // Thread for the UDP handling QElapsedTimer Elapsed_time; QHostAddress address; quint16 port; }; #endif // UDPHANDLER_H #include "udphandler.h" #include "custom/data/DataFile.h" #include <QDebug> #include <QtConcurrent/QtConcurrent> #include <QTimer> UdpSocketHandler::UdpSocketHandler(QObject *parent) : QObject(parent), udpSocket(nullptr), workerThread(new QThread(this)) { } void UdpSocketHandler::setupThread() { this->moveToThread(workerThread); connect(workerThread, &QThread::started, this, &UdpSocketHandler::setupSocket); workerThread->start(); } UdpSocketHandler::~UdpSocketHandler() { if (workerThread->isRunning()) { workerThread->quit(); workerThread->wait(); } if (udpSocket) { udpSocket->close(); delete udpSocket; udpSocket = nullptr; } delete workerThread; } void UdpSocketHandler::setupSocket() { udpSocket = new QUdpSocket(this); udpSocket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, 64 * 1024); udpSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1); if (udpSocket->bind(address, port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint)) { qWarning() << Q_FUNC_INFO << "Socket bound to Addr:" << address.toString() << "PORT:" << port; connect(udpSocket, &QUdpSocket::readyRead, this, &UdpSocketHandler::onReadyRead); } else { qWarning() << Q_FUNC_INFO << "Failed to bind UDP socket. Addr:" << address.toString() << "PORT:" << port; } } void UdpSocketHandler::bindSocket(const QHostAddress &m_address, quint16 m_port) { address = m_address; port = m_port; setupThread(); } void UdpSocketHandler::onReadyRead() { while (udpSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(), datagram.size()); QtConcurrent::run([datagram]() { aerx::readDataFromStruct(datagram); }); datagram.clear(); } } //In main.cpp UdpSocketHandler* udpHandler = new UdpSocketHandler(); udpHandler->bindSocket(QHostAddress(offsetSenderIP), offsetSenderPORT);
测试代码(无UDP连接时正常运行)
connect(timerForTest, &QTimer::timeout, this, [this](){ aerx::setMapVal(KEY_PitchVal, pitch); aerx::setMapVal(KEY_YawVal, yaw); aerx::setMapVal(KEY_RollVal, roll); pitch += i_pitch; yaw += i_yaw; roll += i_roll; if(pitch >= 90 || pitch <= -90) i_pitch *= -1; if(yaw >= 180 && yaw <= -180) i_yaw *= -1; if(roll >= 50 || roll <= -50) i_roll *= -1; }); //timerForTest->start(33); /* uncomment this, to test smoothness of PFD UI update without data coming from simulator */
内容的提问来源于stack exchange,提问作者Imran
相关产品推荐
相关产品推荐

