@@ -29,7 +29,8 @@ use crate::address_family::{Inet, Inet6};
2929use crate :: fsm:: { Command , FSM } ;
3030use crate :: services:: { ServiceData , Services , ServicesInner } ;
3131
32- const DEFAULT_TTL : u32 = 60 ;
32+ /// The default TTL for announced mDNS Services.
33+ pub const DEFAULT_TTL : u32 = 60 ;
3334const MDNS_PORT : u16 = 5353 ;
3435
3536pub struct Responder {
@@ -262,7 +263,7 @@ impl Responder {
262263}
263264
264265impl Responder {
265- /// Register a service to be advertised by the ` Responder` . The service is unregistered on
266+ /// Register a service to be advertised by the Responder with the [`DEFAULT_TTL`] . The service is unregistered on
266267 /// drop.
267268 ///
268269 /// # Example
@@ -289,6 +290,50 @@ impl Responder {
289290 /// If the TXT records are longer than 255 bytes, this will panic.
290291 #[ must_use]
291292 pub fn register ( & self , svc_type : & str , svc_name : & str , port : u16 , txt : & [ & str ] ) -> Service {
293+ self . register_with_ttl ( svc_type, svc_name, port, txt, DEFAULT_TTL )
294+ }
295+
296+ /// Register a service to be advertised by the Responder. With a custom TTL in seconds. The service is unregistered on
297+ /// drop.
298+ ///
299+ /// You may prefer to use this over [`Responder::register`] if you know your service will be short-lived and want clients to respond
300+ /// to it dissapearing more quickly (lower TTL), or if you find your service is very infrequently down and want to reduce
301+ /// network traffic (higher TTL).
302+ ///
303+ /// This becomes more important whilst waiting for <https://github.com/librespot-org/libmdns/issues/27> to be resolved.
304+ ///
305+ /// # example
306+ ///
307+ /// ```no_run
308+ /// use libmdns::Responder;
309+ ///
310+ /// # use std::io;
311+ /// # fn main() -> io::Result<()> {
312+ /// let responder = Responder::new()?;
313+ /// // bind service
314+ /// let _http_svc = responder.register_with_ttl(
315+ /// "_http._tcp".into(),
316+ /// "my really unreliable and short-lived http server".into(),
317+ /// 80,
318+ /// &["path=/"],
319+ /// 10 // mDNS clients are requested to re-check every 10 seconds for this HTTP server
320+ /// );
321+ /// # Ok(())
322+ /// # }
323+ /// ```
324+ ///
325+ /// # Panics
326+ ///
327+ /// If the TXT records are longer than 255 bytes, this will panic.
328+ #[ must_use]
329+ pub fn register_with_ttl (
330+ & self ,
331+ svc_type : & str ,
332+ svc_name : & str ,
333+ port : u16 ,
334+ txt : & [ & str ] ,
335+ ttl : u32 ,
336+ ) -> Service {
292337 let txt = if txt. is_empty ( ) {
293338 vec ! [ 0 ]
294339 } else {
@@ -315,7 +360,7 @@ impl Responder {
315360
316361 self . commands
317362 . borrow_mut ( )
318- . send_unsolicited ( svc. clone ( ) , DEFAULT_TTL , true ) ;
363+ . send_unsolicited ( svc. clone ( ) , ttl , true ) ;
319364
320365 let id = self . services . write ( ) . unwrap ( ) . register ( svc) ;
321366
0 commit comments