zeroconf/browser.rs
1//! Trait definition for cross-platform browser
2
3use crate::{EventLoop, NetworkInterface, Result, ServiceType, TxtRecord};
4use std::any::Any;
5use std::sync::Arc;
6
7/// Event from [`MdnsBrowser`] received by the `ServiceBrowserCallback`.
8///
9/// [`MdnsBrowser`]: type.MdnsBrowser.html
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum BrowserEvent {
12 Add(ServiceDiscovery),
13 Remove(ServiceRemoval),
14}
15
16/// Interface for interacting with underlying mDNS implementation service browsing capabilities.
17pub trait TMdnsBrowser {
18 /// Creates a new `MdnsBrowser` that browses for the specified `kind` (e.g. `_http._tcp`)
19 fn new(service_type: ServiceType) -> Self;
20
21 /// Sets the network interface on which to browse for services on.
22 ///
23 /// Most applications will want to use the default value `NetworkInterface::Unspec` to browse
24 /// on all available interfaces.
25 fn set_network_interface(&mut self, interface: NetworkInterface);
26
27 /// Returns the network interface on which to browse for services on.
28 fn network_interface(&self) -> NetworkInterface;
29
30 /// Sets the [`ServiceBrowserCallback`] that is invoked when the browser has discovered and
31 /// resolved or removed a service.
32 ///
33 /// [`ServiceBrowserCallback`]: ../type.ServiceBrowserCallback.html
34 fn set_service_callback(&mut self, service_callback: Box<ServiceBrowserCallback>);
35
36 /// Sets the optional user context to pass through to the callback. This is useful if you need
37 /// to share state between pre and post-callback. The context type must implement `Any`.
38 fn set_context(&mut self, context: Box<dyn Any + Send + Sync>);
39
40 /// Returns the optional user context to pass through to the callback.
41 fn context(&self) -> Option<&(dyn Any + Send + Sync)>;
42
43 /// Starts the browser. Returns an `EventLoop` which can be called to keep the browser alive.
44 fn browse_services(&mut self) -> Result<EventLoop>;
45}
46
47/// Callback invoked from [`MdnsBrowser`] once a service has been discovered and resolved or
48/// removed.
49///
50/// # Arguments
51/// * `browser_event` - The event received from Zeroconf
52/// * `context` - The optional user context passed through
53///
54/// [`MdnsBrowser`]: type.MdnsBrowser.html
55pub type ServiceBrowserCallback =
56 dyn Fn(Result<BrowserEvent>, Option<Arc<dyn Any + Send + Sync>>) + Send + Sync;
57
58/// Represents a service that has been discovered by a [`MdnsBrowser`].
59///
60/// [`MdnsBrowser`]: type.MdnsBrowser.html
61#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
62#[derive(Debug, Getters, Builder, BuilderDelegate, Clone, PartialEq, Eq)]
63pub struct ServiceDiscovery {
64 name: String,
65 service_type: ServiceType,
66 domain: String,
67 host_name: String,
68 address: String,
69 port: u16,
70 txt: Option<TxtRecord>,
71}
72
73/// Represents a service that has been removed by a [`MdnsBrowser`].
74///
75/// [`MdnsBrowser`]: type.MdnsBrowser.html
76#[derive(Debug, Getters, Builder, BuilderDelegate, Clone, PartialEq, Eq)]
77pub struct ServiceRemoval {
78 /// The "abc" part in "abc._http._udp.local"
79 name: String,
80 /// The "_http._udp" part in "abc._http._udp.local"
81 kind: String,
82 /// The "local" part in "abc._http._udp.local"
83 domain: String,
84}