Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Open app windows and mirroring after a reconnect

Bee5173bb7cec7e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bd5371b..ee6b3a4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,10 @@ change things that need a step from you, and its notes say so.
calls and alarms. A new post from the same app shows as usual.
- Android: turning off notification access, or a force stop, no longer
crashes the app.
+- **App windows and mirroring open after a reconnect.** If the connection to
+ the PC dropped or was replaced just as you opened an app window or the
+ phone's screen, the phone gave up at once with "Offline". It now waits up
+ to 10 seconds for the connection to come back, and opens the stream on it.
## 0.1.3 (2026-10-15)
diff --git a/core/nectarlink-core/src/node.rs b/core/nectarlink-core/src/node.rs
index 48effa2..c0c5c25 100644
--- a/core/nectarlink-core/src/node.rs
+++ b/core/nectarlink-core/src/node.rs
@@ -58,6 +58,9 @@ const IDLE_TIMEOUT: Duration = Duration::from_secs(8);
/// Flow-control windows: how much may be in flight per stream and in all.
const STREAM_WINDOW: u32 = 16 * 1024 * 1024;
const CONNECTION_WINDOW: u32 = 32 * 1024 * 1024;
+/// How long opening a stream the PC asked for (its screen, an app window)
+/// waits for the connection to come back if it just dropped or was replaced.
+const STREAM_RECONNECT_WAIT: Duration = Duration::from_secs(10);
/// Capabilities every build offers.
const BASE_CAPABILITIES: &[&str] = &[
@@ -1703,6 +1706,20 @@ impl Node {
self.shared.session(peer).ok_or(Error::Offline)
}
+ /// Like [`connected`](Self::connected), but waits up to [`STREAM_RECONNECT_WAIT`]
+ /// for a connection that just dropped or was replaced to come back.
+ async fn connected_soon(&self, peer: &DeviceId) -> Result<Arc<Session>> {
+ let deadline = tokio::time::Instant::now() + STREAM_RECONNECT_WAIT;
+ loop {
+ match self.connected(peer) {
+ Err(Error::Offline) if tokio::time::Instant::now() < deadline => {
+ tokio::time::sleep(Duration::from_millis(100)).await;
+ }
+ other => return other,
+ }
+ }
+ }
+
// ---- Capabilities ----
/// What works with a paired device, and how to unlock what doesn't.
@@ -2417,17 +2434,34 @@ impl Node {
crate::mirror::apps(&self.shared, &session).await
}
- /// Opens this phone's video stream to a PC that asked for the screen.
+ /// Opens this phone's video stream to a PC that asked for the screen
+ /// or an app window.
pub async fn mirror_open(&self, peer: DeviceId) -> Result<crate::MirrorStream> {
- let session = self.connected(&peer)?;
- crate::mirror::open(&self.shared, &session, false).await
+ self.open_mirror_stream(peer, false).await
}
/// Opens this phone's sound stream to a PC that asked for the screen
/// with its sound.
pub async fn mirror_open_audio(&self, peer: DeviceId) -> Result<crate::MirrorStream> {
- let session = self.connected(&peer)?;
- crate::mirror::open(&self.shared, &session, true).await
+ self.open_mirror_stream(peer, true).await
+ }
+
+ /// The PC asked for the stream a moment ago, but the connection it asked
+ /// over can drop or be replaced (a reconnect, a duplicate connection
+ /// resolved) before the stream opens. Waits for the connection to come
+ /// back, and retries once on a new one.
+ async fn open_mirror_stream(&self, peer: DeviceId, audio: bool) -> Result<crate::MirrorStream> {
+ let first = self.connected_soon(&peer).await?;
+ match crate::mirror::open(&self.shared, &first, audio).await {
+ Err(err @ (Error::Offline | Error::Network(_))) => {
+ let next = self.connected_soon(&peer).await?;
+ if Arc::ptr_eq(&next, &first) {
+ return Err(err);
+ }
+ crate::mirror::open(&self.shared, &next, audio).await
+ }
+ other => other,
+ }
}
// ---- Webcam (docs/protocol/webcam.md) ----
diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rs
index 5273693..9af299d 100644
--- a/core/nectarlink-core/tests/two_devices.rs
+++ b/core/nectarlink-core/tests/two_devices.rs
@@ -969,6 +969,43 @@ async fn reconnects_after_a_restart() {
with_timeout("ring after reconnect", pc.node.ring(phone_id, true)).await.expect("ring works again");
}
+#[tokio::test(flavor = "multi_thread")]
+async fn mirror_open_waits_for_a_connection_that_is_coming_back() {
+ let pc_offers = [nectarlink_core::MIRROR_VIEW, nectarlink_core::MIRROR_LISTEN];
+ let mut pc = device_with("Desktop", DeviceKind::Desktop, &pc_offers).await;
+ let mut phone = device_with("Pixel", DeviceKind::Phone, &[nectarlink_core::MIRROR_CAPTURE]).await;
+ pair_qr(&mut pc, &mut phone).await;
+ let pc_id = pc.node.device_id();
+
+ // The PC drops off right after asking for the screen.
+ pc.node.shutdown().await;
+ wait_for(&mut phone, "pc offline", |e| match e {
+ NodeEvent::LinkChanged { device, link: LinkState::Offline { .. } } if *device == pc_id => Some(()),
+ _ => None,
+ })
+ .await;
+
+ // If it stays away, opening gives up after a while instead of hanging.
+ let started = std::time::Instant::now();
+ let gone = with_timeout("open while away", phone.node.mirror_open(pc_id)).await;
+ assert!(matches!(gone, Err(Error::Offline)), "{:?}", gone.err());
+ assert!(started.elapsed() >= Duration::from_secs(5), "waits for the PC first");
+
+ // If it comes back meanwhile, the stream opens on the new connection.
+ let TestDevice { node: old_pc, dir, .. } = pc;
+ drop(old_pc);
+ let (opened, _pc) =
+ tokio::join!(with_timeout("open while reconnecting", phone.node.mirror_open(pc_id)), async {
+ tokio::time::sleep(Duration::from_millis(500)).await;
+ let pc = start_in(dir, "Desktop", DeviceKind::Desktop).await;
+ let mut caps = vec!["media.control".to_owned()];
+ caps.extend(pc_offers.iter().map(|c| (*c).to_owned()));
+ pc.node.update_power(PowerLevel::NotApplicable, caps).await;
+ pc
+ });
+ assert!(opened.is_ok(), "opens once the PC is back: {:?}", opened.err());
+}
+
/// Waits for a capability matrix of `peer` whose `feature` is in a state
/// matching `want`.
async fn wait_feature(