Skip to content

Commit 2c9ab41

Browse files
Prevent connecting twice to a single device
1 parent 40c8d64 commit 2c9ab41

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src-tauri/src/server/connection/connection_actor.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::collections::{HashMap, HashSet};
22

33
use futures_util::SinkExt;
44
use futures_util::{
@@ -56,6 +56,7 @@ pub(super) struct ConnectionActor {
5656
discovery_abort: Option<oneshot::Sender<()>>,
5757
notification_aborts: HashMap<(String, Uuid), oneshot::Sender<()>>,
5858
status_listener_abort: Option<oneshot::Sender<()>>,
59+
connected_devices: HashSet<String>,
5960
}
6061

6162
impl ConnectionActor {
@@ -73,6 +74,7 @@ impl ConnectionActor {
7374
discovery_abort: None,
7475
notification_aborts: HashMap::new(),
7576
status_listener_abort: None,
77+
connected_devices: HashSet::new(),
7678
}
7779
}
7880

@@ -96,7 +98,7 @@ impl ConnectionActor {
9698
ConnectionMessage::StatusChanged(status) => self.status_changed(status).await,
9799
ConnectionMessage::ConnectionClosed => {
98100
info!("Connection closed, cleaning up");
99-
self.cleanup();
101+
self.cleanup().await;
100102
break;
101103
}
102104
}
@@ -186,18 +188,33 @@ impl ConnectionActor {
186188
Request::StartDiscovery => self.start_discovery().await,
187189
Request::StopDiscovery => self.stop_discovery(),
188190
Request::Connect { device_id } => {
191+
if self.connected_devices.contains(&device_id) {
192+
return Response::Ok;
193+
}
194+
189195
let result = self.bluetooth.connect(&device_id).await;
190196

191197
match result {
192-
Ok(device) => Response::Connected { device },
198+
Ok(device) => {
199+
self.connected_devices.insert(device_id);
200+
Response::Connected { device }
201+
}
193202
Err(error) => Response::Error {
194203
error: format!("Failed to connect to device: {error:?}"),
195204
},
196205
}
197206
}
198207
Request::Disconnect { device_id } => {
208+
if !self.connected_devices.contains(&device_id) {
209+
return Response::Ok;
210+
}
211+
199212
match self.bluetooth.disconnect(&device_id).await {
200-
Ok(()) => Response::Ok,
213+
Ok(()) => {
214+
self.connected_devices.remove(&device_id);
215+
216+
Response::Ok
217+
}
201218
Err(error) => Response::Error {
202219
error: format!("Failed to disconnect from device: {error:?}"),
203220
},
@@ -479,7 +496,15 @@ impl ConnectionActor {
479496
abort_sender
480497
}
481498

482-
fn cleanup(&mut self) {
499+
async fn cleanup(&mut self) {
500+
let connected_devices = std::mem::take(&mut self.connected_devices);
501+
502+
for device_id in connected_devices {
503+
if let Err(err) = self.bluetooth.disconnect(&device_id).await {
504+
warn!("Failed to disconnect {device_id} during connection cleanup: {err:?}");
505+
}
506+
}
507+
483508
// Abort discovery if running
484509
if let Some(abort) = self.discovery_abort.take() {
485510
let _ = abort.send(());

0 commit comments

Comments
 (0)