plugin_framework, return C structure

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-05-05 13:37:43 +08:00
parent 357028b222
commit 8f0d696304
5 changed files with 95 additions and 108 deletions

View File

@ -3,7 +3,6 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
use super::*; use super::*;
use std::{ffi::c_void, ptr::null};
const EXT_SUPPORT_BLOCK_INPUT: &str = "block-input"; const EXT_SUPPORT_BLOCK_INPUT: &str = "block-input";
@ -11,7 +10,7 @@ pub(super) fn ext_support_callback(
id: &str, id: &str,
peer: &str, peer: &str,
msg: &super::callback_msg::MsgToExtSupport, msg: &super::callback_msg::MsgToExtSupport,
) -> *const c_void { ) -> PluginReturn {
match &msg.r#type as _ { match &msg.r#type as _ {
EXT_SUPPORT_BLOCK_INPUT => { EXT_SUPPORT_BLOCK_INPUT => {
// let supported_plugins = []; // let supported_plugins = [];
@ -19,25 +18,25 @@ pub(super) fn ext_support_callback(
let supported = true; let supported = true;
if supported { if supported {
if msg.data.len() != 1 { if msg.data.len() != 1 {
return make_return_code_msg( return PluginReturn::new(
errno::ERR_CALLBACK_INVALID_ARGS, errno::ERR_CALLBACK_INVALID_ARGS,
"Invalid data length", "Invalid data length",
); );
} }
let block = msg.data[0] != 0; let block = msg.data[0] != 0;
if crate::server::plugin_block_input(peer, block) == block { if crate::server::plugin_block_input(peer, block) == block {
null() PluginReturn::success()
} else { } else {
make_return_code_msg(errno::ERR_CALLBACK_FAILED, "") PluginReturn::new(errno::ERR_CALLBACK_FAILED, "")
} }
} else { } else {
make_return_code_msg( PluginReturn::new(
errno::ERR_CALLBACK_PLUGIN_ID, errno::ERR_CALLBACK_PLUGIN_ID,
&format!("This operation is not supported for plugin '{}', please contact the RustDesk team for support.", id), &format!("This operation is not supported for plugin '{}', please contact the RustDesk team for support.", id),
) )
} }
} }
_ => make_return_code_msg( _ => PluginReturn::new(
errno::ERR_CALLBACK_TARGET_TYPE, errno::ERR_CALLBACK_TARGET_TYPE,
&format!("Unknown target type '{}'", &msg.r#type), &format!("Unknown target type '{}'", &msg.r#type),
), ),

View File

@ -6,7 +6,6 @@ use serde_json;
use std::{ use std::{
collections::HashMap, collections::HashMap,
ffi::{c_char, c_void}, ffi::{c_char, c_void},
ptr::null,
sync::Arc, sync::Arc,
}; };
@ -63,11 +62,9 @@ macro_rules! cb_msg_field {
($field: ident) => { ($field: ident) => {
let $field = match cstr_to_string($field) { let $field = match cstr_to_string($field) {
Err(e) => { Err(e) => {
log::error!("Failed to convert {} to string, {}", stringify!($field), e); let msg = format!("Failed to convert {} to string, {}", stringify!($field), e);
return make_return_code_msg( log::error!("{}", &msg);
errno::ERR_CALLBACK_INVALID_ARGS, return PluginReturn::new(errno::ERR_CALLBACK_INVALID_ARGS, &msg);
&format!("Failed to convert {} to string, {}", stringify!($field), e),
);
} }
Ok(v) => v, Ok(v) => v,
}; };
@ -77,7 +74,7 @@ macro_rules! cb_msg_field {
macro_rules! early_return_value { macro_rules! early_return_value {
($e:expr, $code: ident, $($arg:tt)*) => { ($e:expr, $code: ident, $($arg:tt)*) => {
match $e { match $e {
Err(e) => return make_return_code_msg( Err(e) => return PluginReturn::new(
errno::$code, errno::$code,
&format!("Failed to {} '{}'", format_args!($($arg)*), e), &format!("Failed to {} '{}'", format_args!($($arg)*), e),
), ),
@ -105,7 +102,7 @@ pub(super) extern "C" fn cb_msg(
id: *const c_char, id: *const c_char,
content: *const c_void, content: *const c_void,
len: usize, len: usize,
) -> *const c_void { ) -> PluginReturn {
cb_msg_field!(peer); cb_msg_field!(peer);
cb_msg_field!(target); cb_msg_field!(target);
cb_msg_field!(id); cb_msg_field!(id);
@ -122,9 +119,9 @@ pub(super) extern "C" fn cb_msg(
..Default::default() ..Default::default()
}; };
session.send_plugin_request(request); session.send_plugin_request(request);
null() PluginReturn::success()
} else { } else {
make_return_code_msg( PluginReturn::new(
errno::ERR_CALLBACK_PEER_NOT_FOUND, errno::ERR_CALLBACK_PEER_NOT_FOUND,
&format!("Failed to find session for peer '{}'", peer), &format!("Failed to find session for peer '{}'", peer),
) )
@ -136,7 +133,7 @@ pub(super) extern "C" fn cb_msg(
let content = std::string::String::from_utf8(content_slice[2..].to_vec()) let content = std::string::String::from_utf8(content_slice[2..].to_vec())
.unwrap_or("".to_string()); .unwrap_or("".to_string());
push_event_to_ui(channel, &peer, &content); push_event_to_ui(channel, &peer, &content);
null() PluginReturn::success()
} }
MSG_TO_CONFIG_TARGET => { MSG_TO_CONFIG_TARGET => {
let s = early_return_value!( let s = early_return_value!(
@ -162,7 +159,7 @@ pub(super) extern "C" fn cb_msg(
// No need to set the peer id for location config. // No need to set the peer id for location config.
push_option_to_ui(ui.channel, &id, "", &msg, ui); push_option_to_ui(ui.channel, &id, "", &msg, ui);
} }
null() PluginReturn::success()
} }
config::CONFIG_TYPE_PEER => { config::CONFIG_TYPE_PEER => {
let _r = early_return_value!( let _r = early_return_value!(
@ -173,9 +170,9 @@ pub(super) extern "C" fn cb_msg(
if let Some(ui) = &msg.ui { if let Some(ui) = &msg.ui {
push_option_to_ui(ui.channel, &id, &peer, &msg, ui); push_option_to_ui(ui.channel, &id, &peer, &msg, ui);
} }
null() PluginReturn::success()
} }
_ => make_return_code_msg( _ => PluginReturn::new(
errno::ERR_CALLBACK_TARGET_TYPE, errno::ERR_CALLBACK_TARGET_TYPE,
&format!("Unknown target type '{}'", &msg.r#type), &format!("Unknown target type '{}'", &msg.r#type),
), ),
@ -195,7 +192,7 @@ pub(super) extern "C" fn cb_msg(
); );
super::callback_ext::ext_support_callback(&id, &peer, &msg) super::callback_ext::ext_support_callback(&id, &peer, &msg)
} }
_ => make_return_code_msg( _ => PluginReturn::new(
errno::ERR_CALLBACK_TARGET, errno::ERR_CALLBACK_TARGET,
&format!("Unknown target '{}'", target), &format!("Unknown target '{}'", target),
), ),

View File

@ -1,16 +1,19 @@
use hbb_common::{libc, ResultType}; use hbb_common::{libc, ResultType};
use std::ffi::{c_char, c_void, CStr}; use std::{
ffi::{c_char, c_int, c_void, CStr},
ptr::null,
};
mod callback_msg;
mod callback_ext; mod callback_ext;
mod callback_msg;
mod config; mod config;
pub mod desc; pub mod desc;
mod errno; mod errno;
pub mod ipc; pub mod ipc;
mod plog;
mod plugins;
pub mod native; pub mod native;
pub mod native_handlers; pub mod native_handlers;
mod plog;
mod plugins;
pub use plugins::{ pub use plugins::{
handle_client_event, handle_listen_event, handle_server_event, handle_ui_event, load_plugin, handle_client_event, handle_listen_event, handle_server_event, handle_ui_event, load_plugin,
@ -29,6 +32,51 @@ pub const EVENT_ON_CONN_CLOSE_SERVER: &str = "on_conn_close_server";
pub use config::{ManagerConfig, PeerConfig, SharedConfig}; pub use config::{ManagerConfig, PeerConfig, SharedConfig};
/// Common plugin return.
///
/// [Note]
/// The msg must be nullptr if code is errno::ERR_SUCCESS.
/// The msg must be freed by caller if code is not errno::ERR_SUCCESS.
#[repr(C)]
#[derive(Debug)]
pub struct PluginReturn {
pub code: c_int,
pub msg: *const c_char,
}
impl PluginReturn {
pub fn success() -> Self {
Self {
code: errno::ERR_SUCCESS,
msg: null(),
}
}
#[inline]
pub fn is_success(&self) -> bool {
self.code == errno::ERR_SUCCESS
}
pub fn new(code: c_int, msg: &str) -> Self {
Self {
code,
msg: str_to_cstr_ret(msg),
}
}
pub fn get_code_msg(&mut self) -> (i32, String) {
if self.is_success() {
(self.code, "".to_owned())
} else {
assert!(!self.msg.is_null());
let msg = cstr_to_string(self.msg).unwrap_or_default();
free_c_ptr(self.msg as _);
self.msg = null();
(self.code as _, msg)
}
}
}
#[inline] #[inline]
fn cstr_to_string(cstr: *const c_char) -> ResultType<String> { fn cstr_to_string(cstr: *const c_char) -> ResultType<String> {
Ok(String::from_utf8(unsafe { Ok(String::from_utf8(unsafe {
@ -51,34 +99,6 @@ fn str_to_cstr_ret(s: &str) -> *const c_char {
} }
} }
#[inline]
pub fn make_return_code_msg(code: i32, msg: &str) -> *const c_void {
let mut out = code.to_le_bytes().to_vec();
out.extend(msg.as_bytes());
out.push(0);
unsafe {
let r = libc::malloc(out.len()) as *mut c_char;
libc::memcpy(
r as *mut libc::c_void,
out.as_ptr() as *const libc::c_void,
out.len(),
);
r as *const c_void
}
}
#[inline]
fn get_code_msg_from_ret(ret: *const c_void) -> (i32, String) {
assert!(ret.is_null() == false);
let code_bytes = unsafe { std::slice::from_raw_parts(ret as *const u8, 4) };
let code = i32::from_le_bytes([code_bytes[0], code_bytes[1], code_bytes[2], code_bytes[3]]);
let msg = unsafe { CStr::from_ptr((ret as *const u8).add(4) as _) }
.to_str()
.unwrap_or("")
.to_string();
(code, msg)
}
#[inline] #[inline]
fn free_c_ptr(ret: *mut c_void) { fn free_c_ptr(ret: *mut c_void) {
if !ret.is_null() { if !ret.is_null() {

View File

@ -34,25 +34,13 @@ struct PluginInfo {
/// Initialize the plugins. /// Initialize the plugins.
/// ///
/// data: The initialize data. /// data: The initialize data.
/// type PluginFuncInit = extern "C" fn(data: *const InitData) -> PluginReturn;
/// Return null ptr if success.
/// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string.
/// The plugin allocate memory with `libc::malloc` and return the pointer.
type PluginFuncInit = extern "C" fn(data: *const InitData) -> *const c_void;
/// Reset the plugin. /// Reset the plugin.
/// ///
/// data: The initialize data. /// data: The initialize data.
/// type PluginFuncReset = extern "C" fn(data: *const InitData) -> PluginReturn;
/// Return null ptr if success.
/// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string.
/// The plugin allocate memory with `libc::malloc` and return the pointer.
type PluginFuncReset = extern "C" fn(data: *const InitData) -> *const c_void;
/// Clear the plugin. /// Clear the plugin.
/// type PluginFuncClear = extern "C" fn() -> PluginReturn;
/// Return null ptr if success.
/// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string.
/// The plugin allocate memory with `libc::malloc` and return the pointer.
type PluginFuncClear = extern "C" fn() -> *const c_void;
/// Get the description of the plugin. /// Get the description of the plugin.
/// Return the description. The plugin allocate memory with `libc::malloc` and return the pointer. /// Return the description. The plugin allocate memory with `libc::malloc` and return the pointer.
type PluginFuncDesc = extern "C" fn() -> *const c_char; type PluginFuncDesc = extern "C" fn() -> *const c_char;
@ -64,17 +52,13 @@ type PluginFuncDesc = extern "C" fn() -> *const c_char;
/// id: The id of this plugin. /// id: The id of this plugin.
/// content: The content. /// content: The content.
/// len: The length of the content. /// len: The length of the content.
///
/// Return null ptr if success.
/// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string.
/// The plugin allocate memory with `libc::malloc` and return the pointer.
type CallbackMsg = extern "C" fn( type CallbackMsg = extern "C" fn(
peer: *const c_char, peer: *const c_char,
target: *const c_char, target: *const c_char,
id: *const c_char, id: *const c_char,
content: *const c_void, content: *const c_void,
len: usize, len: usize,
) -> *const c_void; ) -> PluginReturn;
/// Callback to get the config. /// Callback to get the config.
/// peer, key are utf8 strings(null terminated). /// peer, key are utf8 strings(null terminated).
/// ///
@ -114,16 +98,12 @@ type CallbackNative = extern "C" fn(
/// peer: The peer id. /// peer: The peer id.
/// args: The arguments. /// args: The arguments.
/// len: The length of the arguments. /// len: The length of the arguments.
///
/// Return null ptr if success.
/// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string.
/// The plugin allocate memory with `libc::malloc` and return the pointer.
type PluginFuncClientCall = extern "C" fn( type PluginFuncClientCall = extern "C" fn(
method: *const c_char, method: *const c_char,
peer: *const c_char, peer: *const c_char,
args: *const c_void, args: *const c_void,
len: usize, len: usize,
) -> *const c_void; ) -> PluginReturn;
/// The main function of the plugin on the server(remote) side. /// The main function of the plugin on the server(remote) side.
/// ///
/// method: The method. "handle_ui" or "handle_peer" /// method: The method. "handle_ui" or "handle_peer"
@ -133,10 +113,6 @@ type PluginFuncClientCall = extern "C" fn(
/// out: The output. /// out: The output.
/// The plugin allocate memory with `libc::malloc` and return the pointer. /// The plugin allocate memory with `libc::malloc` and return the pointer.
/// out_len: The length of the output. /// out_len: The length of the output.
///
/// Return null ptr if success.
/// Return the error message if failed. `i32-String` without dash, i32 is a signed little-endian number, the String is utf8 string.
/// The plugin allocate memory with `libc::malloc` and return the pointer.
type PluginFuncServerCall = extern "C" fn( type PluginFuncServerCall = extern "C" fn(
method: *const c_char, method: *const c_char,
peer: *const c_char, peer: *const c_char,
@ -144,7 +120,7 @@ type PluginFuncServerCall = extern "C" fn(
len: usize, len: usize,
out: *mut *mut c_void, out: *mut *mut c_void,
out_len: *mut usize, out_len: *mut usize,
) -> *const c_void; ) -> PluginReturn;
/// The plugin callbacks. /// The plugin callbacks.
/// msg: The callback to send message to peer or ui. /// msg: The callback to send message to peer or ui.
@ -157,7 +133,7 @@ struct Callbacks {
get_conf: CallbackGetConf, get_conf: CallbackGetConf,
get_id: CallbackGetId, get_id: CallbackGetId,
log: CallbackLog, log: CallbackLog,
// native: CallbackNative, native: CallbackNative,
} }
/// The plugin initialize data. /// The plugin initialize data.
@ -222,10 +198,9 @@ macro_rules! make_plugin {
} }
fn init(&self, data: &InitData, path: &str) -> ResultType<()> { fn init(&self, data: &InitData, path: &str) -> ResultType<()> {
let init_ret = (self.init)(data as _); let mut init_ret = (self.init)(data as _);
if !init_ret.is_null() { if !init_ret.is_success() {
let (code, msg) = get_code_msg_from_ret(init_ret); let (code, msg) = init_ret.get_code_msg();
free_c_ptr(init_ret as _);
bail!( bail!(
"Failed to init plugin {}, code: {}, msg: {}", "Failed to init plugin {}, code: {}, msg: {}",
path, path,
@ -237,10 +212,9 @@ macro_rules! make_plugin {
} }
fn clear(&self, id: &str) { fn clear(&self, id: &str) {
let clear_ret = (self.clear)(); let mut clear_ret = (self.clear)();
if !clear_ret.is_null() { if !clear_ret.is_success() {
let (code, msg) = get_code_msg_from_ret(clear_ret); let (code, msg) = clear_ret.get_code_msg();
free_c_ptr(clear_ret as _);
log::error!( log::error!(
"Failed to clear plugin {}, code: {}, msg: {}", "Failed to clear plugin {}, code: {}, msg: {}",
id, id,
@ -352,7 +326,7 @@ fn load_plugin_path(path: &str) -> ResultType<()> {
get_conf: config::cb_get_conf, get_conf: config::cb_get_conf,
get_id: config::cb_get_local_peer_id, get_id: config::cb_get_local_peer_id,
log: super::plog::plugin_log, log: super::plog::plugin_log,
// native: super::native::cb_native_data, native: super::native::cb_native_data,
}, },
}; };
plugin.init(&init_data, path)?; plugin.init(&init_data, path)?;
@ -407,17 +381,16 @@ fn handle_event(method: &[u8], id: &str, peer: &str, event: &[u8]) -> ResultType
peer.push('\0'); peer.push('\0');
match PLUGINS.read().unwrap().get(id) { match PLUGINS.read().unwrap().get(id) {
Some(plugin) => { Some(plugin) => {
let ret = (plugin.client_call)( let mut ret = (plugin.client_call)(
method.as_ptr() as _, method.as_ptr() as _,
peer.as_ptr() as _, peer.as_ptr() as _,
event.as_ptr() as _, event.as_ptr() as _,
event.len(), event.len(),
); );
if ret.is_null() { if ret.is_success() {
Ok(()) Ok(())
} else { } else {
let (code, msg) = get_code_msg_from_ret(ret); let (code, msg) = ret.get_code_msg();
free_c_ptr(ret as _);
bail!( bail!(
"Failed to handle plugin event, id: {}, method: {}, code: {}, msg: {}", "Failed to handle plugin event, id: {}, method: {}, code: {}, msg: {}",
id, id,
@ -463,15 +436,14 @@ fn _handle_listen_event(event: String, peer: String) {
for id in plugins { for id in plugins {
match PLUGINS.read().unwrap().get(&id) { match PLUGINS.read().unwrap().get(&id) {
Some(plugin) => { Some(plugin) => {
let ret = (plugin.client_call)( let mut ret = (plugin.client_call)(
METHOD_HANDLE_LISTEN_EVENT.as_ptr() as _, METHOD_HANDLE_LISTEN_EVENT.as_ptr() as _,
peer.as_ptr() as _, peer.as_ptr() as _,
evt_bytes.as_ptr() as _, evt_bytes.as_ptr() as _,
evt_bytes.len(), evt_bytes.len(),
); );
if !ret.is_null() { if !ret.is_success() {
let (code, msg) = get_code_msg_from_ret(ret); let (code, msg) = ret.get_code_msg();
free_c_ptr(ret as _);
log::error!( log::error!(
"Failed to handle plugin listen event, id: {}, event: {}, code: {}, msg: {}", "Failed to handle plugin listen event, id: {}, event: {}, code: {}, msg: {}",
id, id,
@ -502,7 +474,7 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message {
Some(plugin) => { Some(plugin) => {
let mut out = std::ptr::null_mut(); let mut out = std::ptr::null_mut();
let mut out_len: usize = 0; let mut out_len: usize = 0;
let ret = (plugin.server_call)( let mut ret = (plugin.server_call)(
METHOD_HANDLE_PEER.as_ptr() as _, METHOD_HANDLE_PEER.as_ptr() as _,
peer.as_ptr() as _, peer.as_ptr() as _,
event.as_ptr() as _, event.as_ptr() as _,
@ -510,13 +482,12 @@ pub fn handle_client_event(id: &str, peer: &str, event: &[u8]) -> Message {
&mut out as _, &mut out as _,
&mut out_len as _, &mut out_len as _,
); );
if ret.is_null() { if ret.is_success() {
let msg = make_plugin_request(id, out, out_len); let msg = make_plugin_request(id, out, out_len);
free_c_ptr(out as _); free_c_ptr(out as _);
msg msg
} else { } else {
let (code, msg) = get_code_msg_from_ret(ret); let (code, msg) = ret.get_code_msg();
free_c_ptr(ret as _);
if code > ERR_RUSTDESK_HANDLE_BASE && code < ERR_PLUGIN_HANDLE_BASE { if code > ERR_RUSTDESK_HANDLE_BASE && code < ERR_PLUGIN_HANDLE_BASE {
log::debug!( log::debug!(
"Plugin {} failed to handle client event, code: {}, msg: {}", "Plugin {} failed to handle client event, code: {}, msg: {}",

View File

@ -691,7 +691,7 @@ impl Connection {
} }
#[cfg(all(feature = "flutter", feature = "plugin_framework"))] #[cfg(all(feature = "flutter", feature = "plugin_framework"))]
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
MessageInput::BlockOnPlugin(peer) => { MessageInput::BlockOnPlugin(_peer) => {
if crate::platform::block_input(true) { if crate::platform::block_input(true) {
block_input_mode = true; block_input_mode = true;
} }
@ -703,7 +703,7 @@ impl Connection {
} }
#[cfg(all(feature = "flutter", feature = "plugin_framework"))] #[cfg(all(feature = "flutter", feature = "plugin_framework"))]
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
MessageInput::BlockOffPlugin(peer) => { MessageInput::BlockOffPlugin(_peer) => {
if crate::platform::block_input(false) { if crate::platform::block_input(false) {
block_input_mode = false; block_input_mode = false;
} }