Skip to content

Commit

Permalink
Merge pull request rustdesk#4107 from fufesou/feat/plugin_framework
Browse files Browse the repository at this point in the history
plugin framework, handle peer event
  • Loading branch information
rustdesk authored Apr 19, 2023
2 parents c8be201 + 560c6f3 commit 3c6c56c
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 41 deletions.
11 changes: 9 additions & 2 deletions libs/hbb_common/protos/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,17 @@ message SwitchSidesResponse {

message SwitchBack {}

message Plugin {
message PluginRequest {
string id = 1;
bytes content = 2;
}

message PluginResponse {
string id = 1;
string name = 2;
string msg = 3;
}

message Misc {
oneof union {
ChatMessage chat_message = 4;
Expand All @@ -626,7 +632,8 @@ message Misc {
SwitchSidesRequest switch_sides_request = 21;
SwitchBack switch_back = 22;
Resolution change_resolution = 24;
Plugin plugin = 25;
PluginRequest plugin_request = 25;
PluginResponse plugin_response = 26;
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/client/io_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ impl<T: InvokeUiSession> Remote<T> {
{
// Create a channel to receive error or closed message
let (tx, rx) = std::sync::mpsc::channel();
let (tx_audio_data, mut rx_audio_data) = hbb_common::tokio::sync::mpsc::unbounded_channel();
let (tx_audio_data, mut rx_audio_data) =
hbb_common::tokio::sync::mpsc::unbounded_channel();
// Create a stand-alone inner, add subscribe to audio service
let conn_id = CLIENT_SERVER.write().unwrap().get_new_id();
let client_conn_inner = ConnInner::new(conn_id.clone(), Some(tx_audio_data), None);
Expand Down Expand Up @@ -1297,6 +1298,22 @@ impl<T: InvokeUiSession> Remote<T> {
#[cfg(feature = "flutter")]
self.handler.switch_back(&self.handler.id);
}
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
Some(misc::Union::PluginRequest(p)) => {
allow_err!(crate::plugin::handle_server_event(&p.id, &p.content));
// to-do: show message box on UI when error occurs?
}
#[cfg(all(feature = "flutter", feature = "plugin_framework"))]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
Some(misc::Union::PluginResponse(p)) => {
let name = if p.name.is_empty() {
"plugin".to_string()
} else {
p.name
};
self.handler.msgbox("custom-nocancel", &name, &p.msg, "");
}
_ => {}
},
Some(message::Union::TestDelay(t)) => {
Expand Down
4 changes: 4 additions & 0 deletions src/flutter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ pub fn session_next_rgba(id: *const char) {
}
}

#[inline]
#[no_mangle]
#[cfg(feature = "flutter_texture_render")]
pub fn session_register_texture(id: *const char, ptr: usize) {
Expand All @@ -1012,14 +1013,17 @@ pub fn session_register_texture(id: *const char, ptr: usize) {
}
}

#[inline]
#[no_mangle]
#[cfg(not(feature = "flutter_texture_render"))]
pub fn session_register_texture(_id: *const char, _ptr: usize) {}

#[inline]
pub fn push_session_event(peer: &str, name: &str, event: Vec<(&str, &str)>) -> Option<bool> {
SESSIONS.read().unwrap().get(peer)?.push_event(name, event)
}

#[inline]
pub fn push_global_event(channel: &str, event: String) -> Option<bool> {
Some(GLOBAL_EVENT_STREAM.read().unwrap().get(channel)?.add(event))
}
Expand Down
12 changes: 12 additions & 0 deletions src/flutter_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
};
use flutter_rust_bridge::{StreamSink, SyncReturn};
use hbb_common::{
allow_err,
config::{self, LocalConfig, PeerConfig, PeerInfoSerde, ONLINE},
fs, log,
message_proto::KeyboardMode,
Expand Down Expand Up @@ -48,10 +49,12 @@ fn initialize(app_dir: &str) {
}
}

#[inline]
pub fn start_global_event_stream(s: StreamSink<String>, app_type: String) -> ResultType<()> {
super::flutter::start_global_event_stream(s, app_type)
}

#[inline]
pub fn stop_global_event_stream(app_type: String) {
super::flutter::stop_global_event_stream(app_type)
}
Expand Down Expand Up @@ -1391,6 +1394,15 @@ pub fn send_url_scheme(_url: String) {
std::thread::spawn(move || crate::handle_url_scheme(_url));
}

#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn plugin_event(id: String, event: Vec<u8>) {
#[cfg(feature = "plugin_framework")]
{
allow_err!(crate::plugin::handle_ui_event(&id, &event));
}
}

#[cfg(target_os = "android")]
pub mod server_side {
use hbb_common::{config, log};
Expand Down
14 changes: 9 additions & 5 deletions src/plugin/callback_msg.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use super::cstr_to_string;
use crate::flutter::{self, APP_TYPE_CM, APP_TYPE_MAIN, SESSIONS};
use hbb_common::{lazy_static, log, message_proto::Plugin};
use hbb_common::{lazy_static, log, message_proto::PluginRequest};
use serde_json;
use std::{collections::HashMap, ffi::c_char, sync::Arc};
use std::{
collections::HashMap,
ffi::{c_char, c_void},
sync::Arc,
};

const MSG_TO_PEER_TARGET: &str = "peer";
const MSG_TO_UI_TARGET: &str = "ui";
Expand Down Expand Up @@ -42,7 +46,7 @@ pub fn callback_msg(
peer: *const c_char,
target: *const c_char,
id: *const c_char,
content: *const c_char,
content: *const c_void,
len: usize,
) {
macro_rules! callback_msg_field {
Expand All @@ -66,12 +70,12 @@ pub fn callback_msg(
let content_slice =
unsafe { std::slice::from_raw_parts(content as *const u8, len) };
let content_vec = Vec::from(content_slice);
let plugin = Plugin {
let request = PluginRequest {
id,
content: bytes::Bytes::from(content_vec),
..Default::default()
};
session.send_plugin(plugin);
session.send_plugin_request(request);
}
}
MSG_TO_UI_TARGET => {
Expand Down
28 changes: 28 additions & 0 deletions src/plugin/errno.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![allow(dead_code)]

pub const ERR_SUCCESS: i32 = 0;

// ======================================================
// errors that will be handled by RustDesk

pub const ERR_RUSTDESK_HANDLE_BASE: i32 = 10000;

// not loaded
pub const ERR_PLUGIN_LOAD: i32 = 10001;
// not initialized
pub const ERR_PLUGIN_MSG_CB: i32 = 10101;
// invalid
pub const ERR_CALL_INVALID_METHOD: i32 = 10201;
pub const ERR_CALL_NOT_SUPPORTED_METHOD: i32 = 10202;
// failed on calling
pub const ERR_CALL_INVALID_ARGS: i32 = 10301;
pub const ERR_PEER_ID_MISMATCH: i32 = 10302;

// ======================================================
// errors that should be handled by the plugin

pub const ERR_PLUGIN_HANDLE_BASE: i32 = 20000;

pub const EER_CALL_FAILED: i32 = 200021;
pub const ERR_PEER_ON_FAILED: i32 = 30012;
pub const ERR_PEER_OFF_FAILED: i32 = 30012;
20 changes: 18 additions & 2 deletions src/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
use hbb_common::ResultType;
use std::ffi::{c_char, CStr};
use std::ffi::{c_char, c_void, CStr};

mod callback_msg;
mod config;
pub mod desc;
mod errno;
mod plugins;

pub use plugins::load_plugins;
pub use plugins::{
handle_client_event, handle_server_event, handle_ui_event, load_plugin, load_plugins,
reload_plugin, unload_plugin,
};

#[inline]
fn cstr_to_string(cstr: *const c_char) -> ResultType<String> {
Ok(String::from_utf8(unsafe {
CStr::from_ptr(cstr).to_bytes().to_vec()
})?)
}

#[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)
}
Loading

0 comments on commit 3c6c56c

Please sign in to comment.