[][src]Crate zeroconf

zeroconf is a cross-platform library that wraps underlying ZeroConf/mDNS implementations such as Bonjour or Avahi, providing an easy and idiomatic way to both register and browse services.

This crate provides the cross-platform MdnsService and MdnsBrowser available for each supported platform as well as platform-specific modules for lower-level access to the mDNS implementation should that be necessary.

Most users of this crate need only MdnsService and MdnsBrowser.

Examples

Register a service

When registering a service, you may optionally pass a "context" to pass state through the callback. The only requirement is that this context implements the Any trait, which most types will automatically. See MdnsService for more information about contexts.

use std::any::Any;
use std::sync::{Arc, Mutex};
use zeroconf::{MdnsService, ServiceRegistration};

#[derive(Default, Debug)]
pub struct Context {
    service_name: String,
}

fn main() {
    let mut service = MdnsService::new("_http._tcp", 8080);
    let context: Arc<Mutex<Context>> = Arc::default();

    service.set_registered_callback(Box::new(on_service_registered));
    service.set_context(Box::new(context));

    // blocks current thread, must keep-alive to keep service active
    service.start().unwrap();
}

fn on_service_registered(service: ServiceRegistration, context: Option<Arc<dyn Any>>) {
    println!("Service registered: {:?}", service);

    let context = context
        .as_ref()
        .unwrap()
        .downcast_ref::<Arc<Mutex<Context>>>()
        .unwrap()
        .clone();

    context.lock().unwrap().service_name = service.name().clone();

    println!("Context: {:?}", context);

    // ...
}

Browsing services

use std::any::Any;
use std::sync::Arc;
use zeroconf::{MdnsBrowser, ServiceDiscovery};

fn main() {
    let mut browser = MdnsBrowser::new("_http._tcp");

    browser.set_service_discovered_callback(Box::new(on_service_discovered));

    // blocks current thread, must keep-alive to keep browser active
    browser.start().unwrap()
}

fn on_service_discovered(service: ServiceDiscovery, _context: Option<Arc<dyn Any>>) {
    println!("Service discovered: {:?}", &service);

    // ...
}

Modules

builder

Provides builder related helpers

ffi

Utilities related to FFI bindings

linux

Linux-specific ZeroConf bindings

Structs

ServiceDiscovery

Represents a service that has been discovered by a MdnsBrowser.

ServiceDiscoveryBuilder

Builder for ServiceDiscovery.

ServiceRegistration

Represents a registration event for a MdnsService.

ServiceRegistrationBuilder

Builder for ServiceRegistration.

Type Definitions

MdnsBrowser

Type alias for the platform-specific mDNS browser implementation

MdnsService

Type alias for the platform-specific mDNS service implementation

ServiceDiscoveredCallback

Callback invoked from MdnsBrowser once a service has been discovered and resolved.

ServiceRegisteredCallback

Callback invoked from MdnsService once it has successfully registered.