Hi there,

I have been doing some basic network programming using Qt and want to further my knowledge to tackle the following problem. First some background info.

I have a network accessible device that accepts UDP packets on a particular port. The UDP packets contain specific binary data in a certain order (eg. first byte indicates packet type etc). The device will send responses back to the originating source port - so one can send and receive from the device using a single port. I have this working nicely.

The setup is like this:

MyApp <--- UDP ---> MyDevice

Now, I want to bind to another port on the host, and repackage any incoming data to send to MyDevice like this

SomeOtherUDPSource <--- UDP ---> MyApp <--- UDP ---> MyDevice

Now, the problem is this: MyDevice isn't so quick with its processing. It appears I need to slow the sending rate down. Now, according to the doco for QAbstractSocket buffering isn't provided for QUdpSocket which is left to the operating system to handle.

That's okay, but what's the best way to handle it?

  • Sleep the thread for a bit - Correct me if I am wrong, but the problem with this approach is that depending on the sleep interval, the OS may drop the buffered packet, afterall nothing has read it.
  • Implement a queue producer/consumer approach - multi thread approach, one thread (say incoming) adds the packets to a queue which is then processed more slowly by another thread (outgoing). Here, incoming is receiving faster than outgoing so overruns will occur eventually but this is now a matter of the amount of memory for the queue, correct? And a simple overrun check could be added I suppose.
  • Have a timer with a slot that sends a packet on the incoming queue every Xms - slightly easier than the threaded approach?


I'd rather get the design right before launching into implementation, so would like to hear thoughts an opinions on a good way to approach this...

Cheers,

Xav.