mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-09 06:51:27 +03:00
remove dependency on service
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use crate::service::Service;
|
use crate::client::ClientManager;
|
||||||
use lan_mouse_ipc::{ClientHandle, DEFAULT_PORT};
|
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};
|
||||||
@@ -92,8 +92,8 @@ async fn connect_any(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct LanMouseConnection {
|
pub(crate) struct LanMouseConnection {
|
||||||
server: Service,
|
|
||||||
cert: Certificate,
|
cert: Certificate,
|
||||||
|
client_manager: ClientManager,
|
||||||
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>>>,
|
||||||
recv_rx: Receiver<(ClientHandle, ProtoEvent)>,
|
recv_rx: Receiver<(ClientHandle, ProtoEvent)>,
|
||||||
@@ -102,11 +102,11 @@ pub(crate) struct LanMouseConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl LanMouseConnection {
|
impl LanMouseConnection {
|
||||||
pub(crate) fn new(server: Service, cert: Certificate) -> Self {
|
pub(crate) fn new(cert: Certificate, client_manager: ClientManager) -> Self {
|
||||||
let (recv_tx, recv_rx) = channel();
|
let (recv_tx, recv_rx) = channel();
|
||||||
Self {
|
Self {
|
||||||
server,
|
|
||||||
cert,
|
cert,
|
||||||
|
client_manager,
|
||||||
conns: Default::default(),
|
conns: Default::default(),
|
||||||
connecting: Default::default(),
|
connecting: Default::default(),
|
||||||
recv_rx,
|
recv_rx,
|
||||||
@@ -126,20 +126,20 @@ impl LanMouseConnection {
|
|||||||
) -> Result<(), LanMouseConnectionError> {
|
) -> Result<(), LanMouseConnectionError> {
|
||||||
let (buf, len): ([u8; MAX_EVENT_SIZE], usize) = event.into();
|
let (buf, len): ([u8; MAX_EVENT_SIZE], usize) = event.into();
|
||||||
let buf = &buf[..len];
|
let buf = &buf[..len];
|
||||||
if let Some(addr) = self.server.client_manager.active_addr(handle) {
|
if let Some(addr) = self.client_manager.active_addr(handle) {
|
||||||
let conn = {
|
let conn = {
|
||||||
let conns = self.conns.lock().await;
|
let conns = self.conns.lock().await;
|
||||||
conns.get(&addr).cloned()
|
conns.get(&addr).cloned()
|
||||||
};
|
};
|
||||||
if let Some(conn) = conn {
|
if let Some(conn) = conn {
|
||||||
if !self.server.client_manager.alive(handle) {
|
if !self.client_manager.alive(handle) {
|
||||||
return Err(LanMouseConnectionError::TargetEmulationDisabled);
|
return Err(LanMouseConnectionError::TargetEmulationDisabled);
|
||||||
}
|
}
|
||||||
match conn.send(buf).await {
|
match conn.send(buf).await {
|
||||||
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.client_manager, handle, addr, &self.conns).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log::trace!("{event} >->->->->- {addr}");
|
log::trace!("{event} >->->->->- {addr}");
|
||||||
@@ -153,7 +153,7 @@ impl LanMouseConnection {
|
|||||||
connecting.insert(handle);
|
connecting.insert(handle);
|
||||||
// connect in the background
|
// connect in the background
|
||||||
spawn_local(connect_to_handle(
|
spawn_local(connect_to_handle(
|
||||||
self.server.clone(),
|
self.client_manager.clone(),
|
||||||
self.cert.clone(),
|
self.cert.clone(),
|
||||||
handle,
|
handle,
|
||||||
self.conns.clone(),
|
self.conns.clone(),
|
||||||
@@ -167,7 +167,7 @@ impl LanMouseConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn connect_to_handle(
|
async fn connect_to_handle(
|
||||||
server: Service,
|
client_manager: ClientManager,
|
||||||
cert: Certificate,
|
cert: Certificate,
|
||||||
handle: ClientHandle,
|
handle: ClientHandle,
|
||||||
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
|
conns: Rc<Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>>,
|
||||||
@@ -177,11 +177,8 @@ async fn connect_to_handle(
|
|||||||
) -> 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.
|
||||||
if let Some(addrs) = server.client_manager.get_ips(handle) {
|
if let Some(addrs) = client_manager.get_ips(handle) {
|
||||||
let port = server
|
let port = client_manager.get_port(handle).unwrap_or(DEFAULT_PORT);
|
||||||
.client_manager
|
|
||||||
.get_port(handle)
|
|
||||||
.unwrap_or(DEFAULT_PORT);
|
|
||||||
let addrs = addrs
|
let addrs = addrs
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|a| SocketAddr::new(a, port))
|
.map(|a| SocketAddr::new(a, port))
|
||||||
@@ -196,7 +193,7 @@ async fn connect_to_handle(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
log::info!("client ({handle}) connected @ {addr}");
|
log::info!("client ({handle}) connected @ {addr}");
|
||||||
server.client_manager.set_active_addr(handle, Some(addr));
|
client_manager.set_active_addr(handle, Some(addr));
|
||||||
conns.lock().await.insert(addr, conn.clone());
|
conns.lock().await.insert(addr, conn.clone());
|
||||||
connecting.lock().await.remove(&handle);
|
connecting.lock().await.remove(&handle);
|
||||||
|
|
||||||
@@ -205,7 +202,7 @@ async fn connect_to_handle(
|
|||||||
|
|
||||||
// receiver
|
// receiver
|
||||||
spawn_local(receive_loop(
|
spawn_local(receive_loop(
|
||||||
server,
|
client_manager,
|
||||||
handle,
|
handle,
|
||||||
addr,
|
addr,
|
||||||
conn,
|
conn,
|
||||||
@@ -244,7 +241,7 @@ async fn ping_pong(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn receive_loop(
|
async fn receive_loop(
|
||||||
server: Service,
|
client_manager: ClientManager,
|
||||||
handle: ClientHandle,
|
handle: ClientHandle,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
conn: Arc<dyn Conn + Send + Sync>,
|
conn: Arc<dyn Conn + Send + Sync>,
|
||||||
@@ -258,8 +255,8 @@ async fn receive_loop(
|
|||||||
log::trace!("{addr} <==<==<== {event}");
|
log::trace!("{addr} <==<==<== {event}");
|
||||||
match event {
|
match event {
|
||||||
ProtoEvent::Pong(b) => {
|
ProtoEvent::Pong(b) => {
|
||||||
server.client_manager.set_active_addr(handle, Some(addr));
|
client_manager.set_active_addr(handle, Some(addr));
|
||||||
server.client_manager.set_alive(handle, b);
|
client_manager.set_alive(handle, b);
|
||||||
ping_response.borrow_mut().insert(addr);
|
ping_response.borrow_mut().insert(addr);
|
||||||
}
|
}
|
||||||
event => tx.send((handle, event)).expect("channel closed"),
|
event => tx.send((handle, event)).expect("channel closed"),
|
||||||
@@ -267,18 +264,18 @@ async fn receive_loop(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
log::warn!("recv error");
|
log::warn!("recv error");
|
||||||
disconnect(&server, handle, addr, &conns).await;
|
disconnect(&client_manager, handle, addr, &conns).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn disconnect(
|
async fn disconnect(
|
||||||
server: &Service,
|
client_manager: &ClientManager,
|
||||||
handle: ClientHandle,
|
handle: ClientHandle,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
conns: &Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>,
|
conns: &Mutex<HashMap<SocketAddr, Arc<dyn Conn + Send + Sync>>>,
|
||||||
) {
|
) {
|
||||||
log::warn!("client ({handle}) @ {addr} connection closed");
|
log::warn!("client ({handle}) @ {addr} connection closed");
|
||||||
conns.lock().await.remove(&addr);
|
conns.lock().await.remove(&addr);
|
||||||
server.client_manager.set_active_addr(handle, None);
|
client_manager.set_active_addr(handle, None);
|
||||||
let active: Vec<SocketAddr> = conns.lock().await.keys().copied().collect();
|
let active: Vec<SocketAddr> = conns.lock().await.keys().copied().collect();
|
||||||
log::info!("active connections: {active:?}");
|
log::info!("active connections: {active:?}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ impl Service {
|
|||||||
self.authorized_keys.clone(),
|
self.authorized_keys.clone(),
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
let conn = LanMouseConnection::new(self.clone(), self.cert.clone());
|
let conn = LanMouseConnection::new(self.cert.clone(), self.client_manager.clone());
|
||||||
|
|
||||||
// input capture + emulation
|
// input capture + emulation
|
||||||
let capture_backend = self.config.capture_backend.map(|b| b.into());
|
let capture_backend = self.config.capture_backend.map(|b| b.into());
|
||||||
|
|||||||
Reference in New Issue
Block a user