1919// DEALINGS IN THE SOFTWARE.
2020
2121use crate :: { SERVICE_NAME , META_QUERY_SERVICE , dns} ;
22+ use async_io:: Async ;
2223use dns_parser:: { Packet , RData } ;
2324use either:: Either :: { Left , Right } ;
2425use futures:: { future, prelude:: * } ;
2526use libp2p_core:: { multiaddr:: { Multiaddr , Protocol } , PeerId } ;
2627use log:: warn;
27- use std:: { convert:: TryFrom as _ , fmt, io, net:: Ipv4Addr , net:: SocketAddr , str, time:: { Duration , Instant } } ;
28+ use std:: { convert:: TryFrom , fmt, io, net:: Ipv4Addr , net:: { UdpSocket , SocketAddr } , str, time:: { Duration , Instant } } ;
2829use wasm_timer:: Interval ;
2930use lazy_static:: lazy_static;
3031
@@ -37,9 +38,6 @@ lazy_static! {
3738 ) ) ;
3839}
3940
40- macro_rules! codegen {
41- ( $feature_name: expr, $service_name: ident, $udp_socket: ty, $udp_socket_from_std: tt) => {
42-
4341/// A running service that discovers libp2p peers and responds to other libp2p peers' queries on
4442/// the local network.
4543///
@@ -71,10 +69,7 @@ macro_rules! codegen {
7169/// # let my_peer_id = PeerId::from(identity::Keypair::generate_ed25519().public());
7270/// # let my_listened_addrs: Vec<Multiaddr> = vec![];
7371/// # async {
74- /// # #[cfg(feature = "async-std")]
7572/// # let mut service = libp2p_mdns::service::MdnsService::new().await.unwrap();
76- /// # #[cfg(feature = "tokio")]
77- /// # let mut service = libp2p_mdns::service::TokioMdnsService::new().await.unwrap();
7873/// let _future_to_poll = async {
7974/// let (mut service, packet) = service.next().await;
8075///
@@ -108,13 +103,12 @@ macro_rules! codegen {
108103/// };
109104/// # };
110105/// # }
111- #[ cfg_attr( docsrs, doc( cfg( feature = $feature_name) ) ) ]
112- pub struct $service_name {
106+ pub struct MdnsService {
113107 /// Main socket for listening.
114- socket: $udp_socket ,
108+ socket : Async < UdpSocket > ,
115109
116110 /// Socket for sending queries on the network.
117- query_socket: $udp_socket ,
111+ query_socket : Async < UdpSocket > ,
118112
119113 /// Interval for sending queries.
120114 query_interval : Interval ,
@@ -137,42 +131,41 @@ pub struct $service_name {
137131 if_watch : if_watch:: IfWatcher ,
138132}
139133
140- impl $service_name {
134+ impl MdnsService {
141135 /// Starts a new mDNS service.
142- pub async fn new( ) -> io:: Result <$service_name > {
136+ pub async fn new ( ) -> io:: Result < Self > {
143137 Self :: new_inner ( false ) . await
144138 }
145139
146140 /// Same as `new`, but we don't automatically send queries on the network.
147- pub async fn silent( ) -> io:: Result <$service_name > {
141+ pub async fn silent ( ) -> io:: Result < Self > {
148142 Self :: new_inner ( true ) . await
149143 }
150144
151145 /// Starts a new mDNS service.
152- async fn new_inner( silent: bool ) -> io:: Result <$service_name > {
146+ async fn new_inner ( silent : bool ) -> io:: Result < Self > {
153147 let socket = {
154148 let builder = net2:: UdpBuilder :: new_v4 ( ) ?;
155149 builder. reuse_address ( true ) ?;
156150 #[ cfg( unix) ]
157151 net2:: unix:: UnixUdpBuilderExt :: reuse_port ( & builder, true ) ?;
158152 let socket = builder. bind ( ( Ipv4Addr :: UNSPECIFIED , 5353 ) ) ?;
159- let socket = $udp_socket_from_std( socket) ?;
160153 socket. set_multicast_loop_v4 ( true ) ?;
161154 socket. set_multicast_ttl_v4 ( 255 ) ?;
162- socket
155+ Async :: new ( socket) ?
163156 } ;
164157
165158 // Given that we pass an IP address to bind, which does not need to be resolved, we can
166159 // use std::net::UdpSocket::bind, instead of its async counterpart from async-std.
167160 let query_socket = {
168161 let socket = std:: net:: UdpSocket :: bind ( ( Ipv4Addr :: UNSPECIFIED , 0 ) ) ?;
169- $udp_socket_from_std ( socket) ?
162+ Async :: new ( socket) ?
170163 } ;
171164
172165
173166 let if_watch = if_watch:: IfWatcher :: new ( ) . await ?;
174167
175- Ok ( $service_name {
168+ Ok ( Self {
176169 socket,
177170 query_socket,
178171 query_interval : Interval :: new_at ( Instant :: now ( ) , Duration :: from_secs ( 20 ) ) ,
@@ -217,7 +210,8 @@ impl $service_name {
217210 if let Ok ( if_watch:: IfEvent :: Up ( inet) ) = event {
218211 if inet. addr ( ) . is_ipv4 ( ) && !inet. addr ( ) . is_loopback ( ) {
219212 self . socket
220- . join_multicast_v4( From :: from( [ 224 , 0 , 0 , 251 ] ) , Ipv4Addr :: UNSPECIFIED )
213+ . get_ref ( )
214+ . join_multicast_v4 ( & From :: from ( [ 224 , 0 , 0 , 251 ] ) , & Ipv4Addr :: UNSPECIFIED )
221215 . ok ( ) ;
222216 }
223217 }
@@ -294,25 +288,14 @@ impl $service_name {
294288 }
295289}
296290
297- impl fmt:: Debug for $service_name {
291+ impl fmt:: Debug for MdnsService {
298292 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
299293 fmt. debug_struct ( "$service_name" )
300294 . field ( "silent" , & self . silent )
301295 . finish ( )
302296 }
303297}
304298
305- } ;
306- }
307-
308- #[ cfg( feature = "async-std" ) ]
309- codegen ! ( "async-std" , MdnsService , async_std:: net:: UdpSocket , ( |socket| Ok :: <_, std:: io:: Error >( async_std:: net:: UdpSocket :: from( socket) ) ) ) ;
310-
311- // Note: Tokio's UdpSocket::from_std does not set the socket into non-blocking mode.
312- #[ cfg( feature = "tokio" ) ]
313- codegen ! ( "tokio" , TokioMdnsService , tokio:: net:: UdpSocket , ( |socket: std:: net:: UdpSocket | { socket. set_nonblocking( true ) ; tokio:: net:: UdpSocket :: from_std( socket) } ) ) ;
314-
315-
316299/// A valid mDNS packet received by the service.
317300#[ derive( Debug ) ]
318301pub enum MdnsPacket {
@@ -700,17 +683,15 @@ mod tests {
700683 }
701684 }
702685
703- #[ cfg( feature = "async-std" ) ]
704686 testgen ! (
705687 async_std,
706688 crate :: service:: MdnsService ,
707689 ( |fut| async_std:: task:: block_on:: <_, ( ) >( fut) )
708690 ) ;
709691
710- #[ cfg( feature = "tokio" ) ]
711692 testgen ! (
712693 tokio,
713- crate :: service:: TokioMdnsService ,
694+ crate :: service:: MdnsService ,
714695 ( |fut| tokio:: runtime:: Runtime :: new( ) . unwrap( ) . block_on:: <futures:: future:: BoxFuture <( ) >>( fut) )
715696 ) ;
716697}
0 commit comments