Signal - slot question.
-
One class gets a UDP packet and emits a signal to a parser
void UDP::ReadyRead() { socket->readDatagram(udp_buffer.data(), udp_buffer.size(), &sender, &senderPort); emit ReadyForReader(udp_buffer); }
Another class process received data in a slot
void READER::ReadyForReader( QByteArray data) { //process }
What if the reader class slower then the udp class. Say the udp gets 3 packets and the reader process only one by the time. Is signals and data pending?
-
@jenya7 unless you explicitly break this, by calling processEvents, than functions are blocking. Meaning
If same thread, readyRead signals will accumulate, until your parser is finished.
If different thread, and no processEvent calls, the ReadyForReader signal will accumulate until your parser is finished.
-
@jenya7
Not with you: "classes" don't have speeds.Are you using separate threads, or are your signal & slot in one and the same thread?
Assuming single thread: your
ReadyRead
reads one datagram, andReadyForReader
is called immediately to process the datagram.ReadyRead
will eventually be called for all datagrams received. The OS/network will presumably have some buffer size for pending datagrams.I'm not sure you're supposed to write your code exactly as you have.
ReadyRead
might be called when there are several datagrams received/available. I think you're supposed to do awhile
loop inReadyRead()
. E.g. from https://doc.qt.io/qt-5/qudpsocket.html#detailsvoid Server::readPendingDatagrams() { while (udpSocket->hasPendingDatagrams()) { QNetworkDatagram datagram = udpSocket->receiveDatagram(); processTheDatagram(datagram); } }