Skip to content

Commit 70a4938

Browse files
committed
fix ping logic
1 parent 5f80283 commit 70a4938

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/connect.rs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ pub(crate) enum LanMouseConnectionError {
3838
async fn connect(
3939
addr: SocketAddr,
4040
) -> Result<(Arc<dyn Conn + Sync + Send>, SocketAddr), LanMouseConnectionError> {
41+
log::info!("connecting to {addr} ...");
4142
let conn = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
4243
conn.connect(addr).await?;
43-
log::info!("connected to {addr}, establishing secure dtls channel ...");
4444
let certificate = Certificate::generate_self_signed(["localhost".to_owned()])?;
4545
let config = Config {
4646
certificates: vec![certificate],
@@ -50,6 +50,7 @@ async fn connect(
5050
};
5151
let dtls_conn: Arc<dyn Conn + Send + Sync> =
5252
Arc::new(DTLSConn::new(conn, config, true, None).await?);
53+
log::info!("{addr} connected successfully!");
5354
Ok((dtls_conn, addr))
5455
}
5556

@@ -101,10 +102,11 @@ impl LanMouseConnection {
101102
conns.get(&addr).cloned()
102103
};
103104
if let Some(conn) = conn {
105+
log::trace!("{event} >->->->->- {addr}");
104106
match conn.send(buf).await {
105107
Ok(_) => return Ok(()),
106108
Err(e) => {
107-
log::warn!("client {handle} failed to connect: {e}");
109+
log::warn!("client {handle} failed to send: {e}");
108110
self.conns.lock().await.remove(&addr);
109111
self.server.set_active_addr(handle, None);
110112
}
@@ -136,6 +138,7 @@ async fn connect_to_handle(
136138
connecting: Rc<Mutex<HashSet<ClientHandle>>>,
137139
tx: Sender<(ClientHandle, ProtoEvent)>,
138140
) -> Result<(), LanMouseConnectionError> {
141+
log::info!("client {handle} connecting ...");
139142
// sending did not work, figure out active conn.
140143
if let Some(addrs) = server.get_ips(handle) {
141144
let port = server.get_port(handle).unwrap_or(DEFAULT_PORT);
@@ -167,9 +170,10 @@ async fn connect_to_handle(
167170
));
168171

169172
// receiver
170-
spawn_local(receive_loop(handle, conn, tx));
173+
spawn_local(receive_loop(server, handle, addr, conn, conns, tx));
171174
return Ok(());
172175
}
176+
connecting.lock().await.remove(&handle);
173177
Err(LanMouseConnectionError::NotConnected)
174178
}
175179

@@ -182,36 +186,48 @@ async fn ping_pong(
182186
) {
183187
loop {
184188
let (buf, len) = ProtoEvent::Ping.into();
185-
if let Err(e) = conn.send(&buf[..len]).await {
186-
log::warn!("client ({handle}) @ {addr} connection closed: {e}");
187-
conns.lock().await.remove(&addr);
188-
server.set_active_addr(handle, None);
189-
let active: Vec<SocketAddr> = conns.lock().await.keys().copied().collect();
190-
log::info!("active connections: {active:?}");
189+
if let Err(_) = conn.send(&buf[..len]).await {
190+
disconnect(&server, handle, addr, &conns).await;
191191
break;
192192
}
193+
193194
tokio::time::sleep(Duration::from_millis(500)).await;
194-
let mut buf = [0u8; MAX_EVENT_SIZE];
195-
if let Err(e) = conn.recv(&mut buf).await {
196-
log::warn!("recv(): client ({handle}) @ {addr} connection closed: {e}");
197-
conns.lock().await.remove(&addr);
198-
server.set_active_addr(handle, None);
199-
let active: Vec<SocketAddr> = conns.lock().await.keys().copied().collect();
200-
log::info!("active connections: {active:?}");
201-
break;
195+
196+
if server.active_addr(handle).is_none() {
197+
disconnect(&server, handle, addr, &conns).await;
202198
}
203199
}
204200
}
205201

206202
async fn receive_loop(
203+
server: Server,
207204
handle: ClientHandle,
205+
addr: SocketAddr,
208206
conn: Arc<dyn Conn + Send + Sync>,
207+
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
209208
tx: Sender<(ClientHandle, ProtoEvent)>,
210209
) {
211210
let mut buf = [0u8; MAX_EVENT_SIZE];
212211
while let Ok(_) = conn.recv(&mut buf).await {
213212
if let Ok(event) = buf.try_into() {
214-
tx.send((handle, event)).expect("channel closed");
213+
match event {
214+
ProtoEvent::Pong => server.set_active_addr(handle, None),
215+
event => tx.send((handle, event)).expect("channel closed"),
216+
}
215217
}
216218
}
219+
disconnect(&server, handle, addr, &conns).await;
220+
}
221+
222+
async fn disconnect(
223+
server: &Server,
224+
handle: ClientHandle,
225+
addr: SocketAddr,
226+
conns: &Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>,
227+
) {
228+
log::warn!("client ({handle}) @ {addr} connection closed");
229+
conns.lock().await.remove(&addr);
230+
server.set_active_addr(handle, None);
231+
let active: Vec<SocketAddr> = conns.lock().await.keys().copied().collect();
232+
log::info!("active connections: {active:?}");
217233
}

0 commit comments

Comments
 (0)