mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-17 18:01:29 +03:00
use addr from accept
This commit is contained in:
@@ -51,10 +51,9 @@ impl Emulation {
|
|||||||
emulation_proxy.release_keys(addr);
|
emulation_proxy.release_keys(addr);
|
||||||
listener.reply(addr, ProtoEvent::Ack(0)).await;
|
listener.reply(addr, ProtoEvent::Ack(0)).await;
|
||||||
}
|
}
|
||||||
ProtoEvent::Ack(_) => {}
|
|
||||||
ProtoEvent::Input(event) => emulation_proxy.consume(event, addr),
|
ProtoEvent::Input(event) => emulation_proxy.consume(event, addr),
|
||||||
ProtoEvent::Ping => listener.reply(addr, ProtoEvent::Pong).await,
|
ProtoEvent::Ping => listener.reply(addr, ProtoEvent::Pong).await,
|
||||||
ProtoEvent::Pong => {},
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ = interval.tick() => {
|
_ = interval.tick() => {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ pub(crate) struct LanMouseListener {
|
|||||||
listen_rx: Receiver<(ProtoEvent, SocketAddr)>,
|
listen_rx: Receiver<(ProtoEvent, SocketAddr)>,
|
||||||
listen_tx: Sender<(ProtoEvent, SocketAddr)>,
|
listen_tx: Sender<(ProtoEvent, SocketAddr)>,
|
||||||
listen_task: JoinHandle<()>,
|
listen_task: JoinHandle<()>,
|
||||||
conns: Rc<Mutex<Vec<Arc<dyn Conn + Send + Sync>>>>,
|
conns: Rc<Mutex<Vec<(SocketAddr, Arc<dyn Conn + Send + Sync>)>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LanMouseListener {
|
impl LanMouseListener {
|
||||||
@@ -43,7 +43,8 @@ impl LanMouseListener {
|
|||||||
|
|
||||||
let listener = listen(listen_addr, cfg).await?;
|
let listener = listen(listen_addr, cfg).await?;
|
||||||
|
|
||||||
let conns: Rc<Mutex<Vec<Arc<dyn Conn + Send + Sync>>>> = Rc::new(Mutex::new(Vec::new()));
|
let conns: Rc<Mutex<Vec<(SocketAddr, Arc<dyn Conn + Send + Sync>)>>> =
|
||||||
|
Rc::new(Mutex::new(Vec::new()));
|
||||||
|
|
||||||
let conns_clone = conns.clone();
|
let conns_clone = conns.clone();
|
||||||
|
|
||||||
@@ -64,8 +65,8 @@ impl LanMouseListener {
|
|||||||
};
|
};
|
||||||
log::info!("dtls client connected, ip: {addr}");
|
log::info!("dtls client connected, ip: {addr}");
|
||||||
let mut conns = conns_clone.lock().await;
|
let mut conns = conns_clone.lock().await;
|
||||||
conns.push(conn.clone());
|
conns.push((addr, conn.clone()));
|
||||||
spawn_local(read_loop(conns_clone.clone(), conn, tx.clone()));
|
spawn_local(read_loop(conns_clone.clone(), addr, conn, tx.clone()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -80,7 +81,7 @@ impl LanMouseListener {
|
|||||||
pub(crate) async fn terminate(&mut self) {
|
pub(crate) async fn terminate(&mut self) {
|
||||||
self.listen_task.abort();
|
self.listen_task.abort();
|
||||||
let conns = self.conns.lock().await;
|
let conns = self.conns.lock().await;
|
||||||
for conn in conns.iter() {
|
for (_, conn) in conns.iter() {
|
||||||
let _ = conn.close().await;
|
let _ = conn.close().await;
|
||||||
}
|
}
|
||||||
self.listen_tx.close();
|
self.listen_tx.close();
|
||||||
@@ -90,7 +91,7 @@ impl LanMouseListener {
|
|||||||
pub(crate) async fn broadcast(&self, event: ProtoEvent) {
|
pub(crate) async fn broadcast(&self, event: ProtoEvent) {
|
||||||
let (buf, len): ([u8; MAX_EVENT_SIZE], usize) = event.into();
|
let (buf, len): ([u8; MAX_EVENT_SIZE], usize) = event.into();
|
||||||
let conns = self.conns.lock().await;
|
let conns = self.conns.lock().await;
|
||||||
for conn in conns.iter() {
|
for (_, conn) in conns.iter() {
|
||||||
let _ = conn.send(&buf[..len]).await;
|
let _ = conn.send(&buf[..len]).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -98,8 +99,8 @@ impl LanMouseListener {
|
|||||||
pub(crate) async fn reply(&self, addr: SocketAddr, event: ProtoEvent) {
|
pub(crate) async fn reply(&self, addr: SocketAddr, event: ProtoEvent) {
|
||||||
let (buf, len): ([u8; MAX_EVENT_SIZE], usize) = event.into();
|
let (buf, len): ([u8; MAX_EVENT_SIZE], usize) = event.into();
|
||||||
let conns = self.conns.lock().await;
|
let conns = self.conns.lock().await;
|
||||||
for conn in conns.iter() {
|
for (a, conn) in conns.iter() {
|
||||||
if conn.remote_addr() == Some(addr) {
|
if *a == addr {
|
||||||
let _ = conn.send(&buf[..len]).await;
|
let _ = conn.send(&buf[..len]).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -118,7 +119,8 @@ impl Stream for LanMouseListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn read_loop(
|
async fn read_loop(
|
||||||
conns: Rc<Mutex<Vec<Arc<dyn Conn + Send + Sync>>>>,
|
conns: Rc<Mutex<Vec<(SocketAddr, Arc<dyn Conn + Send + Sync>)>>>,
|
||||||
|
addr: SocketAddr,
|
||||||
conn: Arc<dyn Conn + Send + Sync>,
|
conn: Arc<dyn Conn + Send + Sync>,
|
||||||
dtls_tx: Sender<(ProtoEvent, SocketAddr)>,
|
dtls_tx: Sender<(ProtoEvent, SocketAddr)>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
@@ -126,20 +128,18 @@ async fn read_loop(
|
|||||||
|
|
||||||
while conn.recv(&mut b).await.is_ok() {
|
while conn.recv(&mut b).await.is_ok() {
|
||||||
match b.try_into() {
|
match b.try_into() {
|
||||||
Ok(event) => dtls_tx
|
Ok(event) => dtls_tx.send((event, addr)).expect("channel closed"),
|
||||||
.send((event, conn.remote_addr().expect("no remote addr")))
|
|
||||||
.expect("channel closed"),
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!("error receiving event: {e}");
|
log::warn!("error receiving event: {e}");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log::info!("dtls client disconnected {:?}", conn.remote_addr());
|
log::info!("dtls client disconnected {:?}", addr);
|
||||||
let mut conns = conns.lock().await;
|
let mut conns = conns.lock().await;
|
||||||
let index = conns
|
let index = conns
|
||||||
.iter()
|
.iter()
|
||||||
.position(|c| c.remote_addr() == conn.remote_addr())
|
.position(|(a, _)| *a == addr)
|
||||||
.expect("connection not found");
|
.expect("connection not found");
|
||||||
conns.remove(index);
|
conns.remove(index);
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user