mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-03-21 04:01:01 +03:00
plugin_framework, return C structure
Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
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_msg;
|
||||
mod config;
|
||||
pub mod desc;
|
||||
mod errno;
|
||||
pub mod ipc;
|
||||
mod plog;
|
||||
mod plugins;
|
||||
pub mod native;
|
||||
pub mod native_handlers;
|
||||
mod plog;
|
||||
mod plugins;
|
||||
|
||||
pub use plugins::{
|
||||
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};
|
||||
|
||||
/// 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]
|
||||
fn cstr_to_string(cstr: *const c_char) -> ResultType<String> {
|
||||
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]
|
||||
fn free_c_ptr(ret: *mut c_void) {
|
||||
if !ret.is_null() {
|
||||
|
||||
Reference in New Issue
Block a user