pub struct Service { /* private fields */ }
Expand description
Struct representing a ZeroConf
service. This should be created with all
the information that should be associated with the service and then the
publish
method can be used to register the service.
The ServiceRef
returned from publish
should be held
for as long as the service should continue being advertised, once dropped
the service will be deallocated.
§Examples
Normally the default values of domain
, host
and interface
don’t need
to be changed.
let service_ref = async_zeroconf::Service::new("Server", "_http._tcp", 80)
.publish().await?;
// Service kept alive until service_ref dropped
These fields can be customised if required. More details are available in
the DNSServiceRegister
documentation.
let service_ref = async_zeroconf::Service::new("Server", "_http._tcp", 80)
.set_domain("local".to_string())
.set_host("localhost".to_string())
.publish().await?;
// Service kept alive until service_ref dropped
Implementations§
Source§impl Service
impl Service
Sourcepub fn new(name: &str, service_type: &str, port: u16) -> Self
pub fn new(name: &str, service_type: &str, port: u16) -> Self
Create a new Service, called name
of type service_type
that is
listening on port port
.
This must then be published with Service::publish
to advertise the
service.
§Examples
// Create a service description
let service = async_zeroconf::Service::new("Web Server", "_http._tcp", 80);
Sourcepub fn new_with_txt(
name: &str,
service_type: &str,
port: u16,
txt: TxtRecord,
) -> Self
pub fn new_with_txt( name: &str, service_type: &str, port: u16, txt: TxtRecord, ) -> Self
Create a new Service, called name
of type service_type
that is
listening on port port
with the TXT records described by txt
.
This must then be published with Service::publish
to advertise the
service.
§Examples
// Create a TXT record collection
let mut txt = async_zeroconf::TxtRecord::new();
txt.add("version".to_string(), "0.1".to_string());
// Create a service description
let service = async_zeroconf::Service::new_with_txt("Web Server", "_http._tcp", 80, txt);
Sourcepub fn set_interface(&mut self, interface: Interface) -> &mut Self
pub fn set_interface(&mut self, interface: Interface) -> &mut Self
Set an interface to advertise the service on rather than all.
By default the service will be advertised on all interfaces.
Sourcepub fn prevent_rename(&mut self) -> &mut Self
pub fn prevent_rename(&mut self) -> &mut Self
Prevent renaming of this service if there is a name collision.
By default the service will be automatically renamed.
Sourcepub fn set_domain(&mut self, domain: String) -> &mut Self
pub fn set_domain(&mut self, domain: String) -> &mut Self
Set the (optional) domain for the service.
If not specified, the default domain is used.
Sourcepub fn set_host(&mut self, host: String) -> &mut Self
pub fn set_host(&mut self, host: String) -> &mut Self
Set the (optional) hostname for the service.
If not set, the hostname of the host will be used.
Sourcepub fn service_type(&self) -> &str
pub fn service_type(&self) -> &str
Get the type of the service
Sourcepub async fn publish(&self) -> Result<ServiceRef, ZeroconfError>
pub async fn publish(&self) -> Result<ServiceRef, ZeroconfError>
Publish the service, returns a ServiceRef
which should be held to
keep the service alive. Once the ServiceRef
is dropped the service
will be removed and deallocated.
§Arguments
allow_rename
- Allow the service to be automatically renamed if a service with the same name already exists
§Examples
// Create a service description
let service = async_zeroconf::Service::new("Server", "_http._tcp", 80);
// Publish the service
let service_ref = service.publish().await?;
// Service kept alive until service_ref dropped
Sourcepub fn publish_task(
&self,
) -> Result<(ServiceRef, impl ProcessTask, impl Future<Output = Result<(), ZeroconfError>>), ZeroconfError>
pub fn publish_task( &self, ) -> Result<(ServiceRef, impl ProcessTask, impl Future<Output = Result<(), ZeroconfError>>), ZeroconfError>
Publish the service, returns a ServiceRef
which should be held to
keep the service alive and a future which should be awaited on to
respond to any events associated with keeping the service registered.
Once the ServiceRef
is dropped the service will be removed and
deallocated.
§Note
This method is intended if more control is needed over how the task
is spawned. Service::publish
will automatically spawn the task.
The task should be spawned first to process events, and then the
returned future waited on to collect any errors that occurred.
§Examples
// Create a service description
let service = async_zeroconf::Service::new("Server", "_http._tcp", 80);
// Publish the service
let (service_ref, task, service_ok) = service.publish_task()?;
// Spawn the task to respond to events
tokio::spawn(task);
// Wait to confirm service started ok
service_ok.await?;
// Service kept alive until service_ref dropped