feat: add cb for ffi

This commit is contained in:
Kingtous 2023-04-28 15:09:45 +08:00
parent 952598af25
commit 5c0f249390
3 changed files with 38 additions and 14 deletions

View File

@ -17,6 +17,8 @@ pub const ERR_CALL_NOT_SUPPORTED_METHOD: i32 = 10202;
// failed on calling // failed on calling
pub const ERR_CALL_INVALID_ARGS: i32 = 10301; pub const ERR_CALL_INVALID_ARGS: i32 = 10301;
pub const ERR_PEER_ID_MISMATCH: i32 = 10302; pub const ERR_PEER_ID_MISMATCH: i32 = 10302;
// no handlers on calling
pub const ERR_NOT_HANDLED: i32 = 10401;
// ====================================================== // ======================================================
// errors that should be handled by the plugin // errors that should be handled by the plugin

View File

@ -1,16 +1,40 @@
use std::{ffi::{c_char, c_void}, os::raw::c_uint}; use std::{
ffi::{c_char, c_int, c_void},
os::raw::c_uint,
};
use hbb_common::log::error;
use super::{
cstr_to_string,
errno::ERR_NOT_HANDLED,
native_handlers::{Callable, NATIVE_HANDLERS_REGISTRAR},
};
/// The native returned value from librustdesk native. /// The native returned value from librustdesk native.
/// ///
/// [Note] /// [Note]
/// The data is owned by librustdesk. /// The data is owned by librustdesk.
#[repr(C)] #[repr(C)]
pub struct NativeReturnValue{ pub struct NativeReturnValue {
pub return_type: c_uint, pub return_type: c_int,
pub data: *const c_void pub data: *const c_void,
} }
pub(super) extern "C" fn cb_native_data(method: *const c_char, json: *const c_char, raw: *const c_void, raw_len: usize) -> NativeReturnValue { pub(super) extern "C" fn cb_native_data(
// TODO: cb for native data. method: *const c_char,
return NativeReturnValue { return_type: 0, data: std::ptr::null() }; json: *const c_char,
} raw: *const c_void,
raw_len: usize,
) -> NativeReturnValue {
let ret = match cstr_to_string(method) {
Ok(method) => NATIVE_HANDLERS_REGISTRAR.call(&method, json, raw, raw_len),
Err(err) => {
error!("cb_native_data error: {}", err);
None
}
};
return ret.unwrap_or(NativeReturnValue {
return_type: ERR_NOT_HANDLED,
data: std::ptr::null(),
});
}

View File

@ -21,7 +21,7 @@ pub type NR = super::native::NativeReturnValue;
pub type PluginNativeHandlerRegistrar = NativeHandlerRegistrar<Box<dyn Callable + Send + Sync>>; pub type PluginNativeHandlerRegistrar = NativeHandlerRegistrar<Box<dyn Callable + Send + Sync>>;
lazy_static! { lazy_static! {
static ref NATIVE_HANDLERS_REGISTRAR: Arc<PluginNativeHandlerRegistrar> = pub static ref NATIVE_HANDLERS_REGISTRAR: Arc<PluginNativeHandlerRegistrar> =
Arc::new(PluginNativeHandlerRegistrar::default()); Arc::new(PluginNativeHandlerRegistrar::default());
} }
@ -34,6 +34,7 @@ impl Default for PluginNativeHandlerRegistrar {
fn default() -> Self { fn default() -> Self {
Self { Self {
handlers: Arc::new(RwLock::new(vec![Box::new( handlers: Arc::new(RwLock::new(vec![Box::new(
// Add prebuilt native handlers here.
PluginNativeSessionHandler::default(), PluginNativeSessionHandler::default(),
)])), )])),
} }
@ -104,10 +105,7 @@ where
} }
} }
impl<C> Callable for NativeHandlerRegistrar<C> impl Callable for PluginNativeHandlerRegistrar {
where
C: Callable,
{
fn call( fn call(
&self, &self,
method: &String, method: &String,