mirror of
https://github.com/jkcoxson/idevice.git
synced 2026-03-02 14:36:16 +01:00
Use opaque handle for readwrite objects
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
// Jackson Coxson
|
||||
|
||||
use crate::{IdeviceErrorCode, RUNTIME};
|
||||
use crate::core_device_proxy::AdapterHandle;
|
||||
use crate::rsd::RsdHandshakeHandle;
|
||||
use crate::{IdeviceErrorCode, RUNTIME, ReadWriteOpaque};
|
||||
use idevice::dvt::remote_server::RemoteServerClient;
|
||||
use idevice::{IdeviceError, ReadWrite};
|
||||
use idevice::tcp::stream::AdapterStream;
|
||||
use idevice::{IdeviceError, ReadWrite, RsdService};
|
||||
|
||||
/// Opaque handle to a RemoteServerClient
|
||||
pub struct RemoteServerHandle(pub RemoteServerClient<Box<dyn ReadWrite>>);
|
||||
@@ -17,25 +20,29 @@ pub struct RemoteServerHandle(pub RemoteServerClient<Box<dyn ReadWrite>>);
|
||||
/// An error code indicating success or failure
|
||||
///
|
||||
/// # Safety
|
||||
/// `socket` must be a valid pointer to a handle allocated by this library
|
||||
/// `socket` must be a valid pointer to a handle allocated by this library. It is consumed and may
|
||||
/// not be used again.
|
||||
/// `handle` must be a valid pointer to a location where the handle will be stored
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn remote_server_new(
|
||||
socket: *mut Box<dyn ReadWrite>,
|
||||
socket: *mut ReadWriteOpaque,
|
||||
handle: *mut *mut RemoteServerHandle,
|
||||
) -> IdeviceErrorCode {
|
||||
if socket.is_null() {
|
||||
return IdeviceErrorCode::InvalidArg;
|
||||
}
|
||||
|
||||
let connection = unsafe { Box::from_raw(socket) };
|
||||
let wrapper = unsafe { &mut *socket };
|
||||
|
||||
let res: Result<RemoteServerClient<Box<dyn ReadWrite>>, IdeviceError> =
|
||||
RUNTIME.block_on(async move {
|
||||
let mut client = RemoteServerClient::new(*connection);
|
||||
client.read_message(0).await?; // Until Message has bindings, we'll do the first read
|
||||
Ok(client)
|
||||
});
|
||||
match wrapper.inner.take() {
|
||||
Some(stream) => RUNTIME.block_on(async move {
|
||||
let mut client = RemoteServerClient::new(stream);
|
||||
client.read_message(0).await?;
|
||||
Ok(client)
|
||||
}),
|
||||
None => return IdeviceErrorCode::InvalidArg,
|
||||
};
|
||||
|
||||
match res {
|
||||
Ok(client) => {
|
||||
@@ -47,6 +54,48 @@ pub unsafe extern "C" fn remote_server_new(
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new RemoteServerClient from a handshake and adapter
|
||||
///
|
||||
/// # Arguments
|
||||
/// * [`provider`] - An adapter created by this library
|
||||
/// * [`handshake`] - An RSD handshake from the same provider
|
||||
///
|
||||
/// # Returns
|
||||
/// An error code indicating success or failure
|
||||
///
|
||||
/// # Safety
|
||||
/// `provider` must be a valid pointer to a handle allocated by this library
|
||||
/// `handshake` must be a valid pointer to a location where the handle will be stored
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn remote_server_connect_rsd(
|
||||
provider: *mut AdapterHandle,
|
||||
handshake: *mut RsdHandshakeHandle,
|
||||
handle: *mut *mut RemoteServerHandle,
|
||||
) -> IdeviceErrorCode {
|
||||
if provider.is_null() || handshake.is_null() || handshake.is_null() {
|
||||
return IdeviceErrorCode::InvalidArg;
|
||||
}
|
||||
let res: Result<RemoteServerClient<AdapterStream>, IdeviceError> =
|
||||
RUNTIME.block_on(async move {
|
||||
let provider_ref = unsafe { &mut (*provider).0 };
|
||||
let handshake_ref = unsafe { &mut (*handshake).0 };
|
||||
|
||||
// Connect using the reference
|
||||
RemoteServerClient::connect_rsd(provider_ref, handshake_ref).await
|
||||
});
|
||||
|
||||
match res {
|
||||
Ok(d) => {
|
||||
let boxed = Box::new(RemoteServerHandle(RemoteServerClient::new(Box::new(
|
||||
d.into_inner(),
|
||||
))));
|
||||
unsafe { *handle = Box::into_raw(boxed) };
|
||||
IdeviceErrorCode::IdeviceSuccess
|
||||
}
|
||||
Err(e) => e.into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Frees a RemoteServerClient handle
|
||||
///
|
||||
/// # Arguments
|
||||
|
||||
Reference in New Issue
Block a user