Skip to content

Commit d7023f7

Browse files
committed
release capture if emulation disabled on target
1 parent 8f33cfd commit d7023f7

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed

lan-mouse-ipc/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub struct ClientState {
161161
/// This should generally be the socket address where data
162162
/// was last received from.
163163
pub active_addr: Option<SocketAddr>,
164-
/// tracks whether or not the client is responding to pings
164+
/// tracks whether or not the client is available for emulation
165165
pub alive: bool,
166166
/// ips from dns
167167
pub dns_ips: Vec<IpAddr>,
@@ -239,7 +239,7 @@ pub enum FrontendRequest {
239239
RemoveAuthorizedKey(String),
240240
}
241241

242-
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
242+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
243243
pub enum Status {
244244
#[default]
245245
Disabled,

lan-mouse-proto/src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ pub enum ProtoEvent {
6161
/// Ping event for tracking unresponsive clients.
6262
/// A client has to respond with [`ProtoEvent::Pong`].
6363
Ping,
64-
/// Response to [`ProtoEvent::Ping`]
65-
Pong,
64+
/// Response to [`ProtoEvent::Ping`], true if emulation is enabled / available
65+
Pong(bool),
6666
}
6767

6868
impl Display for ProtoEvent {
@@ -73,7 +73,13 @@ impl Display for ProtoEvent {
7373
ProtoEvent::Ack(s) => write!(f, "Ack({s})"),
7474
ProtoEvent::Input(e) => write!(f, "{e}"),
7575
ProtoEvent::Ping => write!(f, "ping"),
76-
ProtoEvent::Pong => write!(f, "pong"),
76+
ProtoEvent::Pong(alive) => {
77+
write!(
78+
f,
79+
"pong: {}",
80+
if *alive { "alive" } else { "not available" }
81+
)
82+
}
7783
}
7884
}
7985
}
@@ -110,7 +116,7 @@ impl ProtoEvent {
110116
},
111117
},
112118
ProtoEvent::Ping => EventType::Ping,
113-
ProtoEvent::Pong => EventType::Pong,
119+
ProtoEvent::Pong(_) => EventType::Pong,
114120
ProtoEvent::Enter(_) => EventType::Enter,
115121
ProtoEvent::Leave(_) => EventType::Leave,
116122
ProtoEvent::Ack(_) => EventType::Ack,
@@ -164,7 +170,7 @@ impl TryFrom<[u8; MAX_EVENT_SIZE]> for ProtoEvent {
164170
},
165171
))),
166172
EventType::Ping => Ok(Self::Ping),
167-
EventType::Pong => Ok(Self::Pong),
173+
EventType::Pong => Ok(Self::Pong(decode_u8(&mut buf)? != 0)),
168174
EventType::Enter => Ok(Self::Enter(decode_u8(&mut buf)?.try_into()?)),
169175
EventType::Leave => Ok(Self::Leave(decode_u32(&mut buf)?)),
170176
EventType::Ack => Ok(Self::Ack(decode_u32(&mut buf)?)),
@@ -228,7 +234,7 @@ impl From<ProtoEvent> for ([u8; MAX_EVENT_SIZE], usize) {
228234
},
229235
},
230236
ProtoEvent::Ping => {}
231-
ProtoEvent::Pong => {}
237+
ProtoEvent::Pong(alive) => encode_u8(buf, len, alive as u8),
232238
ProtoEvent::Enter(pos) => encode_u8(buf, len, pos as u8),
233239
ProtoEvent::Leave(serial) => encode_u32(buf, len, serial),
234240
ProtoEvent::Ack(serial) => encode_u32(buf, len, serial),

src/client.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,27 +224,41 @@ impl ClientManager {
224224
.collect()
225225
}
226226

227-
pub(crate) fn set_active_addr(&self, handle: u64, addr: Option<SocketAddr>) {
227+
pub(crate) fn set_active_addr(&self, handle: ClientHandle, addr: Option<SocketAddr>) {
228228
if let Some((_, s)) = self.clients.borrow_mut().get_mut(handle as usize) {
229229
s.active_addr = addr;
230230
}
231231
}
232232

233-
pub(crate) fn active_addr(&self, handle: u64) -> Option<SocketAddr> {
233+
pub(crate) fn set_alive(&self, handle: ClientHandle, alive: bool) {
234+
if let Some((_, s)) = self.clients.borrow_mut().get_mut(handle as usize) {
235+
s.alive = alive;
236+
}
237+
}
238+
239+
pub(crate) fn active_addr(&self, handle: ClientHandle) -> Option<SocketAddr> {
234240
self.clients
235241
.borrow()
236242
.get(handle as usize)
237243
.and_then(|(_, s)| s.active_addr)
238244
}
239245

240-
pub(crate) fn get_port(&self, handle: u64) -> Option<u16> {
246+
pub(crate) fn alive(&self, handle: ClientHandle) -> bool {
247+
self.clients
248+
.borrow()
249+
.get(handle as usize)
250+
.map(|(_, s)| s.alive)
251+
.unwrap_or(false)
252+
}
253+
254+
pub(crate) fn get_port(&self, handle: ClientHandle) -> Option<u16> {
241255
self.clients
242256
.borrow()
243257
.get(handle as usize)
244258
.map(|(c, _)| c.port)
245259
}
246260

247-
pub(crate) fn get_ips(&self, handle: u64) -> Option<HashSet<IpAddr>> {
261+
pub(crate) fn get_ips(&self, handle: ClientHandle) -> Option<HashSet<IpAddr>> {
248262
self.clients
249263
.borrow()
250264
.get(handle as usize)

src/connect.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub(crate) enum LanMouseConnectionError {
3333
Webrtc(#[from] webrtc_util::Error),
3434
#[error("not connected")]
3535
NotConnected,
36+
#[error("emulation is disabled on the target device")]
37+
TargetEmulationDisabled,
3638
}
3739

3840
async fn connect(
@@ -104,6 +106,9 @@ impl LanMouseConnection {
104106
conns.get(&addr).cloned()
105107
};
106108
if let Some(conn) = conn {
109+
if !self.server.client_manager.alive(handle) {
110+
return Err(LanMouseConnectionError::TargetEmulationDisabled);
111+
}
107112
log::trace!("{event} >->->->->- {addr}");
108113
match conn.send(buf).await {
109114
Ok(_) => return Ok(()),
@@ -220,7 +225,10 @@ async fn receive_loop(
220225
while let Ok(_) = conn.recv(&mut buf).await {
221226
if let Ok(event) = buf.try_into() {
222227
match event {
223-
ProtoEvent::Pong => server.client_manager.set_active_addr(handle, Some(addr)),
228+
ProtoEvent::Pong(b) => {
229+
server.client_manager.set_active_addr(handle, Some(addr));
230+
server.client_manager.set_alive(handle, b);
231+
}
224232
event => tx.send((handle, event)).expect("channel closed"),
225233
}
226234
}

src/emulation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Emulation {
5555
listener.reply(addr, ProtoEvent::Ack(0)).await;
5656
}
5757
ProtoEvent::Input(event) => emulation_proxy.consume(event, addr),
58-
ProtoEvent::Ping => listener.reply(addr, ProtoEvent::Pong).await,
58+
ProtoEvent::Ping => listener.reply(addr, ProtoEvent::Pong(server.emulation_status.get() == Status::Enabled)).await,
5959
_ => {}
6060
}
6161
}

0 commit comments

Comments
 (0)