Skip to content

Commit

Permalink
hotfix accept bug
Browse files Browse the repository at this point in the history
  • Loading branch information
feschber committed Nov 5, 2024
1 parent efdb679 commit bdd0b76
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketAddr> =
conns.lock().await.keys().copied().collect();
log::info!("active connections: {active:?}");
break;
}
}
});
}
Expand Down
19 changes: 12 additions & 7 deletions src/listen.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit bdd0b76

Please sign in to comment.