54
54
}
55
55
}
56
56
57
+ impl < T > Mdns for & T
58
+ where
59
+ T : Mdns ,
60
+ {
61
+ fn add (
62
+ & self ,
63
+ name : & str ,
64
+ service : & str ,
65
+ protocol : & str ,
66
+ port : u16 ,
67
+ service_subtypes : & [ & str ] ,
68
+ txt_kvs : & [ ( & str , & str ) ] ,
69
+ ) -> Result < ( ) , Error > {
70
+ ( * * self ) . add ( name, service, protocol, port, service_subtypes, txt_kvs)
71
+ }
72
+
73
+ fn remove ( & self , name : & str , service : & str , protocol : & str , port : u16 ) -> Result < ( ) , Error > {
74
+ ( * * self ) . remove ( name, service, protocol, port)
75
+ }
76
+ }
77
+
78
+ #[ cfg( all( feature = "std" , feature = "astro-dnssd" ) ) ]
79
+ pub type DefaultMdns = astro:: Mdns ;
80
+
81
+ #[ cfg( all( feature = "std" , feature = "astro-dnssd" ) ) ]
82
+ pub type DefaultMdnsRunner < ' a > = astro:: MdnsRunner < ' a > ;
83
+
84
+ #[ cfg( not( all( feature = "std" , feature = "astro-dnssd" ) ) ) ]
85
+ pub type DefaultMdns < ' a > = builtin:: Mdns < ' a > ;
86
+
87
+ #[ cfg( not( all( feature = "std" , feature = "astro-dnssd" ) ) ) ]
88
+ pub type DefaultMdnsRunner < ' a > = builtin:: MdnsRunner < ' a > ;
89
+
57
90
pub struct DummyMdns ;
58
91
59
92
impl Mdns for DummyMdns {
@@ -239,8 +272,8 @@ pub mod builtin {
239
272
240
273
const IP_BIND_ADDR : ( IpAddr , u16 ) = ( IpAddr :: V6 ( Ipv6Addr :: UNSPECIFIED ) , 5353 ) ;
241
274
242
- pub type MdnsTxBuf = MaybeUninit < [ u8 ; MAX_TX_BUF_SIZE ] > ;
243
- pub type MdnsRxBuf = MaybeUninit < [ u8 ; MAX_RX_BUF_SIZE ] > ;
275
+ type MdnsTxBuf = MaybeUninit < [ u8 ; MAX_TX_BUF_SIZE ] > ;
276
+ type MdnsRxBuf = MaybeUninit < [ u8 ; MAX_RX_BUF_SIZE ] > ;
244
277
245
278
#[ allow( clippy:: too_many_arguments) ]
246
279
pub fn create_record (
@@ -418,28 +451,6 @@ pub mod builtin {
418
451
}
419
452
}
420
453
421
- pub fn split ( & mut self ) -> ( MdnsApi < ' _ , ' a > , MdnsRunner < ' _ , ' a > ) {
422
- ( MdnsApi ( & * self ) , MdnsRunner ( & * self ) )
423
- }
424
-
425
- fn key (
426
- & self ,
427
- name : & str ,
428
- service : & str ,
429
- protocol : & str ,
430
- port : u16 ,
431
- ) -> heapless:: String < 64 > {
432
- let mut key = heapless:: String :: new ( ) ;
433
-
434
- write ! ( & mut key, "{name}.{service}.{protocol}.{port}" ) . unwrap ( ) ;
435
-
436
- key
437
- }
438
- }
439
-
440
- pub struct MdnsApi < ' a , ' b > ( & ' a Mdns < ' b > ) ;
441
-
442
- impl < ' a , ' b > MdnsApi < ' a , ' b > {
443
454
pub fn add (
444
455
& self ,
445
456
name : & str ,
@@ -454,9 +465,9 @@ pub mod builtin {
454
465
name, service, protocol, service_subtypes, port, txt_kvs
455
466
) ;
456
467
457
- let key = self . 0 . key ( name, service, protocol, port) ;
468
+ let key = self . key ( name, service, protocol, port) ;
458
469
459
- let mut entries = self . 0 . entries . borrow_mut ( ) ;
470
+ let mut entries = self . entries . borrow_mut ( ) ;
460
471
461
472
entries. retain ( |entry| entry. key != key) ;
462
473
entries
@@ -471,10 +482,10 @@ pub mod builtin {
471
482
. unwrap ( ) ;
472
483
473
484
match create_record (
474
- self . 0 . id ,
475
- self . 0 . hostname ,
476
- self . 0 . ip ,
477
- self . 0 . ipv6 ,
485
+ self . id ,
486
+ self . hostname ,
487
+ self . ip ,
488
+ self . ipv6 ,
478
489
60 , /*ttl_sec*/
479
490
name,
480
491
service,
@@ -491,7 +502,7 @@ pub mod builtin {
491
502
}
492
503
}
493
504
494
- self . 0 . notification . signal ( ( ) ) ;
505
+ self . notification . signal ( ( ) ) ;
495
506
496
507
Ok ( ( ) )
497
508
}
@@ -508,37 +519,57 @@ pub mod builtin {
508
519
name, service, protocol, port
509
520
) ;
510
521
511
- let key = self . 0 . key ( name, service, protocol, port) ;
522
+ let key = self . key ( name, service, protocol, port) ;
512
523
513
- let mut entries = self . 0 . entries . borrow_mut ( ) ;
524
+ let mut entries = self . entries . borrow_mut ( ) ;
514
525
515
526
let old_len = entries. len ( ) ;
516
527
517
528
entries. retain ( |entry| entry. key != key) ;
518
529
519
530
if entries. len ( ) != old_len {
520
- self . 0 . notification . signal ( ( ) ) ;
531
+ self . notification . signal ( ( ) ) ;
521
532
}
522
533
523
534
Ok ( ( ) )
524
535
}
536
+
537
+ fn key (
538
+ & self ,
539
+ name : & str ,
540
+ service : & str ,
541
+ protocol : & str ,
542
+ port : u16 ,
543
+ ) -> heapless:: String < 64 > {
544
+ let mut key = heapless:: String :: new ( ) ;
545
+
546
+ write ! ( & mut key, "{name}.{service}.{protocol}.{port}" ) . unwrap ( ) ;
547
+
548
+ key
549
+ }
525
550
}
526
551
527
- pub struct MdnsRunner < ' a , ' b > ( & ' a Mdns < ' b > ) ;
552
+ pub struct MdnsRunner < ' a > ( & ' a Mdns < ' a > ) ;
528
553
529
- impl < ' a , ' b > MdnsRunner < ' a , ' b > {
530
- pub async fn run_udp (
531
- & mut self ,
532
- tx_buf : & mut MdnsTxBuf ,
533
- rx_buf : & mut MdnsRxBuf ,
534
- ) -> Result < ( ) , Error > {
535
- let udp = UdpListener :: new ( SocketAddr :: new ( IP_BIND_ADDR . 0 , IP_BIND_ADDR . 1 ) ) . await ?;
554
+ impl < ' a > MdnsRunner < ' a > {
555
+ pub const fn new ( mdns : & ' a Mdns < ' a > ) -> Self {
556
+ Self ( mdns)
557
+ }
558
+
559
+ pub async fn run_udp ( & mut self ) -> Result < ( ) , Error > {
560
+ let mut tx_buf = MdnsTxBuf :: uninit ( ) ;
561
+ let mut rx_buf = MdnsRxBuf :: uninit ( ) ;
562
+
563
+ let tx_buf = & mut tx_buf;
564
+ let rx_buf = & mut rx_buf;
536
565
537
566
let tx_pipe = Pipe :: new ( unsafe { tx_buf. assume_init_mut ( ) } ) ;
538
567
let rx_pipe = Pipe :: new ( unsafe { rx_buf. assume_init_mut ( ) } ) ;
539
568
540
569
let tx_pipe = & tx_pipe;
541
570
let rx_pipe = & rx_pipe;
571
+
572
+ let udp = UdpListener :: new ( SocketAddr :: new ( IP_BIND_ADDR . 0 , IP_BIND_ADDR . 1 ) ) . await ?;
542
573
let udp = & udp;
543
574
544
575
let mut tx = pin ! ( async move {
@@ -584,7 +615,7 @@ pub mod builtin {
584
615
select3 ( & mut tx, & mut rx, & mut run) . await . unwrap ( )
585
616
}
586
617
587
- pub async fn run ( & mut self , tx_pipe : & Pipe < ' _ > , rx_pipe : & Pipe < ' _ > ) -> Result < ( ) , Error > {
618
+ pub async fn run ( & self , tx_pipe : & Pipe < ' _ > , rx_pipe : & Pipe < ' _ > ) -> Result < ( ) , Error > {
588
619
let mut broadcast = pin ! ( self . broadcast( tx_pipe) ) ;
589
620
let mut respond = pin ! ( self . respond( rx_pipe, tx_pipe) ) ;
590
621
@@ -667,7 +698,7 @@ pub mod builtin {
667
698
}
668
699
}
669
700
670
- impl < ' a , ' b > super :: Mdns for MdnsApi < ' a , ' b > {
701
+ impl < ' a , ' b > super :: Mdns for Mdns < ' a > {
671
702
fn add (
672
703
& self ,
673
704
name : & str ,
@@ -677,7 +708,7 @@ pub mod builtin {
677
708
service_subtypes : & [ & str ] ,
678
709
txt_kvs : & [ ( & str , & str ) ] ,
679
710
) -> Result < ( ) , Error > {
680
- MdnsApi :: add (
711
+ Mdns :: add (
681
712
self ,
682
713
name,
683
714
service,
@@ -695,7 +726,7 @@ pub mod builtin {
695
726
protocol : & str ,
696
727
port : u16 ,
697
728
) -> Result < ( ) , Error > {
698
- MdnsApi :: remove ( self , name, service, protocol, port)
729
+ Mdns :: remove ( self , name, service, protocol, port)
699
730
}
700
731
}
701
732
}
@@ -705,28 +736,34 @@ pub mod astro {
705
736
use core:: cell:: RefCell ;
706
737
use std:: collections:: HashMap ;
707
738
708
- use super :: Mdns ;
709
- use crate :: error:: { Error , ErrorCode } ;
739
+ use crate :: {
740
+ error:: { Error , ErrorCode } ,
741
+ transport:: pipe:: Pipe ,
742
+ } ;
710
743
use astro_dnssd:: { DNSServiceBuilder , RegisteredDnsService } ;
711
744
use log:: info;
712
745
713
746
#[ derive( Debug , Clone , Eq , PartialEq , Hash ) ]
714
- pub struct ServiceId {
747
+ struct ServiceId {
715
748
name : String ,
716
749
service : String ,
717
750
protocol : String ,
718
751
port : u16 ,
719
752
}
720
753
721
- pub struct AstroMdns {
754
+ pub struct Mdns {
722
755
services : RefCell < HashMap < ServiceId , RegisteredDnsService > > ,
723
756
}
724
757
725
- impl AstroMdns {
726
- pub fn new ( ) -> Result < Self , Error > {
727
- Ok ( Self {
758
+ impl Mdns {
759
+ pub fn new ( _id : u16 , _hostname : & str , _ip : [ u8 ; 4 ] , _ipv6 : Option < [ u8 ; 16 ] > ) -> Self {
760
+ Self :: native_new ( )
761
+ }
762
+
763
+ pub fn native_new ( ) -> Self {
764
+ Self {
728
765
services : RefCell :: new ( HashMap :: new ( ) ) ,
729
- } )
766
+ }
730
767
}
731
768
732
769
pub fn add (
@@ -798,7 +835,23 @@ pub mod astro {
798
835
}
799
836
}
800
837
801
- impl Mdns for AstroMdns {
838
+ pub struct MdnsRunner < ' a > ( & ' a Mdns ) ;
839
+
840
+ impl < ' a > MdnsRunner < ' a > {
841
+ pub const fn new ( mdns : & ' a Mdns ) -> Self {
842
+ Self ( mdns)
843
+ }
844
+
845
+ pub async fn run_udp ( & mut self ) -> Result < ( ) , Error > {
846
+ core:: future:: pending :: < Result < ( ) , Error > > ( ) . await
847
+ }
848
+
849
+ pub async fn run ( & self , _tx_pipe : & Pipe < ' _ > , _rx_pipe : & Pipe < ' _ > ) -> Result < ( ) , Error > {
850
+ core:: future:: pending :: < Result < ( ) , Error > > ( ) . await
851
+ }
852
+ }
853
+
854
+ impl super :: Mdns for Mdns {
802
855
fn add (
803
856
& self ,
804
857
name : & str ,
@@ -808,7 +861,7 @@ pub mod astro {
808
861
service_subtypes : & [ & str ] ,
809
862
txt_kvs : & [ ( & str , & str ) ] ,
810
863
) -> Result < ( ) , Error > {
811
- AstroMdns :: add (
864
+ Mdns :: add (
812
865
self ,
813
866
name,
814
867
service,
@@ -826,7 +879,7 @@ pub mod astro {
826
879
protocol : & str ,
827
880
port : u16 ,
828
881
) -> Result < ( ) , Error > {
829
- AstroMdns :: remove ( self , name, service, protocol, port)
882
+ Mdns :: remove ( self , name, service, protocol, port)
830
883
}
831
884
}
832
885
}
0 commit comments