pub fn server(addr: SocketAddr, config: Config) -> Result<Self, Error> {
        let socket = socket2::Socket::new(
            socket2::Domain::IPV4,
            socket2::Type::DGRAM,
            Some(socket2::Protocol::UDP),
        )?;

        socket.set_reuse_address(true)?;

        //TODO: implement set_reuse_port for windows platform
        #[cfg(target_family = "unix")]
        socket.set_reuse_port(true)?;

        socket.set_read_timeout(Some(Duration::from_millis(100)))?;
        socket.bind(&SockAddr::from(addr))?;

        {
            let mut join_error_count = 0;
            let interfaces = match ifaces::ifaces() {
                Ok(e) => e,
                Err(e) => {
                    log::error!("Error getting interfaces: {:?}", e);
                    return Err(Error::new(e.to_string()));
                }
            };

            for interface in &interfaces {
                if let Some(SocketAddr::V4(e)) = interface.addr {
                    if let Err(e) = socket.join_multicast_v4(&Ipv4Addr::new(224, 0, 0, 251), e.ip())
                    {
                        log::error!("Error connecting multicast, error: {:?}", e);
                        join_error_count += 1;
                        continue;
                    }

                    log::trace!("Connected to interface address {:?}", e);
                }
            }

            if join_error_count >= interfaces.len() {
                return Err(ERR_JOINING_MULTICAST_GROUP.to_owned());
            }
        }

        let socket = UdpSocket::from_std(socket.into())?;

        let local_names = config
            .local_names
            .iter()
            .map(|l| l.to_string() + ".")
            .collect();

        let dst_addr: SocketAddr = DEFAULT_DEST_ADDR.parse()?;

        let is_server_closed = Arc::new(atomic::AtomicBool::new(false));

        let (close_server_send, close_server_rcv) = mpsc::channel(1);

        let c = DNSConn {
            query_interval: if config.query_interval != Duration::from_secs(0) {
                config.query_interval
            } else {
                DEFAULT_QUERY_INTERVAL
            },

            queries: Arc::new(Mutex::new(vec![])),
            socket: Arc::new(socket),
            dst_addr,
            is_server_closed: Arc::clone(&is_server_closed),
            close_server: close_server_send,
        };

        let queries = c.queries.clone();
        let socket = Arc::clone(&c.socket);

        tokio::spawn(async move {
            DNSConn::start(
                close_server_rcv,
                is_server_closed,
                socket,
                local_names,
                dst_addr,
                queries,
            )
            .await
        });

        Ok(c)
    }
/// server establishes a mDNS connection over an existing connection
server establishes a mDNS connection over an existing connection
server establishes a mDNS connection over an existing connection
pub fn server(addr: SocketAddr, config: Config) -> Result<Self, Error> {
        let socket = socket2::Socket::new(
            socket2::Domain::IPV4,
            socket2::Type::DGRAM,
            Some(socket2::Protocol::UDP),
        )?;
        socket.set_reuse_address(true)?;
        //TODO: implement set_reuse_port for windows platform
        #[cfg(target_family = "unix")]
        socket.set_reuse_port(true)?;
        socket.set_read_timeout(Some(Duration::from_millis(100)))?;
        socket.bind(&SockAddr::from(addr))?;
        {
            let mut join_error_count = 0;
            let interfaces = match ifaces::ifaces() {
                Ok(e) => e,
                Err(e) => {
                    log::error!("Error getting interfaces: {:?}", e);
                    return Err(Error::new(e.to_string()));
                }
            };
            for interface in &interfaces {
                if let Some(SocketAddr::V4(e)) = interface.addr {
                    if let Err(e) = socket.join_multicast_v4(&Ipv4Addr::new(224, 0, 0, 251), e.ip())
                    {
                        log::error!("Error connecting multicast, error: {:?}", e);
                        join_error_count += 1;
                        continue;
                    }
                    log::trace!("Connected to interface address {:?}", e);
                }
            }
            if join_error_count >= interfaces.len() {
                return Err(ERR_JOINING_MULTICAST_GROUP.to_owned());
            }
        }
        let socket = UdpSocket::from_std(socket.into())?;
        let local_names = config
            .local_names
            .iter()
            .map(|l| l.to_string() + ".")
            .collect();
        let dst_addr: SocketAddr = DEFAULT_DEST_ADDR.parse()?;
        let is_server_closed = Arc::new(atomic::AtomicBool::new(false));
        let (close_server_send, close_server_rcv) = mpsc::channel(1);
        let c = DNSConn {
            query_interval: if config.query_interval != Duration::from_secs(0) {
                config.query_interval
            } else {
                DEFAULT_QUERY_INTERVAL
            },
            queries: Arc::new(Mutex::new(vec![])),
            socket: Arc::new(socket),
            dst_addr,
            is_server_closed: Arc::clone(&is_server_closed),
            close_server: close_server_send,
        };
        let queries = c.queries.clone();
        let socket = Arc::clone(&c.socket);
        tokio::spawn(async move {
            DNSConn::start(
                close_server_rcv,
                is_server_closed,
                socket,
                local_names,
                dst_addr,
                queries,
            )
            .await
        });
        Ok(c)
    }
pub fn server ( addr : SocketAddr , config : Config ) -> Result < Self , Error > { let socket = socket2 :: Socket :: new ( socket2 :: Domain :: IPV4 , socket2 :: Type :: DGRAM , Some ( socket2 :: Protocol :: UDP ) , ) ? ; socket . set_reuse_address ( true ) ? ; //TODO: implement set_reuse_port for windows platform # [ cfg ( target_family " " ) ] socket . set_reuse_port ( true ) ? ; socket . set_read_timeout ( Some ( Duration :: from_millis ( 100 ) ) ) ? ; socket . bind ( & SockAddr :: from ( addr ) ) ? ; { let mut join_error_count = 0 ; let interfaces = match ifaces :: ifaces ( ) { Ok ( e ) => e , Err ( e ) => { log :: error ! ( " " e ) ; return Err ( Error :: new ( e . to_string ( ) ) ) ; } } ; for interface in & interfaces { if let Some ( SocketAddr :: V4 ( e ) ) = interface . addr { if let Err ( e ) = socket . join_multicast_v4 ( & Ipv4Addr :: new ( 224 , 0 , 0 , 251 ) , e . ip ( ) ) { log :: error ! ( " " e ) ; join_error_count += 1 ; continue ; } log :: trace ! ( " " e ) ; } } if join_error_count >= interfaces . len ( ) { return Err ( ERR_JOINING_MULTICAST_GROUP . to_owned ( ) ) ; } } let socket = UdpSocket :: from_std ( socket . into ( ) ) ? ; let local_names = config . local_names . iter ( ) . map ( | l | l . to_string ( ) + " " ) . collect ( ) ; let dst_addr : SocketAddr = DEFAULT_DEST_ADDR . parse ( ) ? ; let is_server_closed = Arc :: new ( atomic :: AtomicBool :: new ( false ) ) ; let ( close_server_send , close_server_rcv ) = mpsc :: channel ( 1 ) ; let c = DNSConn { query_interval : if config . query_interval != Duration :: from_secs ( 0 ) { config . query_interval } else { DEFAULT_QUERY_INTERVAL } , queries : Arc :: new ( Mutex :: new ( vec ! [ ] ) ) , socket : Arc :: new ( socket ) , dst_addr , is_server_closed : Arc :: clone ( & is_server_closed ) , close_server : close_server_send , } ; let queries = c . queries . clone ( ) ; let socket = Arc :: clone ( & c . socket ) ; tokio :: spawn ( async move { DNSConn :: start ( close_server_rcv , is_server_closed , socket , local_names , dst_addr , queries , ) . await } ) ; Ok ( c ) }
server establishes a mDNS connection over an existing connection
server establishes a mDNS connection over an existing connection
addr
SocketAddr
config
Config
addr
SocketAddr
config
Config