2023-04-28 13:46:43 +08:00
|
|
|
use std::{
|
|
|
|
ffi::c_void,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
vec,
|
|
|
|
};
|
2023-04-27 23:37:13 +08:00
|
|
|
|
2023-04-28 13:46:43 +08:00
|
|
|
use hbb_common::libc::c_char;
|
|
|
|
use lazy_static::lazy_static;
|
2023-04-27 23:37:13 +08:00
|
|
|
use serde_json::Map;
|
|
|
|
|
2023-04-28 13:46:43 +08:00
|
|
|
use crate::return_if_not_method;
|
|
|
|
|
|
|
|
use self::session::PluginNativeSessionHandler;
|
|
|
|
|
|
|
|
use super::cstr_to_string;
|
|
|
|
|
|
|
|
mod macros;
|
2023-04-27 23:37:13 +08:00
|
|
|
pub mod session;
|
|
|
|
|
|
|
|
pub type NR = super::native::NativeReturnValue;
|
2023-04-28 13:46:43 +08:00
|
|
|
pub type PluginNativeHandlerRegistrar = NativeHandlerRegistrar<Box<dyn Callable + Send + Sync>>;
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref NATIVE_HANDLERS_REGISTRAR: Arc<PluginNativeHandlerRegistrar> =
|
|
|
|
Arc::new(PluginNativeHandlerRegistrar::default());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct NativeHandlerRegistrar<H> {
|
|
|
|
handlers: Arc<RwLock<Vec<H>>>,
|
|
|
|
}
|
2023-04-27 23:37:13 +08:00
|
|
|
|
2023-04-28 13:46:43 +08:00
|
|
|
impl Default for PluginNativeHandlerRegistrar {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
handlers: Arc::new(RwLock::new(vec![Box::new(
|
|
|
|
PluginNativeSessionHandler::default(),
|
|
|
|
)])),
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 23:37:13 +08:00
|
|
|
}
|
|
|
|
|
2023-04-28 13:46:43 +08:00
|
|
|
pub(self) trait PluginNativeHandler {
|
|
|
|
/// The method prefix handled by this handler.s
|
|
|
|
fn method_prefix(&self) -> &'static str;
|
|
|
|
|
2023-04-27 23:37:13 +08:00
|
|
|
/// Try to handle the method with the given data.
|
2023-04-28 13:46:43 +08:00
|
|
|
///
|
2023-04-27 23:37:13 +08:00
|
|
|
/// Returns: None for the message does not be handled by this handler.
|
2023-04-28 13:46:43 +08:00
|
|
|
fn on_message(&self, method: &str, data: &Map<String, serde_json::Value>) -> Option<NR>;
|
2023-04-27 23:37:13 +08:00
|
|
|
|
|
|
|
/// Try to handle the method with the given data and extra void binary data.
|
2023-04-28 13:46:43 +08:00
|
|
|
///
|
2023-04-27 23:37:13 +08:00
|
|
|
/// Returns: None for the message does not be handled by this handler.
|
2023-04-28 13:46:43 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|