Skip to content

Commit

Permalink
Tentatively embrace async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
ashthespy authored and willstott101 committed Apr 12, 2020
1 parent 3761cf9 commit c129851
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ tokio = {version = "0.2", features = ["sync","udp","stream","rt-core"]}
quick-error = "1.2"

[dev-dependencies]
env_logger = "0.5"
env_logger = {version = "0.7", default-features = false, features = ["termcolor","humantime","atty"]}
4 changes: 3 additions & 1 deletion examples/register.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub fn main() {
env_logger::init();
let mut builder = env_logger::Builder::new();
builder.parse_filters("libmdns=debug");
builder.init();

let responder = libmdns::Responder::new().unwrap();
let _svc = responder.register(
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
edition = "2018"
16 changes: 5 additions & 11 deletions src/fsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
task::{Context, Poll},
};

use tokio::{net::UdpSocket, runtime::Handle, stream::Stream, sync::mpsc};
use tokio::{net::UdpSocket, stream::Stream, sync::mpsc};

use super::{DEFAULT_TTL, MDNS_PORT};
use crate::address_family::AddressFamily;
Expand All @@ -39,16 +39,10 @@ pub struct FSM<AF: AddressFamily> {
}

impl<AF: AddressFamily> FSM<AF> {
pub fn new(
handle: &Handle,
services: &Services,
) -> io::Result<(FSM<AF>, mpsc::UnboundedSender<Command>)> {
let socket = handle.enter(move || {
let std_socket = AF::bind().unwrap();
let socket = UdpSocket::from_std(std_socket).unwrap();

socket
});
// Will panic if called from outside the context of a runtime
pub fn new(services: &Services) -> io::Result<(FSM<AF>, mpsc::UnboundedSender<Command>)> {
let std_socket = AF::bind()?;
let socket = UdpSocket::from_std(std_socket)?;

let (tx, rx) = mpsc::unbounded_channel();

Expand Down
39 changes: 12 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,29 @@ pub struct Service {
type ResponderTask = Box<dyn Future<Output = ()> + Send + Unpin>;

impl Responder {
fn setup_core() -> io::Result<(Runtime, ResponderTask, Responder)> {
let rt = Runtime::new().unwrap();
let handle = rt.handle();
let (responder, task) = Self::with_handle(handle)?;
Ok((rt, task, responder))
}

pub fn new() -> io::Result<Responder> {
let (tx, rx) = std::sync::mpsc::sync_channel(0);
thread::Builder::new()
.name("mdns-responder".to_owned())
.spawn(move || match Self::setup_core() {
Ok((mut core, task, responder)) => {
.spawn(move || {
let mut rt = Runtime::new().unwrap();
rt.block_on(async {
let (responder, task) = Self::with_default_handle()?;
tx.send(Ok(responder)).expect("tx responder channel closed");
core.block_on(task);
}
Err(err) => {
tx.send(Err(err)).expect("tx responder channel closed");
}
task.await;
Ok::<(), io::Error>(())
})
})?;

rx.recv().expect("rx responder channel closed")
}

pub fn spawn(handle: &Handle) -> io::Result<Responder> {
let (responder, task) = Self::with_handle(handle)?;
handle.spawn(
task,
// .map_err(|e| {
// warn!("mdns error {:?}", e);
// ()
// })
);
// .unwrap();
let (responder, task) = Self::with_default_handle()?;
handle.spawn(task);
Ok(responder)
}

pub fn with_handle(handle: &Handle) -> io::Result<(Responder, ResponderTask)> {
pub fn with_default_handle() -> io::Result<(Responder, ResponderTask)> {
let mut hostname = match hostname::get() {
Ok(s) => match s.into_string() {
Ok(s) => s,
Expand All @@ -98,8 +83,8 @@ impl Responder {

let services = Arc::new(RwLock::new(ServicesInner::new(hostname)));

let v4 = FSM::<Inet>::new(&handle, &services);
let v6 = FSM::<Inet6>::new(&handle, &services);
let v4 = FSM::<Inet>::new(&services);
let v6 = FSM::<Inet6>::new(&services);

let (task, commands): (ResponderTask, _) = match (v4, v6) {
(Ok((v4_task, v4_command)), Ok((v6_task, v6_command))) => {
Expand Down

0 comments on commit c129851

Please sign in to comment.