From 5c0f2493902462445f8e718eda345e04366d101a Mon Sep 17 00:00:00 2001 From: Kingtous Date: Fri, 28 Apr 2023 15:09:45 +0800 Subject: [PATCH] feat: add cb for ffi --- src/plugin/errno.rs | 2 ++ src/plugin/native.rs | 42 ++++++++++++++++++++++++------- src/plugin/native_handlers/mod.rs | 8 +++--- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/plugin/errno.rs b/src/plugin/errno.rs index db580c0bd..f62a3daac 100644 --- a/src/plugin/errno.rs +++ b/src/plugin/errno.rs @@ -17,6 +17,8 @@ pub const ERR_CALL_NOT_SUPPORTED_METHOD: i32 = 10202; // failed on calling pub const ERR_CALL_INVALID_ARGS: i32 = 10301; 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 diff --git a/src/plugin/native.rs b/src/plugin/native.rs index 3b5bcb2e4..ce885c77c 100644 --- a/src/plugin/native.rs +++ b/src/plugin/native.rs @@ -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. -/// +/// /// [Note] /// The data is owned by librustdesk. #[repr(C)] -pub struct NativeReturnValue{ - pub return_type: c_uint, - pub data: *const c_void +pub struct NativeReturnValue { + pub return_type: c_int, + 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 { - // TODO: cb for native data. - return NativeReturnValue { return_type: 0, data: std::ptr::null() }; -} \ No newline at end of file +pub(super) extern "C" fn cb_native_data( + method: *const c_char, + 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(), + }); +} diff --git a/src/plugin/native_handlers/mod.rs b/src/plugin/native_handlers/mod.rs index 3aa0868e1..32b6627e2 100644 --- a/src/plugin/native_handlers/mod.rs +++ b/src/plugin/native_handlers/mod.rs @@ -21,7 +21,7 @@ pub type NR = super::native::NativeReturnValue; pub type PluginNativeHandlerRegistrar = NativeHandlerRegistrar>; lazy_static! { - static ref NATIVE_HANDLERS_REGISTRAR: Arc = + pub static ref NATIVE_HANDLERS_REGISTRAR: Arc = Arc::new(PluginNativeHandlerRegistrar::default()); } @@ -34,6 +34,7 @@ impl Default for PluginNativeHandlerRegistrar { fn default() -> Self { Self { handlers: Arc::new(RwLock::new(vec![Box::new( + // Add prebuilt native handlers here. PluginNativeSessionHandler::default(), )])), } @@ -104,10 +105,7 @@ where } } -impl Callable for NativeHandlerRegistrar -where - C: Callable, -{ +impl Callable for PluginNativeHandlerRegistrar { fn call( &self, method: &String,