Quote:> Hi all,
> I want to do this:
> - receive UDP datagram from an host A;
> - delay the datagram for a time T;
> - transmit the packet to an host B.
> I've to do the same thing for the datagrams sent from B to A.
> My problem is that A (and B) transmit continuously, and when I delay a
> packet the next packet can arrive. What type of solution can I use?
> There is someone that has resolved similar problems?
Just use a linked list of packets with arrival timestamps. A
multithreaded approach might be easiest, but you can use a poll/select loop
type approach as well.
Since UDP sockets are always writable, you don't have to worry about
your writes blocking. So your code could look like a big, simple loop:
1) Compose an FD_SET include both sockets.
2) Check if there are any packets in either queue. If not, 'select' on
the FD_SET (for readability) with a NULL timeout. Otherwise, 'select' with a
timeout equal to how long before the first packet (head of either queue)
need to be sent.
3) Call 'select'.
4) Note the time and see if the head packet on either queue (A-to-B or
B-to-A) is ready to send. If so, pop it off the queue and send it.
5) If 'select' returned non-zero, check which fds are set in the read
set. Loop on 'recvmsg' on that fd until it returns EWOULDBLOCK. (Make sure
to make the sockets non-blocking!) For any received packets, stuff them in
the queue. (You already noted the time in step 4, so you can put that
information in the queue entry so you'll know when to send them.)
6) Jump back to step 1.