mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-13 10:31:28 +03:00
(hopefully) fix ping logic
This commit is contained in:
@@ -3,6 +3,7 @@ use lan_mouse_ipc::{ClientHandle, DEFAULT_PORT};
|
|||||||
use lan_mouse_proto::{ProtoEvent, MAX_EVENT_SIZE};
|
use lan_mouse_proto::{ProtoEvent, MAX_EVENT_SIZE};
|
||||||
use local_channel::mpsc::{channel, Receiver, Sender};
|
use local_channel::mpsc::{channel, Receiver, Sender};
|
||||||
use std::{
|
use std::{
|
||||||
|
cell::RefCell,
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
io,
|
io,
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
@@ -75,6 +76,7 @@ pub(crate) struct LanMouseConnection {
|
|||||||
connecting: Rc<Mutex<HashSet<ClientHandle>>>,
|
connecting: Rc<Mutex<HashSet<ClientHandle>>>,
|
||||||
recv_rx: Receiver<(ClientHandle, ProtoEvent)>,
|
recv_rx: Receiver<(ClientHandle, ProtoEvent)>,
|
||||||
recv_tx: Sender<(ClientHandle, ProtoEvent)>,
|
recv_tx: Sender<(ClientHandle, ProtoEvent)>,
|
||||||
|
ping_response: Rc<RefCell<HashSet<SocketAddr>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LanMouseConnection {
|
impl LanMouseConnection {
|
||||||
@@ -87,6 +89,7 @@ impl LanMouseConnection {
|
|||||||
connecting: Default::default(),
|
connecting: Default::default(),
|
||||||
recv_rx,
|
recv_rx,
|
||||||
recv_tx,
|
recv_tx,
|
||||||
|
ping_response: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,14 +113,15 @@ impl LanMouseConnection {
|
|||||||
if !self.server.client_manager.alive(handle) {
|
if !self.server.client_manager.alive(handle) {
|
||||||
return Err(LanMouseConnectionError::TargetEmulationDisabled);
|
return Err(LanMouseConnectionError::TargetEmulationDisabled);
|
||||||
}
|
}
|
||||||
log::trace!("{event} >->->->->- {addr}");
|
|
||||||
match conn.send(buf).await {
|
match conn.send(buf).await {
|
||||||
Ok(_) => return Ok(()),
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!("client {handle} failed to send: {e}");
|
log::warn!("client {handle} failed to send: {e}");
|
||||||
disconnect(&self.server, handle, addr, &self.conns).await;
|
disconnect(&self.server, handle, addr, &self.conns).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
log::trace!("{event} >->->->->- {addr}");
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,6 +137,7 @@ impl LanMouseConnection {
|
|||||||
self.conns.clone(),
|
self.conns.clone(),
|
||||||
self.connecting.clone(),
|
self.connecting.clone(),
|
||||||
self.recv_tx.clone(),
|
self.recv_tx.clone(),
|
||||||
|
self.ping_response.clone(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Err(LanMouseConnectionError::NotConnected)
|
Err(LanMouseConnectionError::NotConnected)
|
||||||
@@ -146,6 +151,7 @@ async fn connect_to_handle(
|
|||||||
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
|
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
|
||||||
connecting: Rc<Mutex<HashSet<ClientHandle>>>,
|
connecting: Rc<Mutex<HashSet<ClientHandle>>>,
|
||||||
tx: Sender<(ClientHandle, ProtoEvent)>,
|
tx: Sender<(ClientHandle, ProtoEvent)>,
|
||||||
|
ping_response: Rc<RefCell<HashSet<SocketAddr>>>,
|
||||||
) -> Result<(), LanMouseConnectionError> {
|
) -> Result<(), LanMouseConnectionError> {
|
||||||
log::info!("client {handle} connecting ...");
|
log::info!("client {handle} connecting ...");
|
||||||
// sending did not work, figure out active conn.
|
// sending did not work, figure out active conn.
|
||||||
@@ -173,16 +179,18 @@ async fn connect_to_handle(
|
|||||||
connecting.lock().await.remove(&handle);
|
connecting.lock().await.remove(&handle);
|
||||||
|
|
||||||
// poll connection for active
|
// poll connection for active
|
||||||
spawn_local(ping_pong(
|
spawn_local(ping_pong(addr, conn.clone(), ping_response.clone()));
|
||||||
server.clone(),
|
|
||||||
handle,
|
|
||||||
addr,
|
|
||||||
conn.clone(),
|
|
||||||
conns.clone(),
|
|
||||||
));
|
|
||||||
|
|
||||||
// receiver
|
// receiver
|
||||||
spawn_local(receive_loop(server, handle, addr, conn, conns, tx));
|
spawn_local(receive_loop(
|
||||||
|
server,
|
||||||
|
handle,
|
||||||
|
addr,
|
||||||
|
conn,
|
||||||
|
conns,
|
||||||
|
tx,
|
||||||
|
ping_response.clone(),
|
||||||
|
));
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
connecting.lock().await.remove(&handle);
|
connecting.lock().await.remove(&handle);
|
||||||
@@ -190,26 +198,25 @@ async fn connect_to_handle(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn ping_pong(
|
async fn ping_pong(
|
||||||
server: Service,
|
|
||||||
handle: ClientHandle,
|
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
conn: Arc<dyn Conn + Send + Sync>,
|
conn: Arc<dyn Conn + Send + Sync>,
|
||||||
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
|
ping_response: Rc<RefCell<HashSet<SocketAddr>>>,
|
||||||
) {
|
) {
|
||||||
loop {
|
loop {
|
||||||
let (buf, len) = ProtoEvent::Ping.into();
|
let (buf, len) = ProtoEvent::Ping.into();
|
||||||
log::trace!("PING >->->->->- {addr}");
|
|
||||||
if let Err(e) = conn.send(&buf[..len]).await {
|
if let Err(e) = conn.send(&buf[..len]).await {
|
||||||
log::warn!("send: {e}");
|
log::warn!("{addr}: send error `{e}`, closing connection");
|
||||||
disconnect(&server, handle, addr, &conns).await;
|
let _ = conn.close().await;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
log::trace!("PING >->->->->- {addr}");
|
||||||
|
|
||||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||||
|
|
||||||
if server.client_manager.active_addr(handle).is_none() {
|
if !ping_response.borrow_mut().remove(&addr) {
|
||||||
log::warn!("no active addr");
|
log::warn!("{addr} did not respond, closing connection");
|
||||||
disconnect(&server, handle, addr, &conns).await;
|
let _ = conn.close().await;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,14 +228,17 @@ async fn receive_loop(
|
|||||||
conn: Arc<dyn Conn + Send + Sync>,
|
conn: Arc<dyn Conn + Send + Sync>,
|
||||||
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
|
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
|
||||||
tx: Sender<(ClientHandle, ProtoEvent)>,
|
tx: Sender<(ClientHandle, ProtoEvent)>,
|
||||||
|
ping_response: Rc<RefCell<HashSet<SocketAddr>>>,
|
||||||
) {
|
) {
|
||||||
let mut buf = [0u8; MAX_EVENT_SIZE];
|
let mut buf = [0u8; MAX_EVENT_SIZE];
|
||||||
while conn.recv(&mut buf).await.is_ok() {
|
while conn.recv(&mut buf).await.is_ok() {
|
||||||
if let Ok(event) = buf.try_into() {
|
if let Ok(event) = buf.try_into() {
|
||||||
|
log::trace!("{addr} <==<==<== {event}");
|
||||||
match event {
|
match event {
|
||||||
ProtoEvent::Pong(b) => {
|
ProtoEvent::Pong(b) => {
|
||||||
server.client_manager.set_active_addr(handle, Some(addr));
|
server.client_manager.set_active_addr(handle, Some(addr));
|
||||||
server.client_manager.set_alive(handle, b);
|
server.client_manager.set_alive(handle, b);
|
||||||
|
ping_response.borrow_mut().insert(addr);
|
||||||
}
|
}
|
||||||
event => tx.send((handle, event)).expect("channel closed"),
|
event => tx.send((handle, event)).expect("channel closed"),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user