Skip to content

Commit 95fee1f

Browse files
committed
(hopefully) fix ping logic
1 parent e905c3b commit 95fee1f

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/connect.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use lan_mouse_ipc::{ClientHandle, DEFAULT_PORT};
33
use lan_mouse_proto::{ProtoEvent, MAX_EVENT_SIZE};
44
use local_channel::mpsc::{channel, Receiver, Sender};
55
use std::{
6+
cell::RefCell,
67
collections::{HashMap, HashSet},
78
io,
89
net::SocketAddr,
@@ -75,6 +76,7 @@ pub(crate) struct LanMouseConnection {
7576
connecting: Rc<Mutex<HashSet<ClientHandle>>>,
7677
recv_rx: Receiver<(ClientHandle, ProtoEvent)>,
7778
recv_tx: Sender<(ClientHandle, ProtoEvent)>,
79+
ping_response: Rc<RefCell<HashSet<SocketAddr>>>,
7880
}
7981

8082
impl LanMouseConnection {
@@ -87,6 +89,7 @@ impl LanMouseConnection {
8789
connecting: Default::default(),
8890
recv_rx,
8991
recv_tx,
92+
ping_response: Default::default(),
9093
}
9194
}
9295

@@ -110,14 +113,15 @@ impl LanMouseConnection {
110113
if !self.server.client_manager.alive(handle) {
111114
return Err(LanMouseConnectionError::TargetEmulationDisabled);
112115
}
113-
log::trace!("{event} >->->->->- {addr}");
114116
match conn.send(buf).await {
115-
Ok(_) => return Ok(()),
117+
Ok(_) => {}
116118
Err(e) => {
117119
log::warn!("client {handle} failed to send: {e}");
118120
disconnect(&self.server, handle, addr, &self.conns).await;
119121
}
120122
}
123+
log::trace!("{event} >->->->->- {addr}");
124+
return Ok(());
121125
}
122126
}
123127

@@ -133,6 +137,7 @@ impl LanMouseConnection {
133137
self.conns.clone(),
134138
self.connecting.clone(),
135139
self.recv_tx.clone(),
140+
self.ping_response.clone(),
136141
));
137142
}
138143
Err(LanMouseConnectionError::NotConnected)
@@ -146,6 +151,7 @@ async fn connect_to_handle(
146151
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
147152
connecting: Rc<Mutex<HashSet<ClientHandle>>>,
148153
tx: Sender<(ClientHandle, ProtoEvent)>,
154+
ping_response: Rc<RefCell<HashSet<SocketAddr>>>,
149155
) -> Result<(), LanMouseConnectionError> {
150156
log::info!("client {handle} connecting ...");
151157
// sending did not work, figure out active conn.
@@ -173,43 +179,44 @@ async fn connect_to_handle(
173179
connecting.lock().await.remove(&handle);
174180

175181
// poll connection for active
176-
spawn_local(ping_pong(
177-
server.clone(),
182+
spawn_local(ping_pong(addr, conn.clone(), ping_response.clone()));
183+
184+
// receiver
185+
spawn_local(receive_loop(
186+
server,
178187
handle,
179188
addr,
180-
conn.clone(),
181-
conns.clone(),
189+
conn,
190+
conns,
191+
tx,
192+
ping_response.clone(),
182193
));
183-
184-
// receiver
185-
spawn_local(receive_loop(server, handle, addr, conn, conns, tx));
186194
return Ok(());
187195
}
188196
connecting.lock().await.remove(&handle);
189197
Err(LanMouseConnectionError::NotConnected)
190198
}
191199

192200
async fn ping_pong(
193-
server: Service,
194-
handle: ClientHandle,
195201
addr: SocketAddr,
196202
conn: Arc<dyn Conn + Send + Sync>,
197-
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
203+
ping_response: Rc<RefCell<HashSet<SocketAddr>>>,
198204
) {
199205
loop {
200206
let (buf, len) = ProtoEvent::Ping.into();
201-
log::trace!("PING >->->->->- {addr}");
202207
if let Err(e) = conn.send(&buf[..len]).await {
203-
log::warn!("send: {e}");
204-
disconnect(&server, handle, addr, &conns).await;
208+
log::warn!("{addr}: send error `{e}`, closing connection");
209+
let _ = conn.close().await;
205210
break;
206211
}
212+
log::trace!("PING >->->->->- {addr}");
207213

208214
tokio::time::sleep(Duration::from_millis(500)).await;
209215

210-
if server.client_manager.active_addr(handle).is_none() {
211-
log::warn!("no active addr");
212-
disconnect(&server, handle, addr, &conns).await;
216+
if !ping_response.borrow_mut().remove(&addr) {
217+
log::warn!("{addr} did not respond, closing connection");
218+
let _ = conn.close().await;
219+
return;
213220
}
214221
}
215222
}
@@ -221,14 +228,17 @@ async fn receive_loop(
221228
conn: Arc<dyn Conn + Send + Sync>,
222229
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
223230
tx: Sender<(ClientHandle, ProtoEvent)>,
231+
ping_response: Rc<RefCell<HashSet<SocketAddr>>>,
224232
) {
225233
let mut buf = [0u8; MAX_EVENT_SIZE];
226234
while conn.recv(&mut buf).await.is_ok() {
227235
if let Ok(event) = buf.try_into() {
236+
log::trace!("{addr} <==<==<== {event}");
228237
match event {
229238
ProtoEvent::Pong(b) => {
230239
server.client_manager.set_active_addr(handle, Some(addr));
231240
server.client_manager.set_alive(handle, b);
241+
ping_response.borrow_mut().insert(addr);
232242
}
233243
event => tx.send((handle, event)).expect("channel closed"),
234244
}

0 commit comments

Comments
 (0)