feat: add native call

This commit is contained in:
Kingtous 2023-04-28 13:46:43 +08:00
parent 3774f8308f
commit c140bcfed6
4 changed files with 159 additions and 30 deletions

View File

@ -48,13 +48,6 @@ mod license;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
mod port_forward;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
#[cfg(any(feature = "flutter"))]
pub mod api;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
#[cfg(any(feature = "flutter"))]
pub mod plugins;
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub mod plugin;

View File

@ -0,0 +1,27 @@
#[macro_export]
macro_rules! return_if_not_method {
($call: ident, $prefix: ident) => {
if $call.starts_with($prefix) {
return None;
}
};
}
#[macro_export]
macro_rules! call_if_method {
($call: ident ,$method: literal, $block: block) => {
if ($call != $method) {
$block
}
};
}
#[macro_export]
macro_rules! define_method_prefix {
($prefix: literal) => {
#[inline]
fn method_prefix(&self) -> &'static str {
$prefix
}
};
}

View File

@ -1,24 +1,126 @@
use std::ffi::{c_void, c_ulonglong, c_ulong};
use std::{
ffi::c_void,
sync::{Arc, RwLock},
vec,
};
use hbb_common::libc::c_char;
use lazy_static::lazy_static;
use serde_json::Map;
use crate::return_if_not_method;
use self::session::PluginNativeSessionHandler;
use super::cstr_to_string;
mod macros;
pub mod session;
pub type NR = super::native::NativeReturnValue;
pub type PluginNativeHandlerRegistrar = NativeHandlerRegistrar<dyn PluginNativeHandler>;
pub type PluginNativeHandlerRegistrar = NativeHandlerRegistrar<Box<dyn Callable + Send + Sync>>;
pub struct NativeHandlerRegistrar<H>{
handlers: Vec<H>
lazy_static! {
static ref NATIVE_HANDLERS_REGISTRAR: Arc<PluginNativeHandlerRegistrar> =
Arc::new(PluginNativeHandlerRegistrar::default());
}
pub(crate) trait PluginNativeHandler {
#[derive(Clone)]
pub struct NativeHandlerRegistrar<H> {
handlers: Arc<RwLock<Vec<H>>>,
}
impl Default for PluginNativeHandlerRegistrar {
fn default() -> Self {
Self {
handlers: Arc::new(RwLock::new(vec![Box::new(
PluginNativeSessionHandler::default(),
)])),
}
}
}
pub(self) trait PluginNativeHandler {
/// The method prefix handled by this handler.s
fn method_prefix(&self) -> &'static str;
/// Try to handle the method with the given data.
///
///
/// Returns: None for the message does not be handled by this handler.
fn on_message(method: &String, data: &Map<String, serde_json::Value>) -> Option<NR>;
fn on_message(&self, method: &str, data: &Map<String, serde_json::Value>) -> Option<NR>;
/// Try to handle the method with the given data and extra void binary data.
///
///
/// Returns: None for the message does not be handled by this handler.
fn on_message_raw(method: &String, data: &Map<String, serde_json::Value>, raw: *const c_void, raw_len: usize) -> Option<NR>;
}
fn on_message_raw(
&self,
method: &str,
data: &Map<String, serde_json::Value>,
raw: *const c_void,
raw_len: usize,
) -> Option<NR>;
}
pub(crate) trait Callable {
fn call(
&self,
method: &String,
json: *const c_char,
raw: *const c_void,
raw_len: usize,
) -> Option<NR> {
None
}
}
impl<T> Callable for T
where
T: PluginNativeHandler + Send + Sync,
{
fn call(
&self,
method: &String,
json: *const c_char,
raw: *const c_void,
raw_len: usize,
) -> Option<NR> {
let prefix = self.method_prefix();
return_if_not_method!(method, prefix);
match cstr_to_string(json) {
Ok(s) => {
if let Ok(json) = serde_json::from_str(s.as_str()) {
let method_suffix = &method[prefix.len()..];
if raw != std::ptr::null() && raw_len > 0 {
return self.on_message_raw(method_suffix, &json, raw, raw_len);
} else {
return self.on_message(method_suffix, &json);
}
} else {
return None;
}
}
Err(_) => return None,
}
}
}
impl<C> Callable for NativeHandlerRegistrar<C>
where
C: Callable,
{
fn call(
&self,
method: &String,
json: *const c_char,
raw: *const c_void,
raw_len: usize,
) -> Option<NR> {
for handler in self.handlers.read().unwrap().iter() {
let ret = handler.call(method, json, raw, raw_len);
if ret.is_some() {
return ret;
}
}
None
}
}

View File

@ -1,28 +1,35 @@
use crate::{call_if_method, define_method_prefix, return_if_not_method};
use super::PluginNativeHandler;
#[derive(Default)]
/// Session related handler for librustdesk core.
pub struct PluginNativeSessionHandler;
impl PluginNativeHandler for PluginNativeSessionHandler {
fn on_message(method: &String, data: &serde_json::Map<String, serde_json::Value>) -> Option<super::NR> {
define_method_prefix!("session_");
fn on_message(
&self,
method: &str,
data: &serde_json::Map<String, serde_json::Value>,
) -> Option<super::NR> {
None
}
fn on_message_raw(method: &String, data: &serde_json::Map<String, serde_json::Value>, raw: *const std::ffi::c_void, raw_len: usize) -> Option<super::NR> {
fn on_message_raw(
&self,
method: &str,
data: &serde_json::Map<String, serde_json::Value>,
raw: *const std::ffi::c_void,
raw_len: usize,
) -> Option<super::NR> {
None
}
}
impl PluginNativeSessionHandler {
fn create_session() {
fn create_session() {}
}
fn add_session_hook() {
}
}
fn add_session_hook() {}
}