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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
/*
Hurry Curry! - a game about cooking
Copyright 2024 metamuffin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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(())
}
|