attempt removing option
This commit is contained in:
parent
7ae47cb1d8
commit
6e68353a93
1 changed files with 28 additions and 48 deletions
|
@ -53,7 +53,7 @@ const MAX_QUERIES: usize = 2;
|
|||
pub struct StackResources<const SOCK: usize> {
|
||||
sockets: [SocketStorage<'static>; SOCK],
|
||||
#[cfg(feature = "dns")]
|
||||
queries: Option<[Option<dns::DnsQuery>; MAX_QUERIES]>,
|
||||
queries: [Option<dns::DnsQuery>; MAX_QUERIES],
|
||||
}
|
||||
|
||||
impl<const SOCK: usize> StackResources<SOCK> {
|
||||
|
@ -63,7 +63,7 @@ impl<const SOCK: usize> StackResources<SOCK> {
|
|||
Self {
|
||||
sockets: [SocketStorage::EMPTY; SOCK],
|
||||
#[cfg(feature = "dns")]
|
||||
queries: Some([INIT; MAX_QUERIES]),
|
||||
queries: [INIT; MAX_QUERIES],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ struct Inner<D: Driver> {
|
|||
#[cfg(feature = "dhcpv4")]
|
||||
dhcp_socket: Option<SocketHandle>,
|
||||
#[cfg(feature = "dns")]
|
||||
dns_socket: Option<SocketHandle>,
|
||||
dns_socket: SocketHandle,
|
||||
}
|
||||
|
||||
pub(crate) struct SocketStack {
|
||||
|
@ -156,15 +156,7 @@ impl<D: Driver + 'static> Stack<D> {
|
|||
|
||||
let next_local_port = (random_seed % (LOCAL_PORT_MAX - LOCAL_PORT_MIN) as u64) as u16 + LOCAL_PORT_MIN;
|
||||
|
||||
let mut inner = Inner {
|
||||
device,
|
||||
link_up: false,
|
||||
config: None,
|
||||
#[cfg(feature = "dhcpv4")]
|
||||
dhcp_socket: None,
|
||||
#[cfg(feature = "dns")]
|
||||
dns_socket: None,
|
||||
};
|
||||
|
||||
let mut socket = SocketStack {
|
||||
sockets,
|
||||
iface,
|
||||
|
@ -172,12 +164,15 @@ impl<D: Driver + 'static> Stack<D> {
|
|||
next_local_port,
|
||||
};
|
||||
|
||||
#[cfg(feature = "dns")]
|
||||
{
|
||||
if let Some(queries) = resources.queries.take() {
|
||||
inner.dns_socket = Some(socket.sockets.add(dns::Socket::new(&[], queries)));
|
||||
}
|
||||
}
|
||||
let mut inner = Inner {
|
||||
device,
|
||||
link_up: false,
|
||||
config: None,
|
||||
#[cfg(feature = "dhcpv4")]
|
||||
dhcp_socket: None,
|
||||
#[cfg(feature = "dns")]
|
||||
dns_socket: socket.sockets.add(dns::Socket::new(&[], &mut resources.queries)),
|
||||
};
|
||||
|
||||
match config {
|
||||
Config::Static(config) => {
|
||||
|
@ -235,42 +230,29 @@ impl<D: Driver + 'static> Stack<D> {
|
|||
#[cfg(feature = "dns")]
|
||||
pub async fn dns_query(&self, name: &str, qtype: dns::DnsQueryType) -> Result<Vec<IpAddress, 1>, dns::Error> {
|
||||
let query = self.with_mut(|s, i| {
|
||||
if let Some(dns_handle) = i.dns_socket {
|
||||
let socket = s.sockets.get_mut::<dns::Socket>(dns_handle);
|
||||
match socket.start_query(s.iface.context(), name, qtype) {
|
||||
Ok(handle) => Ok(handle),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
} else {
|
||||
Err(dns::Error::Failed)
|
||||
}
|
||||
let socket = s.sockets.get_mut::<dns::Socket>(i.dns_socket);
|
||||
socket.start_query(s.iface.context(), name, qtype)
|
||||
})?;
|
||||
|
||||
use embassy_hal_common::drop::OnDrop;
|
||||
let drop = OnDrop::new(|| {
|
||||
self.with_mut(|s, i| {
|
||||
if let Some(dns_handle) = i.dns_socket {
|
||||
let socket = s.sockets.get_mut::<dns::Socket>(dns_handle);
|
||||
socket.cancel_query(query);
|
||||
s.waker.wake();
|
||||
}
|
||||
let socket = s.sockets.get_mut::<dns::Socket>(i.dns_socket);
|
||||
socket.cancel_query(query);
|
||||
s.waker.wake();
|
||||
})
|
||||
});
|
||||
|
||||
let res = poll_fn(|cx| {
|
||||
self.with_mut(|s, i| {
|
||||
if let Some(dns_handle) = i.dns_socket {
|
||||
let socket = s.sockets.get_mut::<dns::Socket>(dns_handle);
|
||||
match socket.get_query_result(query) {
|
||||
Ok(addrs) => Poll::Ready(Ok(addrs)),
|
||||
Err(dns::GetQueryResultError::Pending) => {
|
||||
socket.register_query_waker(query, cx.waker());
|
||||
Poll::Pending
|
||||
}
|
||||
Err(e) => Poll::Ready(Err(e.into())),
|
||||
let socket = s.sockets.get_mut::<dns::Socket>(i.dns_socket);
|
||||
match socket.get_query_result(query) {
|
||||
Ok(addrs) => Poll::Ready(Ok(addrs)),
|
||||
Err(dns::GetQueryResultError::Pending) => {
|
||||
socket.register_query_waker(query, cx.waker());
|
||||
Poll::Pending
|
||||
}
|
||||
} else {
|
||||
Poll::Ready(Err(dns::Error::Failed))
|
||||
Err(e) => Poll::Ready(Err(e.into())),
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -322,11 +304,9 @@ impl<D: Driver + 'static> Inner<D> {
|
|||
}
|
||||
|
||||
#[cfg(feature = "dns")]
|
||||
if let Some(dns_socket) = self.dns_socket {
|
||||
let socket = s.sockets.get_mut::<smoltcp::socket::dns::Socket>(dns_socket);
|
||||
let servers: Vec<IpAddress, 3> = config.dns_servers.iter().map(|c| IpAddress::Ipv4(*c)).collect();
|
||||
socket.update_servers(&servers[..]);
|
||||
}
|
||||
let socket = s.sockets.get_mut::<smoltcp::socket::dns::Socket>(self.dns_socket);
|
||||
let servers: Vec<IpAddress, 3> = config.dns_servers.iter().map(|c| IpAddress::Ipv4(*c)).collect();
|
||||
socket.update_servers(&servers[..]);
|
||||
|
||||
self.config = Some(config)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue