1

everybody!

I have a strange issue in working with QUdpSocket and readyRead signal, I can say it's not working as I think,

I create a QUdpSocket and bind it to some port , connect the readyRead signal to my slot and I read all the pending datagram as below

if(!udp_listener)
{
      udp_listener = new QUdpSocket(this);
      connect(udp_listener, SiGNAL(readyRead()), this, SLOT(readBuffers(), Qt::QueuedConnection);
      // the rate of receiving data is 10 msec if i dont put Qt::QueuedConnection, it didn't receive any more signal after first received. why ???
      // change the rate of data to 1 sec and this code work well without Qt::QueuedConnection !!! 
}

udp_lister.bind(Any, 5555);

and my readBuffers code

void readBuffers() {
    QString buffer;
    while(udp_listener->hasPendingDatagrams()) {
           QByteArray received;
           received.resize(udp_listener->pendingDatagramSize());
           udp_listener->readDatagram(received, received.size(), 0,0);
           buffer.append(received);
           // Do some job in 1 msec on buffer and take data from buffer
           if(/* some works done */) buffer.clear(); // almost every time my buffer got cleared 
    }
}

I thought my problems solved with using of Qt::QueuedConnection but today I add another widget to my project and updated it every 100 msec. I don't know how but my slot didn't signal anymore after 2 seconds.

If I change my timer interval or sending data rate to 1 sec, everything is fine.

all of my classes and my widgets live in main program's thread and I don't use another thread, but it seems I should!

so why signals dropped by Qt eventloop?

I check my socket state and it didn't change after Bound.

Thanks in advance

danics
  • 323
  • 1
  • 9
  • Maybe your program is stuck in the `while(udp_listener->hasPendingDatagrams())` loop? – thuga Aug 02 '16 at 12:24
  • no my Gui is response to all type of signals – danics Aug 02 '16 at 12:25
  • 1
    I found [this](https://bugreports.qt.io/browse/QTBUG-46552) and [this bug report](https://bugreports.qt.io/browse/QTBUG-48556) which seem related. – thuga Aug 02 '16 at 12:36
  • @thuga actually I wrote this code in Qt 5.6 on a Windows 7 platform , but I think yes its related to this bug. what you think is a better implementation? give some advice in an answer so I mark your answer as a correct one. thanks for searching – danics Aug 02 '16 at 12:44
  • I tested this on Windows 7 with Qt 5.5.1, and it worked fine no matter how fast I made it send the datagrams. I ran the sender and the listener on the same machine though, not sure if it matters. – thuga Aug 02 '16 at 13:22
  • @thuga, Did you use Qt::QueueConnection?, plz test this, add into your while QThread::msleep(1) , run another widget beside this class and update it each 100 msec, after some minutes it stopped to getting more datagrams tnx in advance – danics Aug 02 '16 at 13:27

1 Answers1

1


Qt::QueuedConnection tells the signal to be added to the queue, not waiting for it to be treated before continuing.
If the work you do on the received data takes some time, maybe the sending rate is too much higher than the reading rate, resulting in a big signal queue so the qt system blocks the signal?

Don't have the time to test it, but what you say about changing data rate timer makes me think it could be something like that.

Maybe try to measure the time you need to process your data and try some different sending timer lengths to test if you can verify this idea.

RDK
  • 88
  • 2
  • 9
  • How can I find the current signal queue situation? I can't understand why this happen all readBuffers function run in .6 msec – danics Aug 02 '16 at 16:22
  • I was wrong about my Qt version! it was Qt 5.5 I updated it to 5.7 and without QueueConneciton everything works great; :)) – danics Aug 03 '16 at 05:45
  • @danics Ah well there you go then. The bug report says it was fixed in 5.5.1, not 5.5. – thuga Aug 03 '16 at 06:31