pub fn run<F: Fn(std::io::Result<TcpStream>)>(
    connect_to: &SocketAddr,
    method: Method,
    spawn_callback: F,
) -> std::io::Result<()> {
    match method {
        Method::Broadcast(addr) => {
            let socket = Socket::new(Domain::ipv4(), Type::dgram(), None)?;
            socket.set_reuse_address(true)?;
            socket.set_broadcast(true)?;
            socket.bind(&addr.into())?;
            let socket: UdpSocket = socket.into_udp_socket();
            let result = socket.send_to(&to_bytes(connect_to), addr)?;
            trace!("sent {} byte announcement to {:?}", result, addr);
            handle_broadcast_message(socket, connect_to, &spawn_callback)?;
        }
        Method::Multicast(addr) => {
            let socket = Socket::new(Domain::ipv6(), Type::dgram(), None)?;
            socket.set_reuse_address(true)?;
            socket.bind(&addr.into())?;
            let socket: UdpSocket = socket.into_udp_socket();
            match addr.ip() {
                IpAddr::V4(addr) => {
                    let iface: Ipv4Addr = 0u32.into();
                    socket.join_multicast_v4(&addr, &iface)?;
                }
                IpAddr::V6(addr) => {
                    socket.join_multicast_v6(&addr, 0)?;
                }
            }
            // we need a different, temporary socket, to send multicast in IPv6
            {
                let socket = UdpSocket::bind(":::0")?;
                let result = socket.send_to(&to_bytes(connect_to), addr)?;
                trace!("sent {} byte announcement to {:?}", result, addr);
            }
            handle_broadcast_message(socket, connect_to, &spawn_callback)?;
        }
    }
    warn!("It looks like I stopped listening; this shouldn't happen.");
    Ok(())
}
/// run will block forever. It sends a notification using the configured method, then listens for other notifications and begins
/// connecting to them, calling spawn_callback (which should return right away!) with the connected streams. The connect_to address
/// should be a socket we have already bind'ed too, since we advertise that to other autodiscovery clients.
run will block forever. It sends a notification using the configured method, then listens for other notifications and begins
connecting to them, calling spawn_callback (which should return right away!) with the connected streams. The connect_to address
should be a socket we have already bind'ed too, since we advertise that to other autodiscovery clients.
run will block forever . It sends a notification using the configured method then listens for other notifications and begins connecting to them calling spawn_callback ( which should return right away! ) with the connected streams . The connect_to address should be a socket we have already bind ' ed too since we advertise that to other autodiscovery clients .
pub fn run<F: Fn(std::io::Result<TcpStream>)>(
    connect_to: &SocketAddr,
    method: Method,
    spawn_callback: F,
) -> std::io::Result<()> {
    match method {
        Method::Broadcast(addr) => {
            let socket = Socket::new(Domain::ipv4(), Type::dgram(), None)?;
            socket.set_reuse_address(true)?;
            socket.set_broadcast(true)?;
            socket.bind(&addr.into())?;
            let socket: UdpSocket = socket.into_udp_socket();
            let result = socket.send_to(&to_bytes(connect_to), addr)?;
            trace!("sent {} byte announcement to {:?}", result, addr);
            handle_broadcast_message(socket, connect_to, &spawn_callback)?;
        }
        Method::Multicast(addr) => {
            let socket = Socket::new(Domain::ipv6(), Type::dgram(), None)?;
            socket.set_reuse_address(true)?;
            socket.bind(&addr.into())?;
            let socket: UdpSocket = socket.into_udp_socket();
            match addr.ip() {
                IpAddr::V4(addr) => {
                    let iface: Ipv4Addr = 0u32.into();
                    socket.join_multicast_v4(&addr, &iface)?;
                }
                IpAddr::V6(addr) => {
                    socket.join_multicast_v6(&addr, 0)?;
                }
            }
            // we need a different, temporary socket, to send multicast in IPv6
            {
                let socket = UdpSocket::bind(":::0")?;
                let result = socket.send_to(&to_bytes(connect_to), addr)?;
                trace!("sent {} byte announcement to {:?}", result, addr);
            }
            handle_broadcast_message(socket, connect_to, &spawn_callback)?;
        }
    }
    warn!("It looks like I stopped listening; this shouldn't happen.");
    Ok(())
}
pub fn run < F : Fn ( std :: io :: Result < TcpStream > ) > ( connect_to : & SocketAddr , method : Method , spawn_callback : F , ) -> std :: io :: Result < ( ) > { match method { Method :: Broadcast ( addr ) => { let socket = Socket :: new ( Domain :: ipv4 ( ) , Type :: dgram ( ) , None ) ? ; socket . set_reuse_address ( true ) ? ; socket . set_broadcast ( true ) ? ; socket . bind ( & addr . into ( ) ) ? ; let socket : UdpSocket = socket . into_udp_socket ( ) ; let result = socket . send_to ( & to_bytes ( connect_to ) , addr ) ? ; trace ! ( " " result addr ) ; handle_broadcast_message ( socket , connect_to , & spawn_callback ) ? ; } Method :: Multicast ( addr ) => { let socket = Socket :: new ( Domain :: ipv6 ( ) , Type :: dgram ( ) , None ) ? ; socket . set_reuse_address ( true ) ? ; socket . bind ( & addr . into ( ) ) ? ; let socket : UdpSocket = socket . into_udp_socket ( ) ; match addr . ip ( ) { IpAddr :: V4 ( addr ) => { let iface : Ipv4Addr = 0u32 . into ( ) ; socket . join_multicast_v4 ( & addr , & iface ) ? ; } IpAddr :: V6 ( addr ) => { socket . join_multicast_v6 ( & addr , 0 ) ? ; } } // we need a different, temporary socket, to send multicast in IPv6 { let socket = UdpSocket :: bind ( " " ) ? ; let result = socket . send_to ( & to_bytes ( connect_to ) , addr ) ? ; trace ! ( " " result addr ) ; } handle_broadcast_message ( socket , connect_to , & spawn_callback ) ? ; } } warn ! ( " " ) ; Ok ( ( ) ) }
run will block forever.
run will block forever .
connect_to
&SocketAddr
method
Method
spawn_callback
F
connect_to
&SocketAddr
method
Method
spawn_callback
F