wip
This commit is contained in:
parent
b37086e8f6
commit
1a13b77295
20 changed files with 767 additions and 468 deletions
|
|
@ -3,7 +3,11 @@ name = "tun-async"
|
|||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
default = ["tokio"]
|
||||
tokio = ["dep:tokio"]
|
||||
|
||||
[dependencies]
|
||||
tun = { version = "0.1", path = "../tun" }
|
||||
fehler = "1.0"
|
||||
tun = { path = "../tun" }
|
||||
tokio = { version = "1.0", features = ["net"], optional = true }
|
||||
|
|
|
|||
|
|
@ -1,14 +1,5 @@
|
|||
pub fn add(left: usize, right: usize) -> usize {
|
||||
left + right
|
||||
}
|
||||
#[cfg(feature = "tokio")]
|
||||
#[path = "tokio/mod.rs"]
|
||||
pub(crate) mod imp;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
}
|
||||
}
|
||||
pub use imp::TunQueue;
|
||||
|
|
|
|||
29
tun-async/src/tokio/mod.rs
Normal file
29
tun-async/src/tokio/mod.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use fehler::throws;
|
||||
use std::{
|
||||
io::{self, Error},
|
||||
mem::MaybeUninit,
|
||||
};
|
||||
use tokio::io::unix::AsyncFd;
|
||||
|
||||
pub struct TunQueue {
|
||||
io: AsyncFd<tun::TunQueue>,
|
||||
}
|
||||
|
||||
impl TunQueue {
|
||||
#[throws]
|
||||
pub fn from_queue(queue: tun::TunQueue) -> Self {
|
||||
Self {
|
||||
io: AsyncFd::new(queue)?,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn try_recv(&self, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {
|
||||
loop {
|
||||
let mut guard = self.io.readable().await?;
|
||||
match guard.try_io(|inner| inner.get_ref().recv(buf)) {
|
||||
Ok(result) => return result,
|
||||
Err(..) => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue