2016-11-29 12:01 GMT+08:00 Gavin Lambert <gavinl@compacsort.com>:
On 29/11/2016 16:26, TONGARI J wrote:
The problem only happens if you have previous timeout.
You can call socket.cancel() in the timeout branch.

I think the problem is that, if you don't call socket.cancel() on
timeout, in the next round you're calling async_receive_from before the
previous call completes, which is not allowed.

Note that even if you call cancel(), you still aren't allowed to immediately call async_receive_from() again.

You need to cancel *and then* wait without timeout for the future to be ready before you can make another request.  This should be fast (since you're just waiting for the cancellation to complete) but may not be zero time.

(If you were using completion handlers rather than futures, this is equivalent to waiting for the handler to be called with a cancellation error before making another request.)

I think we're wrong about whether it's allowed to call async_receive_from() multiple times before it completes.
It was from my impression that we can't do that, but skimming over the ASIO docs I found nothing says that.
Experiments show that it works, so I think my argument in this regard was wrong, at least ASIO itself does not enforce the restriction.

So calling socket.cancel() w/o waiting the cancellation to complete should be OK.