Expand description
autodiscovery-rs provides a function to automatically detect and connect to peers.
§Examples
use std::net::{TcpListener, TcpStream};
use std::thread;
use autodiscover_rs::Method;
use env_logger;
fn handle_client(stream: std::io::Result<TcpStream>) {
println!("Got a connection from {:?}", stream.unwrap().peer_addr());
}
fn main() -> std::io::Result<()> {
env_logger::init();
// make sure to bind before announcing ready
let listener = TcpListener::bind(":::0")?;
// get the port we were bound too; note that the trailing :0 above gives us a random unused port
let socket = listener.local_addr()?;
thread::spawn(move || {
// this function blocks forever; running it a seperate thread
autodiscover_rs::run(&socket, Method::Multicast("[ff0e::1]:1337".parse().unwrap()), |s| {
// change this to task::spawn if using async_std or tokio
thread::spawn(|| handle_client(s));
}).unwrap();
});
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next() {
// if you are using an async library, such as async_std or tokio, you can convert the stream to the
// appropriate type before using task::spawn from your library of choice.
thread::spawn(|| handle_client(stream));
}
Ok(())
}
Enums§
- Method
- Method describes whether a multicast or broadcast method for sending discovery messages should be used.
Functions§
- run
- 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.