Properly implement TEventLoop::poll for AvahiEventLoop
As of Avahi 0.7, the sleep_time arg passed to `avahi_simple_poll_iterate()` is utilized. Properly implementing this reduces CPU spent polling while improving responsiveness.
This commit is contained in:
parent
886560528e
commit
401b8b5759
|
@ -3,6 +3,7 @@
|
||||||
use super::poll::ManagedAvahiSimplePoll;
|
use super::poll::ManagedAvahiSimplePoll;
|
||||||
use crate::event_loop::TEventLoop;
|
use crate::event_loop::TEventLoop;
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
|
use std::convert::TryInto;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -16,10 +17,9 @@ pub struct AvahiEventLoop<'a> {
|
||||||
impl<'a> TEventLoop for AvahiEventLoop<'a> {
|
impl<'a> TEventLoop for AvahiEventLoop<'a> {
|
||||||
/// Polls for new events.
|
/// Polls for new events.
|
||||||
///
|
///
|
||||||
/// Internally calls `ManagedAvahiSimplePoll::iterate(0)`, the `timeout` parameter does not
|
/// The `timeout` parameter defines the maximum time to sleep, but it will return earlier if
|
||||||
/// currently do anything in the Avahi implementation.
|
/// an event occurs.
|
||||||
fn poll(&self, _timeout: Duration) -> Result<()> {
|
fn poll(&self, timeout: Duration) -> Result<()> {
|
||||||
self.poll.iterate(0);
|
self.poll.iterate(timeout.as_millis().try_into().unwrap_or(-1))
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,8 +38,15 @@ impl ManagedAvahiSimplePoll {
|
||||||
/// Delegate function for [`avahi_simple_poll_iterate()`].
|
/// Delegate function for [`avahi_simple_poll_iterate()`].
|
||||||
///
|
///
|
||||||
/// [`avahi_simple_poll_iterate()`]: https://avahi.org/doxygen/html/simple-watch_8h.html#ad5b7c9d3b7a6584d609241ee6f472a2e
|
/// [`avahi_simple_poll_iterate()`]: https://avahi.org/doxygen/html/simple-watch_8h.html#ad5b7c9d3b7a6584d609241ee6f472a2e
|
||||||
pub fn iterate(&self, sleep_time: i32) {
|
pub fn iterate(&self, sleep_time: i32) -> Result<()> {
|
||||||
unsafe { avahi_simple_poll_iterate(self.0, sleep_time) };
|
let err = unsafe { avahi_simple_poll_iterate(self.0, sleep_time) };
|
||||||
|
if err < 0 {
|
||||||
|
avahi!(err, "AvahiSimplePoll poll failed")
|
||||||
|
} else if err > 0 {
|
||||||
|
Err("AvahiSimplePoll requested quit".into())
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn inner(&self) -> *mut AvahiSimplePoll {
|
pub(super) fn inner(&self) -> *mut AvahiSimplePoll {
|
||||||
|
|
Loading…
Reference in New Issue