Skip to content

Commit

Permalink
replace async-std with async-io/futures-lite
Browse files Browse the repository at this point in the history
  • Loading branch information
SludgePhD committed Apr 20, 2024
1 parent 6233dd8 commit d57fd30
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[workspace]
members = [".", "uwuhi-async"]
package.version = "0.4.1"
dependencies.uwuhi = { version = "0.4.1", path = "." }
# update these versions together ^

[package]
name = "uwuhi"
version.workspace = true
Expand All @@ -16,9 +22,3 @@ log = "0.4.16"
env_logger = "0.10.0"
if-addrs = "0.10.1"
expect-test = "1.4.1"

[workspace]
members = [".", "uwuhi-async"]
package.version = "0.4.0"
dependencies.uwuhi = { version = "0.4.0", path = "." }
# update these versions together ^
3 changes: 2 additions & 1 deletion uwuhi-async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ repository = "https://github.com/SludgePhD/uwuhi"
[dependencies]
uwuhi.workspace = true
log = "0.4.17"
async-std = "1.12.0"
async-io = "2.3.2"
futures-lite = "2.3.0"
19 changes: 12 additions & 7 deletions uwuhi-async/src/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
//! DNS name resolution.
use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
io,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket},
time::Duration,
};

use async_io::{Async, Timer};
use futures_lite::future;
pub use uwuhi::resolver::*;
use uwuhi::{name::DomainName, DNS_BUFFER_SIZE, MDNS_BUFFER_SIZE};

use async_std::{io, net::UdpSocket};

pub struct AsyncResolver {
servers: Vec<SocketAddr>,
sock: UdpSocket,
sock: Async<UdpSocket>,
ip_buf: Vec<IpAddr>,
is_multicast: bool,
timeout: Duration,
Expand All @@ -30,7 +31,7 @@ impl AsyncResolver {
};
Ok(Self {
servers: vec![server],
sock: UdpSocket::bind(bind_addr).await?,
sock: Async::<UdpSocket>::bind(bind_addr)?,
ip_buf: Vec::new(),
is_multicast: bind_addr.ip().is_multicast(),
timeout: Self::DEFAULT_TIMEOUT,
Expand Down Expand Up @@ -116,12 +117,16 @@ impl AsyncResolver {

// FIXME: retransmit
for addr in &self.servers {
self.sock.send_to(data, addr).await?;
self.sock.send_to(data, *addr).await?;
}

loop {
let mut recv_buf = [0; DNS_BUFFER_SIZE];
let (b, addr) = io::timeout(self.timeout, self.sock.recv_from(&mut recv_buf)).await?;
let timeout = async {
Timer::after(self.timeout).await;
Err(io::ErrorKind::TimedOut.into())
};
let (b, addr) = future::or(self.sock.recv_from(&mut recv_buf), timeout).await?;
let recv = &recv_buf[..b];
log::trace!("recv from {}: {:x?}", addr, recv);

Expand Down
7 changes: 4 additions & 3 deletions uwuhi-async/src/service/advertising.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Service advertising.
use std::net::UdpSocket;
use std::{io, net::IpAddr};

use async_std::net::UdpSocket;
use async_io::Async;
use uwuhi::{
name::Label,
service::{InstanceDetails, ServiceInstance},
Expand All @@ -14,7 +15,7 @@ pub use uwuhi::service::advertising::*;
/// Asynchronous mDNS service advertiser and name server.
pub struct AsyncAdvertiser {
adv: Advertiser,
sock: UdpSocket,
sock: Async<UdpSocket>,
}

impl AsyncAdvertiser {
Expand All @@ -25,7 +26,7 @@ impl AsyncAdvertiser {
pub fn new(hostname: Label, addr: IpAddr) -> io::Result<Self> {
let adv = Advertiser::new(hostname, addr)?;
Ok(Self {
sock: adv.create_socket()?.into(),
sock: Async::new(adv.create_socket()?)?,
adv,
})
}
Expand Down
23 changes: 12 additions & 11 deletions uwuhi-async/src/service/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
use std::{
collections::{btree_map::Entry, BTreeMap},
io,
net::{Ipv4Addr, Ipv6Addr, SocketAddr},
net::{Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket},
ops::ControlFlow,
time::{Duration, Instant},
};

use async_std::{future, net::UdpSocket};
use async_io::{Async, Timer};
use futures_lite::future;
use uwuhi::{
name::DomainName,
packet::{records::Record, QType},
Expand All @@ -19,7 +20,7 @@ use uwuhi::{
pub use uwuhi::service::discovery::*;

pub struct AsyncDiscoverer {
sock: UdpSocket,
sock: Async<UdpSocket>,
server: SocketAddr,
domain: DomainName,
retransmit_timeout: Duration,
Expand All @@ -39,7 +40,7 @@ impl AsyncDiscoverer {
(Ipv4Addr::UNSPECIFIED, 0).into()
};
Ok(Self {
sock: UdpSocket::bind(bind_addr).await?,
sock: Async::<UdpSocket>::bind(bind_addr)?,
server,
domain,
retransmit_timeout: Self::DEFAULT_RETRANSMIT_TIMEOUT,
Expand Down Expand Up @@ -242,15 +243,15 @@ impl AsyncDiscoverer {
}

let mut recv_buf = [0; MDNS_BUFFER_SIZE];
let (b, addr) = match future::timeout(
self.retransmit_timeout,
self.sock.recv_from(&mut recv_buf),
)
.await
{
let timeout = async {
Timer::after(self.retransmit_timeout).await;
Err(())
};
let recv = async { Ok(self.sock.recv_from(&mut recv_buf).await) };
let (b, addr) = match future::or(recv, timeout).await {
Ok(Ok(res)) => res,
Err(_) => continue 'retransmit,
Ok(Err(e)) => return Err(e),
Err(()) => continue 'retransmit,
};
let recv = &recv_buf[..b];
log::trace!("recv from {}: {}", addr, recv.escape_ascii());
Expand Down

0 comments on commit d57fd30

Please sign in to comment.