Skip to content

Commit 5f80283

Browse files
committed
use addr from accept
1 parent dd9a154 commit 5f80283

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

src/emulation.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,9 @@ impl Emulation {
5151
emulation_proxy.release_keys(addr);
5252
listener.reply(addr, ProtoEvent::Ack(0)).await;
5353
}
54-
ProtoEvent::Ack(_) => {}
5554
ProtoEvent::Input(event) => emulation_proxy.consume(event, addr),
5655
ProtoEvent::Ping => listener.reply(addr, ProtoEvent::Pong).await,
57-
ProtoEvent::Pong => {},
56+
_ => {}
5857
}
5958
}
6059
_ = interval.tick() => {

src/listen.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(crate) struct LanMouseListener {
2626
listen_rx: Receiver<(ProtoEvent, SocketAddr)>,
2727
listen_tx: Sender<(ProtoEvent, SocketAddr)>,
2828
listen_task: JoinHandle<()>,
29-
conns: Rc<Mutex<Vec<Arc<dyn Conn + Send + Sync>>>>,
29+
conns: Rc<Mutex<Vec<(SocketAddr, Arc<dyn Conn + Send + Sync>)>>>,
3030
}
3131

3232
impl LanMouseListener {
@@ -43,7 +43,8 @@ impl LanMouseListener {
4343

4444
let listener = listen(listen_addr, cfg).await?;
4545

46-
let conns: Rc<Mutex<Vec<Arc<dyn Conn + Send + Sync>>>> = Rc::new(Mutex::new(Vec::new()));
46+
let conns: Rc<Mutex<Vec<(SocketAddr, Arc<dyn Conn + Send + Sync>)>>> =
47+
Rc::new(Mutex::new(Vec::new()));
4748

4849
let conns_clone = conns.clone();
4950

@@ -64,8 +65,8 @@ impl LanMouseListener {
6465
};
6566
log::info!("dtls client connected, ip: {addr}");
6667
let mut conns = conns_clone.lock().await;
67-
conns.push(conn.clone());
68-
spawn_local(read_loop(conns_clone.clone(), conn, tx.clone()));
68+
conns.push((addr, conn.clone()));
69+
spawn_local(read_loop(conns_clone.clone(), addr, conn, tx.clone()));
6970
}
7071
});
7172

@@ -80,7 +81,7 @@ impl LanMouseListener {
8081
pub(crate) async fn terminate(&mut self) {
8182
self.listen_task.abort();
8283
let conns = self.conns.lock().await;
83-
for conn in conns.iter() {
84+
for (_, conn) in conns.iter() {
8485
let _ = conn.close().await;
8586
}
8687
self.listen_tx.close();
@@ -90,16 +91,16 @@ impl LanMouseListener {
9091
pub(crate) async fn broadcast(&self, event: ProtoEvent) {
9192
let (buf, len): ([u8; MAX_EVENT_SIZE], usize) = event.into();
9293
let conns = self.conns.lock().await;
93-
for conn in conns.iter() {
94+
for (_, conn) in conns.iter() {
9495
let _ = conn.send(&buf[..len]).await;
9596
}
9697
}
9798

9899
pub(crate) async fn reply(&self, addr: SocketAddr, event: ProtoEvent) {
99100
let (buf, len): ([u8; MAX_EVENT_SIZE], usize) = event.into();
100101
let conns = self.conns.lock().await;
101-
for conn in conns.iter() {
102-
if conn.remote_addr() == Some(addr) {
102+
for (a, conn) in conns.iter() {
103+
if *a == addr {
103104
let _ = conn.send(&buf[..len]).await;
104105
}
105106
}
@@ -118,28 +119,27 @@ impl Stream for LanMouseListener {
118119
}
119120

120121
async fn read_loop(
121-
conns: Rc<Mutex<Vec<Arc<dyn Conn + Send + Sync>>>>,
122+
conns: Rc<Mutex<Vec<(SocketAddr, Arc<dyn Conn + Send + Sync>)>>>,
123+
addr: SocketAddr,
122124
conn: Arc<dyn Conn + Send + Sync>,
123125
dtls_tx: Sender<(ProtoEvent, SocketAddr)>,
124126
) -> Result<(), Error> {
125127
let mut b = [0u8; MAX_EVENT_SIZE];
126128

127129
while conn.recv(&mut b).await.is_ok() {
128130
match b.try_into() {
129-
Ok(event) => dtls_tx
130-
.send((event, conn.remote_addr().expect("no remote addr")))
131-
.expect("channel closed"),
131+
Ok(event) => dtls_tx.send((event, addr)).expect("channel closed"),
132132
Err(e) => {
133133
log::warn!("error receiving event: {e}");
134134
break;
135135
}
136136
}
137137
}
138-
log::info!("dtls client disconnected {:?}", conn.remote_addr());
138+
log::info!("dtls client disconnected {:?}", addr);
139139
let mut conns = conns.lock().await;
140140
let index = conns
141141
.iter()
142-
.position(|c| c.remote_addr() == conn.remote_addr())
142+
.position(|(a, _)| *a == addr)
143143
.expect("connection not found");
144144
conns.remove(index);
145145
Ok(())

0 commit comments

Comments
 (0)