Cancellable timers: sorted_list::remove, timer cancel, replace done flag with timer cancellation
This commit is contained in:
@@ -30,27 +30,24 @@ static void test_ping(const responder& resp, ipv4::ip4_addr dst_ip) {
|
||||
|
||||
ipv4::ip4_addr our_ip = ns.ip;
|
||||
|
||||
auto done = std::make_shared<bool>(false);
|
||||
auto timer = std::make_shared<timer_handle>(nullptr);
|
||||
auto cb = std::make_shared<std::function<void(std::span<const uint8_t>)>>();
|
||||
*cb = [resp, ping_id, our_ip, done, cb](std::span<const uint8_t> frame) {
|
||||
if (*done) return;
|
||||
*cb = [resp, ping_id, our_ip, timer, cb](std::span<const uint8_t> frame) {
|
||||
ipv4::ip4_addr src_ip;
|
||||
if (!icmp::parse_echo_reply(frame, src_ip, ping_id)) {
|
||||
net_add_frame_callback(*cb);
|
||||
return;
|
||||
}
|
||||
dispatch_cancel_timer(*timer);
|
||||
if (src_ip == our_ip) {
|
||||
net_add_frame_callback(*cb);
|
||||
resp.respond(ResponseTest{false, {"got reply from self: " + ipv4::to_string(src_ip)}});
|
||||
return;
|
||||
}
|
||||
*done = true;
|
||||
resp.respond(ResponseTest{true, {"reply from " + ipv4::to_string(src_ip)}});
|
||||
};
|
||||
net_add_frame_callback(*cb);
|
||||
|
||||
dispatch_schedule_ms(5000, [resp, done]() {
|
||||
if (*done) return;
|
||||
*done = true;
|
||||
*timer = dispatch_schedule_ms(5000, [resp]() {
|
||||
resp.respond(ResponseTest{false, {"no reply from non-self host within 5s"}});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user