Jack
Jack

Reputation: 1769

Boost.ASIO UDP socket: sink all the packets

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:

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

Upvotes: 0

Views: 1830

Answers (2)

Galimov Albert
Galimov Albert

Reputation: 7357

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

Upvotes: 0

David Schwartz
David Schwartz

Reputation: 182761

  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.

Upvotes: 1

Related Questions