0
votes

I'm using the boost::asio::ip::udp::socket to receive UDP packets via socket's async_receive_from method.

The code works fine, the only problem is that in the time I process a packet, lots more come creating a queue (the buffer) to process. My program though must sink all the packets received since the start of the processing, so that it listens only to the most recent ones.

Example:

  • packet 1 is sent
  • packet 1 is being processed
  • packets 2, 3, 4 are sent
  • packet 1 ends the computation
  • flush the buffer
  • packet 5 is sent
  • packet 5 is being processed
  • etc

Is there any way to discard the packets in the middle? Thanks!

2
You have to store and order your packets before you process them. For example you could add them to a list and then process this list from the back.user743414
Thanks user743414. I currently do it via the buffer. What I don't want is to process "old" packets, I just want to process as many packets I can, discarding the ones in the middle.Jack
Define "old" packet? Are you using a timestamp or a simple up counting id? When you just want to process the last packet, then you just have to store the last received packet.user743414
Nope, nothing of it, they are just commands that overwrite themselves. I need to process only the last one, because otherwise I'd create a delay. I don't need to process the packets in the middle. And I can't specify a buffer's minimum size as packets have different sizesJack
Is the processing of packet 1 on the sender or receiver?Collin Dauphinee

2 Answers

1
votes
  1. Use a buffer that holds only a single datagram.

  2. Keep reading into the buffer until there are no more datagrams to read.

  3. If you read at least one packet, process the datagram in the buffer.

  4. Go to step 2.

Note that UDP is a datagram protocol, not a packet protocol. A single UDP datagram can be split over multiple packets.

0
votes

I think it can be done quite simple:

  1. async_receive_from until got a packet.
  2. Check available method to determine is more data in the socket.
  3. If we have more data, discard buffer and goto 1; else process packet