Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,16 @@ keywords = ["asynchronous", "routing"]
[dependencies]
libc = "0.2.66"

[target.'cfg(unix)'.dependencies]
async-io = "1.2.0"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.8", features = ["netioapi", "ntdef", "ws2def"] }
futures-lite = "1.11.2"
if-addrs = "0.6.5"
winapi = { version = "0.3.8", features = ["netioapi", "ntdef", "winerror", "ws2def"] }

[target.'cfg(windows)'.dev-dependencies]
winapi = { version = "0.3.8", features = ["processthreadsapi", "synchapi"] }

[dev-dependencies]
futures-lite = "1.11.2"
31 changes: 0 additions & 31 deletions bindgen.sh

This file was deleted.

10 changes: 10 additions & 0 deletions examples/ip_watch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use ip_watch::AddrSet;

fn main() {
futures_lite::future::block_on(async {
let mut set = AddrSet::new().await.unwrap();
loop {
println!("Got event {:?}", set.next().await);
}
});
}
37 changes: 29 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! limited to synchronous operation.

#![deny(
exceeding_bitshifts,
arithmetic_overflow,
invalid_type_param_default,
missing_fragment_specifier,
mutable_transmutes,
Expand Down Expand Up @@ -72,15 +72,36 @@ pub enum Event {
Delete(std::net::IpAddr),
}

#[cfg(all(test, not(windows)))]
#[cfg(test)]
mod tests {
use super::*;
use futures_lite::future::poll_fn;
use std::{future::Future, pin::Pin, task::Poll};

#[test]
fn test_ip_watch() {
futures_lite::future::block_on(async {
let mut set = AddrSet::new().await.unwrap();
poll_fn(|cx| loop {
let next = set.next();
futures_lite::pin!(next);
if let Poll::Ready(Ok(ev)) = Pin::new(&mut next).poll(cx) {
println!("Got event {:?}", ev);
continue
}
return Poll::Ready(())
})
.await;
});
}

#[test]
fn it_works() {
let set = AddrSet::new();
println!("Got event {:?}", set);
for i in set.unwrap() {
println!("Got event {:?}", i.unwrap())
}
fn test_is_send() {
futures_lite::future::block_on(async {
fn is_send<T: Send>(_: T) {}
is_send(AddrSet::new());
is_send(AddrSet::new().await.unwrap());
is_send(AddrSet::new().await.unwrap().next());
});
}
}
21 changes: 9 additions & 12 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,48 +44,45 @@ pub struct AddrSet {

impl AddrSet {
/// Create a watcher
pub fn new() -> std::io::Result<Self> {
pub async fn new() -> std::io::Result<Self> {
let mut hash = HashSet::new();
let mut watcher = Watcher::new()?;
let mut buf = Vec::with_capacity(1 << 16);
let mut queue = VecDeque::new();
watcher.resync(&mut buf, &mut queue, &mut hash)?;
watcher.resync(&mut buf, &mut queue, &mut hash).await?;
Ok(Self {
hash,
watcher,
buf,
queue,
})
}
}

impl Iterator for AddrSet {
type Item = std::io::Result<Event>;

fn next(&mut self) -> Option<Self::Item> {
/// Returns a future for the next event.
pub async fn next(&mut self) -> std::io::Result<Event> {
let Self {
watcher,
buf,
hash,
queue,
} = self;
if let Some(event) = queue.pop_front() {
return Some(Ok(event))
return Ok(event)
}
loop {
match watcher.next(buf, queue, hash) {
Status::IO(e) => return Some(Err(e)),
match watcher.next(buf, queue, hash).await {
Status::IO(e) => return Err(e),
Status::Desync => {
if buf.capacity() < 1 << 19 {
buf.reserve(buf.capacity() * 2);
}
if watcher.resync(buf, queue, hash).is_err() {
if watcher.resync(buf, queue, hash).await.is_err() {
continue
}
}
Status::Data(()) => {
if let Some(event) = queue.pop_front() {
return Some(Ok(event))
return Ok(event)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Fd {
#[cfg(target_os = "linux")]
let fd = unsafe { socket(libc::PF_NETLINK, FLAGS, libc::NETLINK_ROUTE) };
#[cfg(not(target_os = "linux"))]
let fd = unsafe { socket(libc::PF_ROUTE, FLAGS | libc::SOCK_NONBLOCK, libc::AF_UNSPEC) };
let fd = unsafe { socket(libc::PF_ROUTE, FLAGS, libc::AF_UNSPEC) };
if fd < 0 {
Err(std::io::Error::last_os_error())
} else {
Expand Down
Loading