Skip to content

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

Typing on the PC keeps every letter when voice typing rewrites it

Beef74a081e5d80
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8063688..3a9bd46 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -122,6 +122,11 @@ has a Stop button. Choosing another
start its Bluetooth beacon, and the phone looked for the wrong bytes in
it. Both are fixed; the beacon comes from the Windows app for now
([#47](https://github.com/thtbee/nectarlink/issues/47)).
+- **Typing on the PC from the phone lost letters** when voice typing
+ rewrote what it had typed: the PC turned away the burst of backspaces
+ as too fast. The PC now takes typing at full speed, and the phone
+ resends anything an older PC turns away
+ ([#52](https://github.com/thtbee/nectarlink/issues/52)).
- With NectarLink in two profiles on one phone (a work profile, or a
cloned app), only one of them could connect over USB, and the PC kept
offering to pair the other. Each profile now connects over USB, and the
diff --git a/core/nectarlink-core/src/remote.rs b/core/nectarlink-core/src/remote.rs
index f583e73..ef43d76 100644
--- a/core/nectarlink-core/src/remote.rs
+++ b/core/nectarlink-core/src/remote.rs
@@ -29,13 +29,25 @@ pub(crate) const TOGGLE: &str = "remote_input";
const TIMEOUT: Duration = Duration::from_secs(5);
+/// What a PC answers when a phone sends input faster than [`CONTROL_RATE_PER_SEC`].
+const RATE_LIMITED: &str = "rate limit exceeded";
+/// How long a phone keeps resending an input the PC turned away as too fast
+/// (older PCs allowed only 30 at once, then 60 a second), and how long it
+/// waits between tries. Inputs go one at a time, so their order is kept.
+const RATE_LIMIT_RETRY_FOR: Duration = Duration::from_secs(3);
+const RATE_LIMIT_RETRY_AFTER: Duration = Duration::from_millis(25);
+
/// Maximum rate of incoming datagrams (`move`, `scroll`, `laser`) per peer.
const DATAGRAM_RATE_PER_SEC: f64 = 240.0;
const DATAGRAM_BURST: f64 = 60.0;
/// Maximum rate of incoming control-stream `remote.input` requests per peer.
-const CONTROL_RATE_PER_SEC: f64 = 60.0;
-const CONTROL_BURST: f64 = 30.0;
+/// Typing sends one request per key: when voice typing rewrites a sentence,
+/// the phone sends a backspace for each character it replaces, one after the
+/// other as each is answered (several hundred a second over USB). The limit
+/// only stops a runaway flood.
+const CONTROL_RATE_PER_SEC: f64 = 500.0;
+const CONTROL_BURST: f64 = 500.0;
/// Simple token bucket for per-peer input rate limiting.
#[derive(Debug, Clone)]
@@ -168,9 +180,19 @@ pub(crate) async fn input(shared: &Arc<Shared>, session: &Arc<Session>, event: R
check(shared, session).await?;
return move_pointer(shared, session, dx, dy).await;
}
- let env = Envelope::new(types::REMOTE_INPUT, &event)?;
- session.request(env, TIMEOUT).await?.expect(types::OK)?;
- Ok(())
+ let give_up = Instant::now() + RATE_LIMIT_RETRY_FOR;
+ loop {
+ let env = Envelope::new(types::REMOTE_INPUT, &event)?;
+ let reply = session.request(env, TIMEOUT).await?;
+ match reply.expect(types::OK) {
+ Err(nectarlink_protocol::ProtocolError::Remote { code: ErrorCode::Denied, msg })
+ if msg == RATE_LIMITED && Instant::now() < give_up =>
+ {
+ tokio::time::sleep(RATE_LIMIT_RETRY_AFTER).await;
+ }
+ done => return Ok(done?),
+ }
+ }
}
/// Sends relative pointer motion `(dx, dy)` in a QUIC datagram (or a dedicated
@@ -263,7 +285,7 @@ pub(crate) async fn handle(shared: &Arc<Shared>, session: &Arc<Session>, env: &E
.unwrap_or_else(|e| e.into_inner())
.allow_control(session.peer, Instant::now());
if !allowed {
- Envelope::error(ErrorCode::Denied, "rate limit exceeded")
+ Envelope::error(ErrorCode::Denied, RATE_LIMITED)
} else {
let platform = shared.platform.clone();
let peer = session.peer;
diff --git a/core/nectarlink-core/tests/two_devices.rs b/core/nectarlink-core/tests/two_devices.rs
index c9f00f7..d4f85e1 100644
--- a/core/nectarlink-core/tests/two_devices.rs
+++ b/core/nectarlink-core/tests/two_devices.rs
@@ -2670,6 +2670,21 @@ async fn remote_input_needs_toggle_and_delivers_motion_keys_and_slides() {
}
assert!(recorded.contains(&RemoteInput::Laser { on: false, x: 0.0, y: 0.0 }));
+ // Voice typing rewrites what it typed: the phone sends a backspace for each
+ // character it replaces, then the new text, each as soon as the last is
+ // answered. None of it may be refused (GitHub #52).
+ pc.platform.remote_inputs.lock().unwrap().clear();
+ let backspace = RemoteInput::Key { key: remote_keys::BACKSPACE.into(), mods: vec![] };
+ let rewrite = RemoteInput::Text { text: "नमस्ते, how are you doing today".into() };
+ for _ in 0..80 {
+ with_timeout("backspace", phone.node.remote_input(pc_id, backspace.clone())).await.unwrap();
+ }
+ with_timeout("rewritten text", phone.node.remote_input(pc_id, rewrite.clone())).await.unwrap();
+ let recorded = pc.platform.remote_inputs.lock().unwrap().clone();
+ let mut expected = vec![backspace; 80];
+ expected.push(rewrite);
+ assert_eq!(recorded, expected, "every backspace and the text arrive, in order");
+
// Invalid inputs are rejected.
assert!(
phone