Search code examples
c++qtevent-handlingudpudpclient

Qt EventLoop delay with high frequency signal<->slot connections


I'm really going crazy over this and I hope somebody has an answer... I do encounter a strange problem with QUdpSockets and the Signal and Slot Connection. I'm receiving small data packets (64 bytes) at 3 different UdpSockets with 100Hz without any processing afterwards. The Signal<-> Slot Connection for reading out the data seams to work completly fine on different laptops, but not on the pc it is supposed to run in the end, which has way more processing power than the laptops.

I'm running the same code on every machine with the same setup. The Problem is, the recvEvents are somehow being queued and delayed up to several seconds on the pc, but not on the laptops. At first I thought It would be a network issue, but I already tried different network cards/switches etc.. and several other things. The only thing that helped is changing the receive code from signal<->slots to another thread with std::thread, so it definitely has to do something with the connection from the readyRead signal.Some Code:


connect(&udpSocket, &QUdpSocket::readyRead, this, &DataController::handleData);// Called with 100Hz
void handleData(){     //This function call is beeing delayed/queued....
    udpSocket.readDatagram(&data,datasize);    
}

My Question:

  1. Is there any limit of Events/seconds which can be handled?
  2. Do you have any suggestion where I can look for errors or how to speed up the QEventLoop?

Im Running: Win10 Pro, VS 2017 64bit,Qt 5.11.2

BR mike


Solution

  • Obviously, with slower computers you always have only one datagram waiting for reading. With faster computer you may have received more than one by the time you are in your slot reading. You should always read all pending datagrams when you receive readyRead signal. You can use QUdpSocket::hasPendingDatagrams for that.

    void handleData(){
        while (udpSocket.hasPendingDatagrams()) {
            // clear data buffer
            udpSocket.readDatagram(&data,datasize);
            // process received datagram before reading next...
        }
    }