Handle cancellation

This commit is contained in:
Ulf Lilleengen 2023-02-06 20:18:12 +01:00
parent 9cfea693ed
commit c203cefe01
Failed to extract signature
2 changed files with 15 additions and 2 deletions

View file

@ -40,6 +40,7 @@ smoltcp = { version = "0.9.0", default-features = false, features = [
]}
embassy-net-driver = { version = "0.1.0", path = "../embassy-net-driver" }
embassy-hal-common = { version = "0.1.0", path = "../embassy-hal-common" }
embassy-time = { version = "0.1.0", path = "../embassy-time" }
embassy-sync = { version = "0.1.0", path = "../embassy-sync" }
embedded-io = { version = "0.4.0", optional = true }

View file

@ -11,6 +11,7 @@ use smoltcp::iface::{Interface, SocketHandle};
pub use smoltcp::socket::dns::DnsQuery;
use smoltcp::socket::dns::{self, GetQueryResultError, StartQueryError, MAX_ADDRESS_COUNT};
pub use smoltcp::wire::{DnsQueryType, IpAddress};
use embassy_hal_common::drop::OnDrop;
use crate::{SocketStack, Stack};
@ -93,7 +94,15 @@ impl<'a> DnsSocket<'a> {
Err(e) => return Err(e.into()),
};
poll_fn(|cx| {
let handle = self.handle;
let drop = OnDrop::new(|| {
let s = &mut *self.stack.borrow_mut();
let socket = s.sockets.get_mut::<dns::Socket>(handle);
socket.cancel_query(query);
s.waker.wake();
});
let res = poll_fn(|cx| {
self.with_mut(|s, _| match s.get_query_result(query) {
Ok(addrs) => Poll::Ready(Ok(addrs)),
Err(GetQueryResultError::Pending) => {
@ -103,7 +112,10 @@ impl<'a> DnsSocket<'a> {
Err(e) => Poll::Ready(Err(e.into())),
})
})
.await
.await;
drop.defuse();
res
}
}