Skip to content

Commit 7da735f

Browse files
committed
Rewrite async runtime handling and port to tracing
1 parent 7527cdf commit 7da735f

28 files changed

Lines changed: 266 additions & 166 deletions

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ idevice = { path = "../idevice", default-features = false }
99
futures = { version = "0.3", optional = true }
1010
tracing = { version = "0.1.41" }
1111
tracing-subscriber = { version = "0.3.20", features = ["env-filter"] }
12+
tracing-appender = { version = "0.2" }
1213
once_cell = "1.21.1"
1314
tokio = { version = "1.44.1", features = ["full"] }
1415
libc = "0.2.171"

ffi/src/adapter.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use idevice::tcp::handle::StreamHandle;
77
use tokio::io::{AsyncReadExt, AsyncWriteExt};
88

99
use crate::core_device_proxy::AdapterHandle;
10-
use crate::{IdeviceFfiError, RUNTIME, ReadWriteOpaque, ffi_err};
10+
use crate::{IdeviceFfiError, ReadWriteOpaque, ffi_err, run_sync};
1111

1212
pub struct AdapterStreamHandle(pub StreamHandle);
1313

@@ -36,7 +36,7 @@ pub unsafe extern "C" fn adapter_connect(
3636
}
3737

3838
let adapter = unsafe { &mut (*adapter_handle).0 };
39-
let res = RUNTIME.block_on(async move { adapter.connect(port).await });
39+
let res = run_sync(async move { adapter.connect(port).await });
4040

4141
match res {
4242
Ok(r) => {
@@ -81,7 +81,7 @@ pub unsafe extern "C" fn adapter_pcap(
8181
Err(_) => return ffi_err!(IdeviceError::FfiInvalidString),
8282
};
8383

84-
let res = RUNTIME.block_on(async move { adapter.pcap(path_str).await });
84+
let res = run_sync(async move { adapter.pcap(path_str).await });
8585

8686
match res {
8787
Ok(_) => null_mut(),
@@ -111,7 +111,7 @@ pub unsafe extern "C" fn adapter_stream_close(
111111
}
112112

113113
let adapter = unsafe { &mut (*handle).0 };
114-
RUNTIME.block_on(async move { adapter.close() });
114+
run_sync(async move { adapter.close() });
115115

116116
null_mut()
117117
}
@@ -133,7 +133,7 @@ pub unsafe extern "C" fn adapter_close(handle: *mut AdapterHandle) -> *mut Idevi
133133
}
134134

135135
let adapter = unsafe { &mut (*handle).0 };
136-
RUNTIME.block_on(async move { adapter.close().await.ok() });
136+
run_sync(async move { adapter.close().await.ok() });
137137

138138
null_mut()
139139
}
@@ -164,7 +164,7 @@ pub unsafe extern "C" fn adapter_send(
164164
let adapter = unsafe { &mut (*handle).0 };
165165
let data_slice = unsafe { std::slice::from_raw_parts(data, length) };
166166

167-
let res = RUNTIME.block_on(async move { adapter.write_all(data_slice).await });
167+
let res = run_sync(async move { adapter.write_all(data_slice).await });
168168

169169
match res {
170170
Ok(_) => null_mut(),
@@ -202,7 +202,7 @@ pub unsafe extern "C" fn adapter_recv(
202202
}
203203

204204
let adapter = unsafe { &mut (*handle).0 };
205-
let res: Result<Vec<u8>, std::io::Error> = RUNTIME.block_on(async move {
205+
let res: Result<Vec<u8>, std::io::Error> = run_sync(async move {
206206
let mut buf = [0; 2048];
207207
let res = adapter.read(&mut buf).await?;
208208
Ok(buf[..res].to_vec())

ffi/src/afc.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use idevice::{
88
provider::IdeviceProvider,
99
};
1010

11-
use crate::{IdeviceFfiError, IdeviceHandle, RUNTIME, ffi_err, provider::IdeviceProviderHandle};
11+
use crate::{
12+
IdeviceFfiError, IdeviceHandle, LOCAL_RUNTIME, ffi_err, provider::IdeviceProviderHandle,
13+
run_sync, run_sync_local,
14+
};
1215

1316
pub struct AfcClientHandle(pub AfcClient);
1417

@@ -34,7 +37,7 @@ pub unsafe extern "C" fn afc_client_connect(
3437
return ffi_err!(IdeviceError::FfiInvalidArg);
3538
}
3639

37-
let res = RUNTIME.block_on(async {
40+
let res = run_sync_local(async {
3841
let provider_ref: &dyn IdeviceProvider = unsafe { &*(*provider).0 };
3942

4043
AfcClient::connect(provider_ref).await
@@ -122,7 +125,7 @@ pub unsafe extern "C" fn afc_list_directory(
122125
// Use to_string_lossy to handle non-UTF8 paths
123126
let path = path_cstr.to_string_lossy();
124127

125-
let res: Result<Vec<String>, IdeviceError> = RUNTIME.block_on(async move {
128+
let res: Result<Vec<String>, IdeviceError> = run_sync_local(async move {
126129
// SAFETY: We're assuming client is a valid pointer here
127130
let client_ref = unsafe { &mut (*client).0 };
128131
client_ref.list_dir(&path.to_string()).await
@@ -194,7 +197,7 @@ pub unsafe extern "C" fn afc_make_directory(
194197
Err(_) => return ffi_err!(IdeviceError::FfiInvalidArg),
195198
};
196199

197-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
200+
let res: Result<(), IdeviceError> = run_sync_local(async move {
198201
let client_ref = unsafe { &mut (*client).0 };
199202
client_ref.mk_dir(path).await
200203
});
@@ -246,7 +249,7 @@ pub unsafe extern "C" fn afc_get_file_info(
246249
Err(_) => return ffi_err!(IdeviceError::FfiInvalidArg),
247250
};
248251

249-
let res: Result<FileInfo, IdeviceError> = RUNTIME.block_on(async move {
252+
let res: Result<FileInfo, IdeviceError> = run_sync_local(async move {
250253
let client_ref = unsafe { &mut (*client).0 };
251254
client_ref.get_file_info(path).await
252255
});
@@ -331,7 +334,7 @@ pub unsafe extern "C" fn afc_get_device_info(
331334
return ffi_err!(IdeviceError::FfiInvalidArg);
332335
}
333336

334-
let res: Result<DeviceInfo, IdeviceError> = RUNTIME.block_on(async move {
337+
let res: Result<DeviceInfo, IdeviceError> = run_sync_local(async move {
335338
let client_ref = unsafe { &mut (*client).0 };
336339
client_ref.get_device_info().await
337340
});
@@ -395,7 +398,7 @@ pub unsafe extern "C" fn afc_remove_path(
395398
Err(_) => return ffi_err!(IdeviceError::FfiInvalidArg),
396399
};
397400

398-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
401+
let res: Result<(), IdeviceError> = run_sync_local(async move {
399402
let client_ref = unsafe { &mut (*client).0 };
400403
client_ref.remove(path).await
401404
});
@@ -433,7 +436,7 @@ pub unsafe extern "C" fn afc_remove_path_and_contents(
433436
Err(_) => return ffi_err!(IdeviceError::FfiInvalidArg),
434437
};
435438

436-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
439+
let res: Result<(), IdeviceError> = run_sync_local(async move {
437440
let client_ref = unsafe { &mut (*client).0 };
438441
client_ref.remove_all(path).await
439442
});
@@ -506,7 +509,7 @@ pub unsafe extern "C" fn afc_file_open(
506509

507510
let mode = mode.into();
508511

509-
let res: Result<*mut AfcFileHandle, IdeviceError> = RUNTIME.block_on(async move {
512+
let res: Result<*mut AfcFileHandle, IdeviceError> = LOCAL_RUNTIME.block_on(async move {
510513
let client_ref = unsafe { &mut (*client).0 };
511514
let result = client_ref.open(path, mode).await;
512515
match result {
@@ -544,7 +547,7 @@ pub unsafe extern "C" fn afc_file_close(handle: *mut AfcFileHandle) -> *mut Idev
544547
}
545548

546549
let fd = unsafe { Box::from_raw(handle as *mut idevice::afc::file::FileDescriptor) };
547-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move { fd.close().await });
550+
let res: Result<(), IdeviceError> = run_sync(async move { fd.close().await });
548551

549552
match res {
550553
Ok(_) => null_mut(),
@@ -575,7 +578,7 @@ pub unsafe extern "C" fn afc_file_read(
575578
}
576579

577580
let fd = unsafe { &mut *(handle as *mut idevice::afc::file::FileDescriptor) };
578-
let res: Result<Vec<u8>, IdeviceError> = RUNTIME.block_on(async move { fd.read().await });
581+
let res: Result<Vec<u8>, IdeviceError> = run_sync(async move { fd.read().await });
579582

580583
match res {
581584
Ok(bytes) => {
@@ -617,7 +620,7 @@ pub unsafe extern "C" fn afc_file_write(
617620
let fd = unsafe { &mut *(handle as *mut idevice::afc::file::FileDescriptor) };
618621
let data_slice = unsafe { std::slice::from_raw_parts(data, length) };
619622

620-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move { fd.write(data_slice).await });
623+
let res: Result<(), IdeviceError> = run_sync(async move { fd.write(data_slice).await });
621624

622625
match res {
623626
Ok(_) => null_mut(),
@@ -674,7 +677,7 @@ pub unsafe extern "C" fn afc_make_link(
674677
AfcLinkType::Symbolic => idevice::afc::opcode::LinkType::Symlink,
675678
};
676679

677-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
680+
let res: Result<(), IdeviceError> = run_sync_local(async move {
678681
let client_ref = unsafe { &mut (*client).0 };
679682
client_ref.link(target, source, link_type).await
680683
});
@@ -720,7 +723,7 @@ pub unsafe extern "C" fn afc_rename_path(
720723
Err(_) => return ffi_err!(IdeviceError::FfiInvalidArg),
721724
};
722725

723-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
726+
let res: Result<(), IdeviceError> = run_sync_local(async move {
724727
let client_ref = unsafe { &mut (*client).0 };
725728
client_ref.rename(source, target).await
726729
});

ffi/src/amfi.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use std::ptr::null_mut;
44

55
use idevice::{IdeviceError, IdeviceService, amfi::AmfiClient, provider::IdeviceProvider};
66

7-
use crate::{IdeviceFfiError, IdeviceHandle, RUNTIME, ffi_err, provider::IdeviceProviderHandle};
7+
use crate::{
8+
IdeviceFfiError, IdeviceHandle, ffi_err, provider::IdeviceProviderHandle, run_sync_local,
9+
};
810

911
pub struct AmfiClientHandle(pub AmfiClient);
1012

@@ -30,7 +32,7 @@ pub unsafe extern "C" fn amfi_connect(
3032
return ffi_err!(IdeviceError::FfiInvalidArg);
3133
}
3234

33-
let res: Result<AmfiClient, IdeviceError> = RUNTIME.block_on(async move {
35+
let res: Result<AmfiClient, IdeviceError> = run_sync_local(async move {
3436
let provider_ref: &dyn IdeviceProvider = unsafe { &*(*provider).0 };
3537

3638
// Connect using the reference
@@ -94,7 +96,7 @@ pub unsafe extern "C" fn amfi_reveal_developer_mode_option_in_ui(
9496
return ffi_err!(IdeviceError::FfiInvalidArg);
9597
}
9698

97-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
99+
let res: Result<(), IdeviceError> = run_sync_local(async move {
98100
let client_ref = unsafe { &mut (*client).0 };
99101
client_ref.reveal_developer_mode_option_in_ui().await
100102
});
@@ -122,7 +124,7 @@ pub unsafe extern "C" fn amfi_enable_developer_mode(
122124
return ffi_err!(IdeviceError::FfiInvalidArg);
123125
}
124126

125-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
127+
let res: Result<(), IdeviceError> = run_sync_local(async move {
126128
let client_ref = unsafe { &mut (*client).0 };
127129
client_ref.enable_developer_mode().await
128130
});
@@ -150,7 +152,7 @@ pub unsafe extern "C" fn amfi_accept_developer_mode(
150152
return ffi_err!(IdeviceError::FfiInvalidArg);
151153
}
152154

153-
let res: Result<(), IdeviceError> = RUNTIME.block_on(async move {
155+
let res: Result<(), IdeviceError> = run_sync_local(async move {
154156
let client_ref = unsafe { &mut (*client).0 };
155157
client_ref.accept_developer_mode().await
156158
});

ffi/src/core_device/app_service.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use idevice::{IdeviceError, ReadWrite, RsdService};
99

1010
use crate::core_device_proxy::AdapterHandle;
1111
use crate::rsd::RsdHandshakeHandle;
12-
use crate::{IdeviceFfiError, RUNTIME, ReadWriteOpaque, ffi_err};
12+
use crate::{IdeviceFfiError, ReadWriteOpaque, ffi_err, run_sync, run_sync_local};
1313

1414
/// Opaque handle to an AppServiceClient
1515
pub struct AppServiceHandle(pub AppServiceClient<Box<dyn ReadWrite>>);
@@ -91,7 +91,7 @@ pub unsafe extern "C" fn app_service_connect_rsd(
9191
}
9292

9393
let res: Result<AppServiceClient<Box<dyn ReadWrite>>, IdeviceError> =
94-
RUNTIME.block_on(async move {
94+
run_sync_local(async move {
9595
let provider_ref = unsafe { &mut (*provider).0 };
9696
let handshake_ref = unsafe { &mut (*handshake).0 };
9797

@@ -130,7 +130,7 @@ pub unsafe extern "C" fn app_service_new(
130130
}
131131

132132
let socket = unsafe { Box::from_raw(socket) };
133-
let res = RUNTIME.block_on(async move { AppServiceClient::new(socket.inner.unwrap()).await });
133+
let res = run_sync(async move { AppServiceClient::new(socket.inner.unwrap()).await });
134134

135135
match res {
136136
Ok(client) => {
@@ -186,7 +186,7 @@ pub unsafe extern "C" fn app_service_list_apps(
186186
}
187187

188188
let client = unsafe { &mut (*handle).0 };
189-
let res = RUNTIME.block_on(async move {
189+
let res = run_sync(async move {
190190
client
191191
.list_apps(
192192
app_clips != 0,
@@ -347,7 +347,7 @@ pub unsafe extern "C" fn app_service_launch_app(
347347
};
348348

349349
let client = unsafe { &mut (*handle).0 };
350-
let res = RUNTIME.block_on(async move {
350+
let res = run_sync(async move {
351351
client
352352
.launch_application(
353353
bundle_id_str,
@@ -430,7 +430,7 @@ pub unsafe extern "C" fn app_service_list_processes(
430430
}
431431

432432
let client = unsafe { &mut (*handle).0 };
433-
let res = RUNTIME.block_on(async move { client.list_processes().await });
433+
let res = run_sync(async move { client.list_processes().await });
434434

435435
match res {
436436
Ok(process_list) => {
@@ -513,7 +513,7 @@ pub unsafe extern "C" fn app_service_uninstall_app(
513513
};
514514

515515
let client = unsafe { &mut (*handle).0 };
516-
let res = RUNTIME.block_on(async move { client.uninstall_app(bundle_id_str).await });
516+
let res = run_sync(async move { client.uninstall_app(bundle_id_str).await });
517517

518518
match res {
519519
Ok(_) => null_mut(),
@@ -546,7 +546,7 @@ pub unsafe extern "C" fn app_service_send_signal(
546546
}
547547

548548
let client = unsafe { &mut (*handle).0 };
549-
let res = RUNTIME.block_on(async move { client.send_signal(pid, signal).await });
549+
let res = run_sync(async move { client.send_signal(pid, signal).await });
550550

551551
match res {
552552
Ok(signal_response) => {
@@ -628,7 +628,7 @@ pub unsafe extern "C" fn app_service_fetch_app_icon(
628628
};
629629

630630
let client = unsafe { &mut (*handle).0 };
631-
let res = RUNTIME.block_on(async move {
631+
let res = run_sync(async move {
632632
client
633633
.fetch_app_icon(bundle_id_str, width, height, scale, allow_placeholder != 0)
634634
.await

0 commit comments

Comments
 (0)