As far as i understood, the sendPacket()
method gets called to fast/often. This means you will need to create a buffer, but thats quite easy (if run in multiples threads for receivePacket and sendPacket, also add a QMutex::lock()/unlock() before/after accessing buffer)
In Class.h:
QTimer *timer; // Timer for periodically calling a sendPacket
QList<MyPacketClass*> buffer; // List containing the packets to be sent
In Class.cpp Constructor create the timer with connection:
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(slotSendPacket()));
timer->start(1000); // Every second
In receivePacket() just add the data to the list:
buffer.append(new MyPacketClass(data));
In class.cpp create a socket for connection and poping data from the list:
void SomeClass::slotSendPacket()
{
if(buffer.size() > 0)
{
sendPacket(buffer.takeAt(0));
}
}
In method sendPacket(MyPacketClass* inData) free the allocated memory:
Do_whatever_needs_to_be_done_and_sent(); // Here is writeDatagram();
delete inData; // Free the memory for Packetclass