Boost Async UDP Client
asynchronous, boost, boost-asio, sockets, udp
Solution
To avoid blocking in the `io_service::run()` you can use io_service::poll_one().
Regarding loosing UDP packets, I think you are out of luck. UDP does not guarantee delivery, and any part of the network may decide to drop UDP packets if there is much traffic. If you need to ensure delivery you need to have either implement some sort of flow control or just use TCP.
Problem
I've read through the boost:asio documentation (which appears silent on async clients), and looked through here, but can't seem to find the forest for the trees here. I've got a simulation that has a main loop that looks like this: ``` for(;;) { a = do_stuff1(); do_stuff2(a); } ``` Easy enough. What I'd like to do, is modify it so that I have: ``` for(;;) { a = do_stuff1(); check_for_new_received_udp_data(&b); modify_a_with_data_from_b(a,b); do_stuff2(a); } ``` Where I have the following requirements: - I cannot lose data just because I wasn't actively listening. IE I don't want to lose packets because I was in do_stuff2() instead of check_for_new_received_udp_data() at the time the server sent the packet. - I can't have check_for_new_received_udp_data() block for more than about 2ms, since the main for loop needs to execute at 60Hz. - The server will be running elsewhere, and has a completely erratic schedule. Sometimes there will be no data, othertimes I may get the same packet repeatedly. I've played with the async UDP, but that requires calling io_service.run(), which blocks indefinitely, so that doesn't really help me. I thought about timing out a blocking socket read, but it seems you have to cheat and get out of the boost calls to do that, so that's a non-starter. Is the answer going to involve threading? Either way, could someone kindly point me to an example that is somewhat similar? Surely this has been done before.