p2p: mDNS
This commit is contained in:
@@ -7,7 +7,6 @@ pub use mdns_sd::DaemonEvent;
|
||||
use mdns_sd::{Receiver, ServiceDaemon, ServiceEvent, ServiceInfo};
|
||||
|
||||
pub const LANSPREAD_SERVICE_TYPE: &str = "_lanspread._udp.local.";
|
||||
pub const LANSPREAD_INSTANCE_NAME: &str = "softlan";
|
||||
|
||||
pub struct MdnsAdvertiser {
|
||||
daemon: ServiceDaemon,
|
||||
@@ -74,7 +73,7 @@ pub fn discover_service(
|
||||
|
||||
if let Some(address) = info.get_addresses().iter().next() {
|
||||
tracing::info!("Found server at {}:{}", address, info.get_port());
|
||||
return Ok((*address, info.get_port()).into());
|
||||
return Ok((address.to_ip_addr(), info.get_port()).into());
|
||||
}
|
||||
|
||||
tracing::error!(?info, "No address found in mDNS response.");
|
||||
|
||||
@@ -9,49 +9,34 @@ mod req;
|
||||
|
||||
use std::{convert::Into, net::SocketAddr, time::Duration};
|
||||
|
||||
use chrono::{DateTime, Local};
|
||||
use clap::Parser as _;
|
||||
use cli::Cli;
|
||||
use gethostname::gethostname;
|
||||
use lanspread_compat::eti;
|
||||
use lanspread_db::db::{Game, GameDB};
|
||||
use lanspread_mdns::{
|
||||
DaemonEvent,
|
||||
LANSPREAD_INSTANCE_NAME,
|
||||
LANSPREAD_SERVICE_TYPE,
|
||||
MdnsAdvertiser,
|
||||
};
|
||||
use lanspread_mdns::{DaemonEvent, LANSPREAD_SERVICE_TYPE, MdnsAdvertiser};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use uuid::Uuid;
|
||||
|
||||
fn spawn_mdns_task(server_addr: SocketAddr) -> eyre::Result<()> {
|
||||
let combined_str = if 1 == 2 {
|
||||
let peer_id = Uuid::now_v7().simple().to_string();
|
||||
|
||||
let uidddd = Uuid::now_v7();
|
||||
|
||||
// TODO
|
||||
let uidddd = uidddd
|
||||
.get_timestamp()
|
||||
.expect("failed to get timestamp from UUID")
|
||||
.to_unix();
|
||||
|
||||
let local_datetime: DateTime<Local> =
|
||||
DateTime::from_timestamp(i64::try_from(uidddd.0).unwrap_or(0), uidddd.1)
|
||||
.expect("Failed to create DateTime from uuid unix timestamp")
|
||||
.into();
|
||||
|
||||
dbg!(local_datetime);
|
||||
|
||||
let hostname = gethostname();
|
||||
let mut hostname = hostname.to_str().unwrap_or("");
|
||||
let hostname_str = hostname.to_str().unwrap_or("");
|
||||
|
||||
if hostname.len() + peer_id.len() > 63 {
|
||||
hostname = &hostname[..63 - peer_id.len()];
|
||||
}
|
||||
format!("{hostname}-{peer_id}")
|
||||
// Calculate maximum hostname length that fits with UUID in 63 char limit
|
||||
let max_hostname_len = 63usize.saturating_sub(peer_id.len() + 1); // +1 for the dash
|
||||
let truncated_hostname = if hostname_str.len() > max_hostname_len {
|
||||
hostname_str.get(..max_hostname_len).unwrap_or(hostname_str)
|
||||
} else {
|
||||
String::from(LANSPREAD_INSTANCE_NAME)
|
||||
hostname_str
|
||||
};
|
||||
|
||||
let combined_str = if truncated_hostname.is_empty() {
|
||||
// If no hostname is available, use just the UUID
|
||||
peer_id
|
||||
} else {
|
||||
format!("{truncated_hostname}-{peer_id}")
|
||||
};
|
||||
|
||||
let mdns = MdnsAdvertiser::new(LANSPREAD_SERVICE_TYPE, &combined_str, server_addr)?;
|
||||
|
||||
@@ -8,7 +8,7 @@ use std::{
|
||||
use eyre::bail;
|
||||
use lanspread_client::{ClientCommand, ClientEvent};
|
||||
use lanspread_db::db::{Game, GameDB};
|
||||
use lanspread_mdns::{LANSPREAD_INSTANCE_NAME, LANSPREAD_SERVICE_TYPE, discover_service};
|
||||
use lanspread_mdns::{LANSPREAD_SERVICE_TYPE, discover_service};
|
||||
use tauri::{AppHandle, Emitter as _, Manager};
|
||||
use tauri_plugin_shell::{ShellExt, process::Command};
|
||||
use tokio::sync::{RwLock, mpsc::UnboundedSender};
|
||||
@@ -233,7 +233,7 @@ async fn find_server(app: AppHandle) {
|
||||
log::info!("Looking for server...");
|
||||
|
||||
loop {
|
||||
match discover_service(LANSPREAD_SERVICE_TYPE, Some(LANSPREAD_INSTANCE_NAME)) {
|
||||
match discover_service(LANSPREAD_SERVICE_TYPE, None) {
|
||||
Ok(server_addr) => {
|
||||
log::info!("Found server at {server_addr}");
|
||||
let state: tauri::State<LanSpreadState> = app.state();
|
||||
|
||||
Reference in New Issue
Block a user