Skip to content

Commit b0af343

Browse files
committed
android: switch from futures-channel to async-channel.
`futures-channel` doesn't support blocking send, so in the callbacks from java (which are not in an async context) we're forced to do `try_send`, which will lose data if the channel is full because the Rust side is too busy. This is not a big deal for scanning, but is for stuff like gatt/l2cap responses that are coming next.
1 parent 359c026 commit b0af343

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ tokio = { version = "1.20.1", features = ["rt-multi-thread"] }
5656

5757
[target.'cfg(target_os = "android")'.dependencies]
5858
java-spaghetti = "0.1.0"
59-
futures-channel = "0.3.24"
59+
async-channel = "2.2.0"
6060

6161
[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
6262
async-broadcast = "0.5.1"

src/android/adapter.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicI32, Ordering};
44
use std::sync::{Arc, Mutex};
55
use std::task::{Context, Poll};
66

7-
use futures_channel::mpsc::{Receiver, Sender};
7+
use async_channel::{Receiver, Sender};
88
use futures_core::Stream;
99
use futures_lite::{stream, StreamExt};
1010
use java_spaghetti::{Arg, ByteArray, Env, Global, Local, PrimitiveArray, VM};
@@ -96,7 +96,7 @@ impl AdapterImpl {
9696
});
9797
});
9898

99-
Ok(receiver.map(move |x| {
99+
Ok(Box::pin(receiver).map(move |x| {
100100
let _guard = &guard;
101101
x
102102
}))
@@ -174,7 +174,7 @@ impl<T: Send + 'static> CallbackRouter<T> {
174174

175175
fn allocate(&'static self) -> CallbackReceiver<T> {
176176
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
177-
let (sender, receiver) = futures_channel::mpsc::channel(16);
177+
let (sender, receiver) = async_channel::bounded(16);
178178
self.map
179179
.lock()
180180
.unwrap()
@@ -190,7 +190,7 @@ impl<T: Send + 'static> CallbackRouter<T> {
190190

191191
fn callback(&'static self, id: i32, val: T) {
192192
if let Some(sender) = self.map.lock().unwrap().as_mut().unwrap().get_mut(&id) {
193-
if let Err(e) = sender.try_send(val) {
193+
if let Err(e) = sender.send_blocking(val) {
194194
warn!("failed to send scan callback: {:?}", e)
195195
}
196196
}

0 commit comments

Comments
 (0)