diff --git a/src/connect.rs b/src/connect.rs index 4054178f..1a8b56cb 100644 --- a/src/connect.rs +++ b/src/connect.rs @@ -151,7 +151,15 @@ impl LanMouseConnection { } tokio::time::sleep(Duration::from_millis(500)).await; let mut buf = [0u8; MAX_EVENT_SIZE]; - conn.recv(&mut buf).await; + if let Err(e) = conn.recv(&mut buf).await { + log::warn!("recv(): client ({handle}) @ {addr} connection closed: {e}"); + conns.lock().await.remove(&addr); + server.set_active_addr(handle, None); + let active: Vec = + conns.lock().await.keys().copied().collect(); + log::info!("active connections: {active:?}"); + break; + } } }); } diff --git a/src/listen.rs b/src/listen.rs index 05a61129..6f361800 100644 --- a/src/listen.rs +++ b/src/listen.rs @@ -1,7 +1,7 @@ use futures::{Stream, StreamExt}; use lan_mouse_proto::{ProtoEvent, MAX_EVENT_SIZE}; use local_channel::mpsc::{channel, Receiver, Sender}; -use std::{net::SocketAddr, rc::Rc, sync::Arc}; +use std::{net::SocketAddr, rc::Rc, sync::Arc, time::Duration}; use thiserror::Error; use tokio::{ sync::Mutex, @@ -50,12 +50,17 @@ impl LanMouseListener { let tx = listen_tx.clone(); let listen_task: JoinHandle<()> = spawn_local(async move { loop { - let (conn, addr) = match listener.accept().await { - Ok(c) => c, - Err(e) => { - log::warn!("accept: {e}"); - continue; - } + log::info!("accepting ..."); + let sleep = tokio::time::sleep(Duration::from_secs(2)); + let (conn, addr) = tokio::select! { + _ = sleep => continue, + c = listener.accept() => match c { + Ok(c) => c, + Err(e) => { + log::warn!("accept: {e}"); + continue; + } + }, }; log::info!("dtls client connected, ip: {addr}"); let mut conns = conns_clone.lock().await;