Closed edgarogh closed 3 years ago
In case some is in the same situation as me, the fix is to capture the service inside the closure's internal structure using a dummy expression statement (it must be a borrow if we want poll
to be callable multiple times):
// fn create_service [...]
let poll = move || {
&service; // <-- THIS
event_loop.poll(Duration::from_secs(0)).unwrap()
};
return poll;
}
This way, the service's drop()
is bound to the closure's drop()
.
If you're not using a closure, return an explicit structure containing the service and the event loop.
Thank you for bringing this to my attention, this is no longer possible with 0.7.0
. The lifetime of EventLoop
has been bound to the underlying poll struct for both Avahi and Bonjour so attempting to do something like this now will fail to compile. Here's a working example of your code:
fn main() {
let mut service = create_service(1337);
let event_loop = service.register().unwrap();
let poll = move || event_loop.poll(Duration::from_secs(0)).unwrap();
loop {
poll();
}
}
fn create_service(port: u16) -> MdnsService {
let mut service = MdnsService::new("_myapp._tcp", port);
service.set_registered_callback(Box::new(|_, _| println!("Registered")));
service
}
Great, thanks for the quick reply & update ! Have a great day/evening/night.
No problem, you too!
OS: Linux (Mint) Zeroconf version: 0.6.3
If an EventLoop is created from an AvahiMdnsService and the service is dropped, polling the event loop doesn't seem to do anything (it doesn't crash though) and the
on_service_registered
callback is never invoked.MWE: