I’m writing a client application to communicate with a server program via UDP. The client periodically makes requests for data and needs to use the most recent server response. The request message has a 16-bit unsigned counter field that is echoed by the server so I can pair requests with server responses.
Since it’s UDP, I have to handle the case where server responses arrive out of order (or don’t arrive at all). Naively, that means holding on to the highest message counter seen so far and dropping any incoming message with a lower number. But that will fail as soon as we pass 65535 messages and the counter wraps back to zero. Is there a good way to detect (with reasonable probability) that, for example, message 5 actually comes after message 65,000?
The implementation language is C++.
If I understand things correctly, you may be able to use some windowing, if you are not already.
That is, do not accept an message that is outside your window. For example, if your counter is at 1000, you may limit your range of incoming counter IDs you will accept to 1000…1031 inclusive. Thus anything outside that range is too much for you to handle (forcing you to initiate some resend protocol). Once you get 1000, your upper limit will go to 1032. Then once you get your lower limit counter of 1001, your upper limit will be 1033, and so on. What you end up with is a sliding window.
If such a scenario is acceptable, then all you have to check is that your incoming counter ID is within your accepted window. This will be a subset of the 16 bit counter, so you can test …
incomingCounterID – lowerLimitCount < windowSize
As long as you are dealing with unsigned variables, you should be fine.
Hope this helps (and makes sense).