PDA

View Full Version : QUdpSocket High rate message reading



danics
2nd August 2016, 13:24
Hi, 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 and I don't know how but my slot didn't signaled any more 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 did'nt change after Bound.

Thanks in advance

anda_skoa
2nd August 2016, 14:55
That QueuedConnection looks wrong, any specific reason why you added it?

If the QUdpSocket is in the same thread as the receiver this just adds more delay, if they are on separate threads then the socket is used from two threads without concurrency protection.

Cheers,
_

danics
2nd August 2016, 15:01
I mentioned it in my code

// the rate of receiving data is 10 msec if i don't put Qt::QueuedConnection, it didn't receive any more signal after first one. why ???
// change the rate of data to 1 sec and this code work well without Qt::QueuedConnection !!!

I see this bug report do you think its related? I m using Qt 5.6 on Windows 7
https://bugreports.qt.io/browse/QTBUG-46552

anda_skoa
2nd August 2016, 17:28
Well, that bug should be fixed in your version, but it indeed sounds similar.

When you say your slot isn't called anymore, do you mean both slots or one specific, i.e. the data receive slot or the widget update slot?

Is there any interaction from the data processing to the widget?

Cheers,
_

danics
2nd August 2016, 20:06
When you say your slot isn't called anymore, do you mean both slots or one specific, i.e. the data receive slot or the widget update slot?

No, just my socket received slot doesn't call any more, actually my widgets remained responsive and work well


Is there any interaction from the data processing to the widget?

No, When data received I just update some values in my class, and my widget just read this values each 100 ms from class and fill the form and update some charts

Added after 1 20 minutes:

How can I find if signals queue is full or not?

danics
3rd August 2016, 07:58
Hi, I was wrong about my Qt version, it was Qt 5.5 and now I updated to 5.7 and everything works great now even without QueuedConnection and with default connection. thanks