2023-04-24 18:45:22 +08:00
|
|
|
use hbb_common::{libc, ResultType};
|
2023-05-04 18:02:34 +08:00
|
|
|
use std::{
|
|
|
|
ffi::{c_char, c_void, CStr},
|
|
|
|
ptr::null,
|
|
|
|
};
|
2023-04-18 23:02:37 +08:00
|
|
|
|
2023-05-03 22:37:43 +08:00
|
|
|
mod callback_ext;
|
2023-05-04 18:02:34 +08:00
|
|
|
mod callback_msg;
|
2023-04-18 23:02:37 +08:00
|
|
|
mod config;
|
|
|
|
pub mod desc;
|
2023-04-19 17:06:59 +08:00
|
|
|
mod errno;
|
2023-04-23 15:40:55 +08:00
|
|
|
pub mod ipc;
|
2023-04-27 23:37:13 +08:00
|
|
|
pub mod native;
|
2023-05-04 13:18:19 +08:00
|
|
|
pub mod native_handlers;
|
2023-05-04 18:02:34 +08:00
|
|
|
mod plog;
|
|
|
|
mod plugins;
|
2023-04-18 23:02:37 +08:00
|
|
|
|
2023-04-19 17:06:59 +08:00
|
|
|
pub use plugins::{
|
2023-04-27 11:24:19 +08:00
|
|
|
handle_client_event, handle_listen_event, handle_server_event, handle_ui_event, load_plugin,
|
|
|
|
load_plugins, reload_plugin, sync_ui, unload_plugin, unload_plugins,
|
2023-04-19 17:06:59 +08:00
|
|
|
};
|
2023-04-18 23:02:37 +08:00
|
|
|
|
2023-04-21 21:40:34 +08:00
|
|
|
const MSG_TO_UI_TYPE_PLUGIN_DESC: &str = "plugin_desc";
|
|
|
|
const MSG_TO_UI_TYPE_PLUGIN_EVENT: &str = "plugin_event";
|
|
|
|
const MSG_TO_UI_TYPE_PLUGIN_RELOAD: &str = "plugin_reload";
|
|
|
|
const MSG_TO_UI_TYPE_PLUGIN_OPTION: &str = "plugin_option";
|
|
|
|
|
2023-04-27 11:24:19 +08:00
|
|
|
pub const EVENT_ON_CONN_CLIENT: &str = "on_conn_client";
|
|
|
|
pub const EVENT_ON_CONN_SERVER: &str = "on_conn_server";
|
|
|
|
pub const EVENT_ON_CONN_CLOSE_CLIENT: &str = "on_conn_close_client";
|
|
|
|
pub const EVENT_ON_CONN_CLOSE_SERVER: &str = "on_conn_close_server";
|
|
|
|
|
2023-04-23 15:40:55 +08:00
|
|
|
pub use config::{ManagerConfig, PeerConfig, SharedConfig};
|
2023-04-20 20:57:47 +08:00
|
|
|
|
2023-05-04 18:02:34 +08:00
|
|
|
/// The common return of plugin.
|
|
|
|
///
|
|
|
|
/// [Note]
|
|
|
|
/// msg must be null if code is ERR_SUCCESS.
|
|
|
|
/// The msg must be freed by caller if it is not null.
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct PluginReturn {
|
|
|
|
pub code: i32,
|
|
|
|
pub msg: *const c_char,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginReturn {
|
|
|
|
pub fn success() -> Self {
|
|
|
|
PluginReturn {
|
|
|
|
code: errno::ERR_SUCCESS,
|
|
|
|
msg: null(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(code: i32, msg: &str) -> Self {
|
|
|
|
let mut msg = msg.as_bytes().to_vec();
|
|
|
|
msg.push(0);
|
|
|
|
let p = unsafe {
|
|
|
|
let p = libc::malloc(msg.len()) as *mut c_char;
|
|
|
|
libc::memcpy(
|
|
|
|
p as *mut libc::c_void,
|
|
|
|
msg.as_ptr() as *const libc::c_void,
|
|
|
|
msg.len(),
|
|
|
|
);
|
|
|
|
p as *const c_char
|
|
|
|
};
|
|
|
|
Self { code, msg: p }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_success(&self) -> bool {
|
|
|
|
self.code == errno::ERR_SUCCESS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 23:02:37 +08:00
|
|
|
#[inline]
|
|
|
|
fn cstr_to_string(cstr: *const c_char) -> ResultType<String> {
|
|
|
|
Ok(String::from_utf8(unsafe {
|
|
|
|
CStr::from_ptr(cstr).to_bytes().to_vec()
|
|
|
|
})?)
|
|
|
|
}
|
2023-04-19 17:06:59 +08:00
|
|
|
|
2023-04-24 18:45:22 +08:00
|
|
|
#[inline]
|
2023-04-27 11:24:19 +08:00
|
|
|
fn str_to_cstr_ret(s: &str) -> *const c_char {
|
2023-04-24 18:45:22 +08:00
|
|
|
let mut s = s.as_bytes().to_vec();
|
|
|
|
s.push(0);
|
|
|
|
unsafe {
|
|
|
|
let r = libc::malloc(s.len()) as *mut c_char;
|
|
|
|
libc::memcpy(
|
|
|
|
r as *mut libc::c_void,
|
|
|
|
s.as_ptr() as *const libc::c_void,
|
|
|
|
s.len(),
|
|
|
|
);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn free_c_ptr(ret: *mut c_void) {
|
|
|
|
if !ret.is_null() {
|
|
|
|
unsafe {
|
|
|
|
libc::free(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|