@@ -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 {
@@ -313,51 +358,14 @@ impl Responder {
313358 txt,
314359 } ;
315360
316- self . commands
317- . borrow_mut ( )
318- . send_unsolicited ( svc. clone ( ) , DEFAULT_TTL , true ) ;
319-
320- let id = self . services . write ( ) . unwrap ( ) . register ( svc) ;
321-
322- Service {
323- id,
324- commands : self . commands . borrow ( ) . clone ( ) ,
325- services : self . services . clone ( ) ,
326- _shutdown : self . shutdown . clone ( ) ,
327- }
328- }
329-
330- #[ must_use]
331- pub fn register_with_ttl ( & self , svc_type : String , svc_name : String , port : u16 , txt : & [ & str ] , ttl : u32 ) -> Service {
332- let txt = if txt. is_empty ( ) {
333- vec ! [ 0 ]
334- } else {
335- txt. iter ( )
336- . flat_map ( |entry| {
337- let entry = entry. as_bytes ( ) ;
338- if entry. len ( ) > 255 {
339- panic ! ( "{:?} is too long for a TXT record" , entry) ;
340- }
341- std:: iter:: once ( entry. len ( ) as u8 ) . chain ( entry. iter ( ) . cloned ( ) )
342- } )
343- . collect ( )
344- } ;
345-
346- let svc = ServiceData {
347- typ : Name :: from_str ( format ! ( "{}.local" , svc_type) ) . unwrap ( ) ,
348- name : Name :: from_str ( format ! ( "{}.{}.local" , svc_name, svc_type) ) . unwrap ( ) ,
349- port : port,
350- txt : txt,
351- } ;
352-
353361 self . commands
354362 . borrow_mut ( )
355363 . send_unsolicited ( svc. clone ( ) , ttl, true ) ;
356364
357365 let id = self . services . write ( ) . unwrap ( ) . register ( svc) ;
358366
359367 Service {
360- id : id ,
368+ id,
361369 commands : self . commands . borrow ( ) . clone ( ) ,
362370 services : self . services . clone ( ) ,
363371 _shutdown : self . shutdown . clone ( ) ,
0 commit comments