Signed-off-by: Walker Crouse <walker.crouse@coop.co.uk>
This commit is contained in:
Walker Crouse 2020-09-28 22:40:43 -04:00
parent 954e749b82
commit 7ee598aeb2
13 changed files with 30 additions and 18 deletions

View File

@ -3,6 +3,7 @@
use crate::{EventLoop, NetworkInterface, Result, ServiceDiscoveredCallback};
use std::any::Any;
/// Interface for interacting with underlying mDNS implementation service browsing capabilities.
pub trait TMdnsBrowser {
/// Creates a new `MdnsBrowser` that browses for the specified `kind` (e.g. `_http._tcp`)
fn new(kind: &str) -> Self;

View File

@ -0,0 +1,11 @@
//! Trait definition for cross-platform event loop
use crate::Result;
use std::time::Duration;
/// A handle on the underlying implementation to poll the event loop. Typically, `poll()`
/// is called in a loop to keep a `MdnsService` or `MdnsBrowser` running.
pub trait TEventLoop {
/// Polls for new events.
fn poll(&self, timeout: Duration) -> Result<()>;
}

View File

@ -1,3 +1,5 @@
//! Utilities related to c-string handling
use libc::c_char;
use std::ffi::{CStr, CString};

View File

@ -142,6 +142,7 @@ mod txt_record;
pub mod browser;
pub mod builder;
pub mod error;
pub mod event_loop;
pub mod ffi;
pub mod prelude;
pub mod service;

View File

@ -1,3 +1,5 @@
//! Avahi implementation for cross-platform browser
use super::avahi_util;
use super::client::{ManagedAvahiClient, ManagedAvahiClientParams};
use super::constants;
@ -22,7 +24,6 @@ use std::ffi::CString;
use std::sync::Arc;
use std::{fmt, ptr};
/// Interface for interacting with Avahi's mDNS service browsing capabilities.
#[derive(Debug)]
pub struct AvahiMdnsBrowser {
client: Option<Arc<ManagedAvahiClient>>,

View File

@ -1,26 +1,22 @@
//! Event loop for running a `MdnsService` or `MdnsBrowser`.
use super::poll::ManagedAvahiSimplePoll;
use crate::event_loop::TEventLoop;
use crate::Result;
use std::sync::Arc;
use std::time::Duration;
/// A handle on the underlying Avahi implementation to poll the event loop. Typically `poll()` is
/// called in a loop to keep a [`MdnsService`] or [`MdnsBrowser`] running.
///
/// [`MdnsService`]: ../../type.MdnsService.html
/// [`MdnsBrowser`]: ../../type.MdnsBrowser.html
#[derive(new)]
pub struct AvahiEventLoop {
poll: Arc<ManagedAvahiSimplePoll>,
}
impl AvahiEventLoop {
impl TEventLoop for AvahiEventLoop {
/// Polls for new events.
///
/// Internally calls `ManagedAvahiSimplePoll::iterate(0)`, the `timeout` parameter does not
/// currently do anything in the Avahi implementation.
pub fn poll(&self, _timeout: Duration) -> Result<()> {
fn poll(&self, _timeout: Duration) -> Result<()> {
self.poll.iterate(0);
Ok(())
}

View File

@ -1,3 +1,5 @@
//! Avahi implementation for cross-platform service.
use super::avahi_util;
use super::client::{self, ManagedAvahiClient, ManagedAvahiClientParams};
use super::constants;
@ -19,7 +21,6 @@ use std::fmt::{self, Formatter};
use std::ptr;
use std::sync::Arc;
/// Interface for interacting with Avahi's mDNS service registration capabilities.
#[derive(Debug)]
pub struct AvahiMdnsService {
client: Option<ManagedAvahiClient>,

View File

@ -1,3 +1,5 @@
//! Avahi implementation for cross-platform TXT record.
use super::string_list::{AvahiStringListNode, ManagedAvahiStringList};
use crate::txt_record::TTxtRecord;
use crate::Result;

View File

@ -15,7 +15,6 @@ use std::fmt::{self, Formatter};
use std::ptr;
use std::sync::{Arc, Mutex};
/// Interface for interacting with Bonjour's mDNS service browsing capabilities.
#[derive(Debug)]
pub struct BonjourMdnsBrowser {
service: Arc<Mutex<ManagedDNSServiceRef>>,

View File

@ -1,27 +1,23 @@
//! Event loop for running a `MdnsService` or `MdnsBrowser`.
use super::service_ref::ManagedDNSServiceRef;
use crate::event_loop::TEventLoop;
use crate::{ffi, Result};
use std::sync::{Arc, Mutex};
use std::time::Duration;
/// A handle on the underlying Bonjour implementation to poll the event loop. Typically, `poll()`
/// is called in a loop to keep a [`MdnsService`] or [`MdnsBrowser`] running.
///
/// [`MdnsService`]: ../../type.MdnsService.html
/// [`MdnsBrowser`]: ../../type.MdnsBrowser.html
#[derive(new)]
pub struct BonjourEventLoop {
service: Arc<Mutex<ManagedDNSServiceRef>>,
}
impl BonjourEventLoop {
impl TEventLoop for BonjourEventLoop {
/// Polls for new events.
///
/// Prior to calling `ManagedDNSServiceRef::process_result()`, this function performs a unix
/// `select()` on the underlying socket with the specified timeout. If the socket contains no
/// new data, the blocking call is not made.
pub fn poll(&self, timeout: Duration) -> Result<()> {
fn poll(&self, timeout: Duration) -> Result<()> {
let service = self.service.lock().unwrap();
let select = unsafe { ffi::read_select(service.sock_fd(), timeout)? };
if select > 0 {

View File

@ -13,7 +13,6 @@ use std::any::Any;
use std::ffi::CString;
use std::sync::{Arc, Mutex};
/// Interface for interacting with Bonjour's mDNS service registration capabilities.
#[derive(Debug)]
pub struct BonjourMdnsService {
service: Arc<Mutex<ManagedDNSServiceRef>>,

View File

@ -1,5 +1,6 @@
//! Crate prelude
pub use crate::browser::TMdnsBrowser;
pub use crate::event_loop::TEventLoop;
pub use crate::service::TMdnsService;
pub use crate::txt_record::TTxtRecord;

View File

@ -3,6 +3,8 @@
use crate::{EventLoop, NetworkInterface, Result, ServiceRegisteredCallback, TxtRecord};
use std::any::Any;
/// Interface for interacting with underlying mDNS service implementation registration
/// capabilities.
pub trait TMdnsService {
/// Creates a new `MdnsService` with the specified `kind` (e.g. `_http._tcp`) and `port`.
fn new(kind: &str, port: u16) -> Self;