Skip to content

Commit

Permalink
feat: resend broadcast mdns if nearby sharing
Browse files Browse the repository at this point in the history
Signed-off-by: Martichou <m@rtin.fyi>
  • Loading branch information
Martichou committed Feb 14, 2024
1 parent cde67c0 commit 2aa4701
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ hmac = "0.12"
libaes = "0.7"
local-ip-address = "0.5"
log = "0.4"
mdns-sd = "0.10"
mdns-sd = { git = "https://github.com/Martichou/mdns-sd", branch = "unsolicited" }
once_cell = "1.0"
open = "5.0"
p256 = { version = "0.13", features = ["ecdh"] }
Expand Down
15 changes: 6 additions & 9 deletions src/backend/ble.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::time::{Duration, SystemTime};

use anyhow::anyhow;
Expand Down Expand Up @@ -42,7 +41,7 @@ impl BleListener {
})
.await?;

let mut already_alerted = HashMap::new();
let mut last_alert = SystemTime::now();

loop {
tokio::select! {
Expand All @@ -53,20 +52,18 @@ impl BleListener {
Some(e) = events.next() => {
match e {
CentralEvent::ServiceDataAdvertisement { id, service_data } => {
let _ = id;
let _ = service_data;
let now = SystemTime::now();
let when = already_alerted.get(&id);

// Don't spam, max once per 60s
if let Some(t) = when {
if now.duration_since(*t)? <= Duration::from_secs(60) {
continue;
}
// Don't spam, max once per 15s
if now.duration_since(last_alert)? <= Duration::from_secs(15) {
continue;
}

debug!("{INNER_NAME}: A device is sharing nearby");
self.sender.send(())?;
already_alerted.insert(id, now);
last_alert = now;
},
// Not interesting for us
_ => {
Expand Down
13 changes: 9 additions & 4 deletions src/backend/mdns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ impl MDnsServer {
device_type: DeviceType,
receiver: Receiver<()>,
) -> Result<Self, anyhow::Error> {
let service = Self::build_service(service_port, device_type)?;
let fullname = service.get_fullname().to_owned();
let service_info = Self::build_service(service_port, device_type)?;
let fullname = service_info.get_fullname().to_owned();

let daemon = ServiceDaemon::new()?;
daemon.register(service)?;
daemon.register(service_info)?;

Ok(Self {
daemon,
Expand Down Expand Up @@ -77,7 +77,12 @@ impl MDnsServer {
// Can be used later on to change the visibility on the fly
// by unregistering the service and registering it only when
// a device nearby is sharing.
_ = receiver.recv() => {}
_ = receiver.recv() => {
// Android can sometime not see the mDNS service if the service
// was running BEFORE Android started the Discovery phase for QuickShare.
// So resend a broadcast if there's a android device sending.
self.daemon.register_resend(&self.fullname)?;
}
}
}

Expand Down

0 comments on commit 2aa4701

Please sign in to comment.