Skip to content

Commit be522dc

Browse files
committed
release capture only if no active capture at pos
1 parent 4d98ac2 commit be522dc

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/capture.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,19 @@ async fn handle_capture_event(
228228
enter_tx.send(handle).expect("channel closed");
229229
}
230230

231+
// incoming connection
232+
if handle >= Service::ENTER_HANDLE_BEGIN {
233+
// if there is no active outgoing connection at the current capture,
234+
// we release the capture
235+
if let Some(pos) = server.get_incoming_pos(handle) {
236+
if server.client_manager.client_at(pos).is_none() {
237+
capture.release().await?;
238+
}
239+
}
240+
// we dont care about events from incoming handles except for releasing the capture
241+
return Ok(());
242+
}
243+
231244
// activated a new client
232245
if event == CaptureEvent::Begin && Some(handle) != server.get_active() {
233246
*state = State::WaitingForAck;

src/service.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub struct Service {
7070
capture_status: Rc<Cell<Status>>,
7171
pub(crate) emulation_status: Rc<Cell<Status>>,
7272
pub(crate) should_release: Rc<RefCell<Option<ReleaseToken>>>,
73-
incoming_conns: Rc<RefCell<HashMap<ClientHandle, (String, SocketAddr)>>>,
73+
incoming_conns: Rc<RefCell<HashMap<ClientHandle, (String, SocketAddr, Position)>>>,
7474
cert: Certificate,
7575
next_trigger_handle: u64,
7676
}
@@ -225,34 +225,42 @@ impl Service {
225225
Ok(())
226226
}
227227

228+
pub(crate) const ENTER_HANDLE_BEGIN: u64 = u64::MAX / 2 + 1;
229+
228230
fn add_incoming(
229231
&mut self,
230232
addr: SocketAddr,
231233
pos: Position,
232234
fingerprint: String,
233235
capture: &Capture,
234236
) {
235-
const ENTER_HANDLE_BEGIN: u64 = u64::MAX / 2 + 1;
236-
let handle = ENTER_HANDLE_BEGIN + self.next_trigger_handle;
237+
let handle = Self::ENTER_HANDLE_BEGIN + self.next_trigger_handle;
237238
self.next_trigger_handle += 1;
238239
capture.create(handle, pos);
239240
self.incoming_conns
240241
.borrow_mut()
241-
.insert(handle, (fingerprint, addr));
242+
.insert(handle, (fingerprint, addr, pos));
242243
}
243244

244245
fn remove_incoming(&mut self, addr: SocketAddr, capture: &Capture) -> Option<String> {
245246
let handle = self
246247
.incoming_conns
247248
.borrow()
248249
.iter()
249-
.find(|(_, (_, a))| *a == addr)
250+
.find(|(_, (_, a, _))| *a == addr)
250251
.map(|(k, _)| *k)?;
251252
capture.destroy(handle);
252253
self.incoming_conns
253254
.borrow_mut()
254255
.remove(&handle)
255-
.map(|(f, _)| f)
256+
.map(|(f, _, _)| f)
257+
}
258+
259+
pub(crate) fn get_incoming_pos(&self, handle: ClientHandle) -> Option<Position> {
260+
self.incoming_conns
261+
.borrow()
262+
.get(&handle)
263+
.map(|&(_, _, p)| p)
256264
}
257265

258266
fn notify_frontend(&self, event: FrontendEvent) {

0 commit comments

Comments
 (0)