aboutsummaryrefslogtreecommitdiff
path: root/server/src/network/mdns.rs
blob: 870ae7ec3d13241bff1a9a3152e898c1bc486d2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::server::Server;
use anyhow::Result;
use get_if_addrs::get_if_addrs;
use hurrycurry_protocol::VERSION;
use log::{info, warn};
use mdns_sd::{ServiceDaemon, ServiceInfo};
use rand::random;
use std::{collections::HashMap, sync::Arc, time::Duration};
use tokio::{sync::RwLock, time::interval};

pub async fn mdns_loop(name: String, port: u16, state: Arc<RwLock<Server>>) {
    let d = match ServiceDaemon::new() {
        Ok(d) => d,
        Err(e) => {
            warn!("mDNS daemon failed to start: {e}");
            return;
        }
    };
    let mut interval = interval(Duration::from_secs(60));
    let hostname = format!("hks-{}.local.", random::<u64>());
    loop {
        interval.tick().await;
        if let Err(e) = update_service(&d, &state, &name, &hostname, port).await {
            warn!("mDNS service update failed: {e}");
        }
        info!("updated mDNS service record");
    }
}

async fn update_service(
    d: &ServiceDaemon,
    state: &Arc<RwLock<Server>>,
    name: &str,
    hostname: &str,
    port: u16,
) -> Result<()> {
    let addrs = get_if_addrs()?
        .into_iter()
        .map(|e| e.addr.ip())
        .filter(|a| !a.is_loopback())
        .collect::<Vec<_>>();

    let players = state.read().await.count_chefs();

    d.register(ServiceInfo::new(
        "_hurrycurry._tcp.local.",
        name,
        hostname,
        &*addrs,
        port,
        HashMap::from_iter([
            ("players".to_string(), players.to_string()),
            ("version".to_string(), env!("CARGO_PKG_VERSION").to_string()),
            (
                "protocol".to_string(),
                format!("{}.{}", VERSION.0, VERSION.1),
            ),
        ]),
    )?)?;
    Ok(())
}