mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
feat: add session related function
This commit is contained in:
@@ -6,8 +6,8 @@ use std::{ffi::{c_char, c_void}, os::raw::c_uint};
|
||||
/// The data is owned by librustdesk.
|
||||
#[repr(C)]
|
||||
pub struct NativeReturnValue{
|
||||
return_type: c_uint,
|
||||
data: *const c_void
|
||||
pub return_type: c_uint,
|
||||
pub data: *const c_void
|
||||
}
|
||||
|
||||
pub(super) extern "C" fn cb_native_data(method: *const c_char, json: *const c_char, raw: *const c_void, raw_len: usize) -> NativeReturnValue {
|
||||
|
||||
@@ -61,7 +61,7 @@ pub(self) trait PluginNativeHandler {
|
||||
) -> Option<NR>;
|
||||
}
|
||||
|
||||
pub(crate) trait Callable {
|
||||
pub trait Callable {
|
||||
fn call(
|
||||
&self,
|
||||
method: &String,
|
||||
|
||||
@@ -1,10 +1,22 @@
|
||||
use crate::{call_if_method, define_method_prefix, return_if_not_method};
|
||||
use std::sync::{atomic::AtomicU64, Arc, RwLock};
|
||||
|
||||
use crate::{
|
||||
call_if_method, define_method_prefix, flutter::FlutterHandler, return_if_not_method,
|
||||
ui_session_interface::Session,
|
||||
};
|
||||
|
||||
use super::PluginNativeHandler;
|
||||
|
||||
#[derive(Default)]
|
||||
/// Session related handler for librustdesk core.
|
||||
pub struct PluginNativeSessionHandler;
|
||||
pub struct PluginNativeSessionHandler {
|
||||
sessions: Arc<RwLock<Vec<Session<FlutterHandler>>>>,
|
||||
id: AtomicU64,
|
||||
}
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref SESSION_HANDLER: Arc<PluginNativeSessionHandler> = Arc::new(PluginNativeSessionHandler::default());
|
||||
}
|
||||
|
||||
impl PluginNativeHandler for PluginNativeSessionHandler {
|
||||
define_method_prefix!("session_");
|
||||
@@ -14,6 +26,48 @@ impl PluginNativeHandler for PluginNativeSessionHandler {
|
||||
method: &str,
|
||||
data: &serde_json::Map<String, serde_json::Value>,
|
||||
) -> Option<super::NR> {
|
||||
match method {
|
||||
"create_session" => {
|
||||
return Some(super::NR {
|
||||
return_type: 1,
|
||||
data: SESSION_HANDLER.create_session() as _,
|
||||
});
|
||||
}
|
||||
"add_session_hook" => {
|
||||
if let Some(id) = data.get("id") {
|
||||
if let Some(id) = id.as_u64() {
|
||||
SESSION_HANDLER.add_session_hook(id);
|
||||
return Some(super::NR {
|
||||
return_type: 0,
|
||||
data: std::ptr::null(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
"remove_session_hook" => {
|
||||
if let Some(id) = data.get("id") {
|
||||
if let Some(id) = id.as_u64() {
|
||||
SESSION_HANDLER.remove_session_hook(id);
|
||||
return Some(super::NR {
|
||||
return_type: 0,
|
||||
data: std::ptr::null(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
"remove_session" => {
|
||||
if let Some(id) = data.get("id") {
|
||||
if let Some(id) = id.as_u64() {
|
||||
SESSION_HANDLER.remove_session(id);
|
||||
return Some(super::NR {
|
||||
return_type: 0,
|
||||
data: std::ptr::null(),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
@@ -29,7 +83,59 @@ impl PluginNativeHandler for PluginNativeSessionHandler {
|
||||
}
|
||||
|
||||
impl PluginNativeSessionHandler {
|
||||
fn create_session() {}
|
||||
fn create_session(&self) -> u64 {
|
||||
let mut sessions = self.sessions.write().unwrap();
|
||||
let unique_id = self.id.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
let mut session: Session<FlutterHandler> = Session::default();
|
||||
session.id = self.get_hook_key(unique_id);
|
||||
sessions.push(session);
|
||||
return unique_id;
|
||||
}
|
||||
|
||||
fn add_session_hook() {}
|
||||
fn add_session_hook(&self, session_id: u64) {
|
||||
let sessions = self.sessions.read().unwrap();
|
||||
let session_id = self.get_hook_key(session_id);
|
||||
for session in sessions.iter() {
|
||||
if session.id == session_id {
|
||||
session.ui_handler.add_session_hook(
|
||||
session_id.to_owned(),
|
||||
crate::flutter::SessionHook::OnSessionRgba(session_rgba_cb),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_session_hook(&self, session_id: u64) {
|
||||
let sessions = self.sessions.read().unwrap();
|
||||
let session_id = self.get_hook_key(session_id);
|
||||
for session in sessions.iter() {
|
||||
if session.id == session_id {
|
||||
session.ui_handler.remove_session_hook(&session_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_session(&self, session_id: u64) {
|
||||
let mut sessions = self.sessions.write().unwrap();
|
||||
let session_id = self.get_hook_key(session_id);
|
||||
for i in 0..sessions.len() {
|
||||
if sessions[i].id == session_id {
|
||||
sessions.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_hook_key(&self, id: u64) -> String {
|
||||
format!("{}_{}", self.method_prefix(), id)
|
||||
}
|
||||
|
||||
// The callback function for rgba data
|
||||
fn session_rgba_cb(&self, key: String, rgb: &mut scrap::ImageRgb) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
fn session_rgba_cb(key: String, rgb: &mut scrap::ImageRgb) {
|
||||
SESSION_HANDLER.session_rgba_cb(key, rgb);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user