feat: add hooks for session

This commit is contained in:
Kingtous 2023-04-14 01:53:34 +08:00
parent 8cad0b7a6c
commit 8e70e2ffe4
3 changed files with 89 additions and 13 deletions

View File

@ -1,4 +1,4 @@
use librustdesk::{api::RustDeskApiTable}; use librustdesk::api::RustDeskApiTable;
/// This file demonstrates how to write a custom plugin for RustDesk. /// This file demonstrates how to write a custom plugin for RustDesk.
use std::ffi::{c_char, c_int, CString}; use std::ffi::{c_char, c_int, CString};
@ -9,8 +9,6 @@ lazy_static::lazy_static! {
pub static ref API: RustDeskApiTable = RustDeskApiTable::default(); pub static ref API: RustDeskApiTable = RustDeskApiTable::default();
} }
#[no_mangle] #[no_mangle]
fn plugin_name() -> *const c_char { fn plugin_name() -> *const c_char {
return PLUGIN_NAME.as_ptr(); return PLUGIN_NAME.as_ptr();

View File

@ -1,32 +1,87 @@
use std::ffi::c_char; use std::ffi::c_char;
use crate::plugins::PLUGIN_REGISTRAR; use crate::{
flutter::{FlutterHandler, SESSIONS},
plugins::PLUGIN_REGISTRAR,
ui_session_interface::Session,
};
// API provided by RustDesk. // API provided by RustDesk.
pub type LoadPluginFunc = fn(*const c_char) -> i32; pub type LoadPluginFunc = fn(*const c_char) -> i32;
pub type UnloadPluginFunc = fn(*const c_char) -> i32; pub type UnloadPluginFunc = fn(*const c_char) -> i32;
pub type AddSessionFunc = fn(session: Session<FlutterHandler>) -> bool;
pub type RemoveSessionFunc = fn(session_id: &String) -> bool;
pub type AddSessionHookFunc = fn(session_id: String, key: String, hook: SessionHook) -> bool;
pub type RemoveSessionHookFunc = fn(session_id: String, key: &String) -> bool;
/// Hooks for session.
#[repr(usize)]
#[derive(Clone)]
pub enum SessionHook {
OnSessionRgba(fn(&Session<FlutterHandler>, Vec<i8>) -> Vec<i8>) = 1,
}
#[repr(C)] #[repr(C)]
pub struct RustDeskApiTable { pub struct RustDeskApiTable {
pub(crate) register_plugin: LoadPluginFunc, pub(crate) load_plugin: LoadPluginFunc,
pub(crate) unload_plugin: UnloadPluginFunc, pub(crate) unload_plugin: UnloadPluginFunc,
pub add_session: AddSessionFunc,
pub remove_session: RemoveSessionFunc,
pub add_session_hook: AddSessionHookFunc,
pub remove_session_hook: RemoveSessionHookFunc,
} }
#[no_mangle]
fn load_plugin(path: *const c_char) -> i32 { fn load_plugin(path: *const c_char) -> i32 {
PLUGIN_REGISTRAR.load_plugin(path) PLUGIN_REGISTRAR.load_plugin(path)
} }
#[no_mangle]
fn unload_plugin(path: *const c_char) -> i32 { fn unload_plugin(path: *const c_char) -> i32 {
PLUGIN_REGISTRAR.unload_plugin(path) PLUGIN_REGISTRAR.unload_plugin(path)
} }
fn add_session(session: Session<FlutterHandler>) -> bool {
let mut sessions = SESSIONS.write().unwrap();
if sessions.contains_key(&session.id) {
return false;
}
let _ = sessions.insert(session.id.to_owned(), session);
true
}
fn remove_session(session_id: &String) -> bool {
let mut sessions = SESSIONS.write().unwrap();
if !sessions.contains_key(session_id) {
return false;
}
let _ = sessions.remove(session_id);
true
}
fn add_session_hook(session_id: String, key: String, hook: SessionHook) -> bool {
let sessions = SESSIONS.read().unwrap();
if let Some(session) = sessions.get(&session_id) {
return session.add_session_hook(key, hook);
}
false
}
fn remove_session_hook(session_id: String, key: &String) -> bool {
let sessions = SESSIONS.read().unwrap();
if let Some(session) = sessions.get(&session_id) {
return session.remove_session_hook(key);
}
false
}
impl Default for RustDeskApiTable { impl Default for RustDeskApiTable {
fn default() -> Self { fn default() -> Self {
Self { Self {
register_plugin: load_plugin, load_plugin,
unload_plugin: unload_plugin, unload_plugin,
add_session,
remove_session,
add_session_hook,
remove_session_hook,
} }
} }
} }

View File

@ -1,4 +1,5 @@
use crate::{ use crate::{
api::SessionHook,
client::*, client::*,
flutter_ffi::EventToUI, flutter_ffi::EventToUI,
ui_session_interface::{io_loop, InvokeUiSession, Session}, ui_session_interface::{io_loop, InvokeUiSession, Session},
@ -40,9 +41,9 @@ pub(super) const APP_TYPE_DESKTOP_FILE_TRANSFER: &str = "file transfer";
pub(super) const APP_TYPE_DESKTOP_PORT_FORWARD: &str = "port forward"; pub(super) const APP_TYPE_DESKTOP_PORT_FORWARD: &str = "port forward";
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub static ref CUR_SESSION_ID: RwLock<String> = Default::default(); pub(crate) static ref CUR_SESSION_ID: RwLock<String> = Default::default();
pub static ref SESSIONS: RwLock<HashMap<String, Session<FlutterHandler>>> = Default::default(); pub(crate) static ref SESSIONS: RwLock<HashMap<String, Session<FlutterHandler>>> = Default::default();
pub static ref GLOBAL_EVENT_STREAM: RwLock<HashMap<String, StreamSink<String>>> = Default::default(); // rust to dart event channel pub(crate) static ref GLOBAL_EVENT_STREAM: RwLock<HashMap<String, StreamSink<String>>> = Default::default(); // rust to dart event channel
} }
#[cfg(all(target_os = "windows", feature = "flutter_texture_render"))] #[cfg(all(target_os = "windows", feature = "flutter_texture_render"))]
@ -146,6 +147,7 @@ pub struct FlutterHandler {
notify_rendered: Arc<RwLock<bool>>, notify_rendered: Arc<RwLock<bool>>,
renderer: Arc<RwLock<VideoRenderer>>, renderer: Arc<RwLock<VideoRenderer>>,
peer_info: Arc<RwLock<PeerInfo>>, peer_info: Arc<RwLock<PeerInfo>>,
hooks: Arc<RwLock<HashMap<String, SessionHook>>>,
} }
#[cfg(not(feature = "flutter_texture_render"))] #[cfg(not(feature = "flutter_texture_render"))]
@ -157,6 +159,7 @@ pub struct FlutterHandler {
pub rgba: Arc<RwLock<Vec<u8>>>, pub rgba: Arc<RwLock<Vec<u8>>>,
pub rgba_valid: Arc<AtomicBool>, pub rgba_valid: Arc<AtomicBool>,
peer_info: Arc<RwLock<PeerInfo>>, peer_info: Arc<RwLock<PeerInfo>>,
hooks: Arc<RwLock<HashMap<String, SessionHook>>>,
} }
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
@ -255,7 +258,7 @@ impl FlutterHandler {
} }
} }
pub fn close_event_stream(&mut self) { pub(crate) fn close_event_stream(&mut self) {
let mut stream_lock = self.event_stream.write().unwrap(); let mut stream_lock = self.event_stream.write().unwrap();
if let Some(stream) = &*stream_lock { if let Some(stream) = &*stream_lock {
stream.add(EventToUI::Event("close".to_owned())); stream.add(EventToUI::Event("close".to_owned()));
@ -277,6 +280,26 @@ impl FlutterHandler {
serde_json::ser::to_string(&msg_vec).unwrap_or("".to_owned()) serde_json::ser::to_string(&msg_vec).unwrap_or("".to_owned())
} }
pub(crate) fn add_session_hook(&self, key: String, hook: crate::api::SessionHook) -> bool {
let mut hooks = self.hooks.write().unwrap();
if hooks.contains_key(&key) {
// Already has the hook with this key.
return false;
}
let _ = hooks.insert(key, hook);
true
}
pub(crate) fn remove_session_hook(&self, key: &String) -> bool {
let mut hooks = self.hooks.write().unwrap();
if !hooks.contains_key(key) {
// The hook with this key does not found.
return false;
}
let _ = hooks.remove(key);
true
}
#[inline] #[inline]
#[cfg(feature = "flutter_texture_render")] #[cfg(feature = "flutter_texture_render")]
pub fn register_texture(&mut self, ptr: usize) { pub fn register_texture(&mut self, ptr: usize) {