[code][fix] improvements for LAN 202503

- more robust client <-> server connection
  - new client event: DownloadGameFilesFailed
  - 3 seconds to reconnect
  - retry forever if server is gone and never lose a UI request

- code cleanup here and there (mostly server)
This commit is contained in:
2025-03-20 19:39:32 +01:00
parent 19434cd1b1
commit 765447e6d1
9 changed files with 405 additions and 239 deletions

View File

@@ -2,27 +2,61 @@ mod cli;
mod quic;
mod req;
use std::{convert::Into, net::SocketAddr};
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,
DaemonEvent,
LANSPREAD_INSTANCE_NAME,
LANSPREAD_SERVICE_TYPE,
MdnsAdvertiser,
};
use quic::run_server;
use tracing_subscriber::EnvFilter;
use uuid::Uuid;
fn spawn_mdns_task(server_addr: SocketAddr) -> eyre::Result<()> {
let mdns = MdnsAdvertiser::new(LANSPREAD_SERVICE_TYPE, LANSPREAD_INSTANCE_NAME, server_addr)?;
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("");
if hostname.len() + peer_id.len() > 63 {
hostname = &hostname[..63 - peer_id.len()];
}
format!("{hostname}-{peer_id}")
} else {
String::from(LANSPREAD_INSTANCE_NAME)
};
let mdns = MdnsAdvertiser::new(LANSPREAD_SERVICE_TYPE, &combined_str, server_addr)?;
tokio::spawn(async move {
while let Ok(event) = mdns.monitor.recv() {
tracing::info!("mDNS: {:?}", &event);
tracing::debug!("mDNS: {:?}", &event);
if let DaemonEvent::Error(e) = event {
tracing::error!("mDNS: {e}");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
tokio::time::sleep(Duration::from_secs(1)).await;
continue;
}
}
@@ -46,6 +80,9 @@ async fn prepare_game_db(cli: &Cli) -> eyre::Result<GameDB> {
game_db.add_thumbnails(&cli.thumbs_dir);
game_db.all_games().iter().for_each(|game| {
tracing::debug!("Found game: {game}");
});
tracing::info!("Prepared game database with {} games", game_db.games.len());
Ok(game_db)
@@ -73,5 +110,5 @@ async fn main() -> eyre::Result<()> {
let game_db = prepare_game_db(&cli).await?;
tracing::info!("Server listening on {server_addr}");
run_server(server_addr, game_db, cli.game_dir).await
crate::quic::run_server(server_addr, game_db, cli.game_dir).await
}