2023-04-21 21:40:34 +08:00
|
|
|
use super::*;
|
2023-04-18 23:02:37 +08:00
|
|
|
use crate::flutter::{self, APP_TYPE_CM, APP_TYPE_MAIN, SESSIONS};
|
2023-04-19 17:06:59 +08:00
|
|
|
use hbb_common::{lazy_static, log, message_proto::PluginRequest};
|
2023-04-21 21:40:34 +08:00
|
|
|
use serde_derive::Deserialize;
|
2023-04-18 23:02:37 +08:00
|
|
|
use serde_json;
|
2023-04-19 17:26:36 +08:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
ffi::{c_char, c_void},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2023-04-18 23:02:37 +08:00
|
|
|
|
|
|
|
const MSG_TO_PEER_TARGET: &str = "peer";
|
|
|
|
const MSG_TO_UI_TARGET: &str = "ui";
|
2023-04-21 21:40:34 +08:00
|
|
|
const MSG_TO_CONFIG_TARGET: &str = "config";
|
2023-04-18 23:02:37 +08:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const MSG_TO_UI_FLUTTER_CHANNEL_MAIN: u16 = 0x01 << 0;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
|
|
|
const MSG_TO_UI_FLUTTER_CHANNEL_CM: u16 = 0x01 << 1;
|
|
|
|
#[cfg(any(target_os = "android", target_os = "ios"))]
|
|
|
|
const MSG_TO_UI_FLUTTER_CHANNEL_CM: u16 = 0x01;
|
|
|
|
const MSG_TO_UI_FLUTTER_CHANNEL_REMOTE: u16 = 0x01 << 2;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const MSG_TO_UI_FLUTTER_CHANNEL_TRANSFER: u16 = 0x01 << 3;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const MSG_TO_UI_FLUTTER_CHANNEL_FORWARD: u16 = 0x01 << 4;
|
|
|
|
|
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref MSG_TO_UI_FLUTTER_CHANNELS: Arc<HashMap<u16, String>> = {
|
|
|
|
let channels = HashMap::from([
|
|
|
|
(MSG_TO_UI_FLUTTER_CHANNEL_MAIN, APP_TYPE_MAIN.to_string()),
|
|
|
|
(MSG_TO_UI_FLUTTER_CHANNEL_CM, APP_TYPE_CM.to_string()),
|
|
|
|
]);
|
|
|
|
Arc::new(channels)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-21 21:40:34 +08:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ConfigToUi {
|
|
|
|
channel: u16,
|
|
|
|
location: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct MsgToConfig {
|
|
|
|
id: String,
|
|
|
|
r#type: String,
|
|
|
|
key: String,
|
|
|
|
value: String,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
ui: Option<ConfigToUi>, // If not None, send msg to ui.
|
|
|
|
}
|
|
|
|
|
2023-04-18 23:02:37 +08:00
|
|
|
/// Callback to send message to peer or ui.
|
|
|
|
/// peer, target, id are utf8 strings(null terminated).
|
|
|
|
///
|
|
|
|
/// peer: The peer id.
|
|
|
|
/// target: "peer" or "ui".
|
|
|
|
/// id: The id of this plugin.
|
|
|
|
/// content: The content.
|
|
|
|
/// len: The length of the content.
|
|
|
|
pub fn callback_msg(
|
|
|
|
peer: *const c_char,
|
|
|
|
target: *const c_char,
|
|
|
|
id: *const c_char,
|
2023-04-19 17:26:36 +08:00
|
|
|
content: *const c_void,
|
2023-04-18 23:02:37 +08:00
|
|
|
len: usize,
|
|
|
|
) {
|
|
|
|
macro_rules! callback_msg_field {
|
|
|
|
($field: ident) => {
|
|
|
|
let $field = match cstr_to_string($field) {
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Failed to convert {} to string, {}", stringify!($field), e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok(v) => v,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
callback_msg_field!(peer);
|
|
|
|
callback_msg_field!(target);
|
|
|
|
callback_msg_field!(id);
|
|
|
|
|
|
|
|
match &target as _ {
|
|
|
|
MSG_TO_PEER_TARGET => {
|
|
|
|
if let Some(session) = SESSIONS.write().unwrap().get_mut(&peer) {
|
|
|
|
let content_slice =
|
|
|
|
unsafe { std::slice::from_raw_parts(content as *const u8, len) };
|
|
|
|
let content_vec = Vec::from(content_slice);
|
2023-04-19 17:26:36 +08:00
|
|
|
let request = PluginRequest {
|
2023-04-18 23:02:37 +08:00
|
|
|
id,
|
|
|
|
content: bytes::Bytes::from(content_vec),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2023-04-19 17:26:36 +08:00
|
|
|
session.send_plugin_request(request);
|
2023-04-18 23:02:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MSG_TO_UI_TARGET => {
|
|
|
|
let content_slice = unsafe { std::slice::from_raw_parts(content as *const u8, len) };
|
|
|
|
let channel = u16::from_be_bytes([content_slice[0], content_slice[1]]);
|
|
|
|
let content = std::string::String::from_utf8(content_slice[2..].to_vec())
|
|
|
|
.unwrap_or("".to_string());
|
2023-04-21 21:40:34 +08:00
|
|
|
push_event_to_ui(channel, &peer, &content);
|
|
|
|
}
|
|
|
|
MSG_TO_CONFIG_TARGET => {
|
|
|
|
if let Ok(s) =
|
|
|
|
std::str::from_utf8(unsafe { std::slice::from_raw_parts(content as _, len) })
|
2023-04-18 23:02:37 +08:00
|
|
|
{
|
2023-04-21 23:25:00 +08:00
|
|
|
// No need to merge the msgs. Handling the msg one by one is ok.
|
2023-04-21 21:40:34 +08:00
|
|
|
if let Ok(msg) = serde_json::from_str::<MsgToConfig>(s) {
|
|
|
|
match &msg.r#type as _ {
|
|
|
|
config::CONFIG_TYPE_LOCAL => {
|
|
|
|
match config::LocalConfig::set(&msg.id, &msg.key, &msg.value) {
|
|
|
|
Ok(_) => {
|
|
|
|
if let Some(ui) = &msg.ui {
|
|
|
|
// No need to set the peer id for location config.
|
|
|
|
push_option_to_ui(ui.channel, "", &msg, ui);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Failed to set local config, {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config::CONFIG_TYPE_PEER => {
|
|
|
|
match config::PeerConfig::set(&msg.id, &peer, &msg.key, &msg.value) {
|
|
|
|
Ok(_) => {
|
|
|
|
if let Some(ui) = &msg.ui {
|
|
|
|
push_option_to_ui(ui.channel, &peer, &msg, ui);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
log::error!("Failed to set peer config, {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2023-04-18 23:02:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
log::error!("Unknown target {}", target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-21 21:40:34 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn is_peer_channel(channel: u16) -> bool {
|
|
|
|
channel & MSG_TO_UI_FLUTTER_CHANNEL_REMOTE != 0
|
|
|
|
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_TRANSFER != 0
|
|
|
|
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_FORWARD != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_event_to_ui(channel: u16, peer: &str, content: &str) {
|
|
|
|
let mut m = HashMap::new();
|
|
|
|
m.insert("name", MSG_TO_UI_TYPE_PLUGIN_EVENT);
|
|
|
|
m.insert("peer", &peer);
|
|
|
|
m.insert("content", &content);
|
|
|
|
let event = serde_json::to_string(&m).unwrap_or("".to_string());
|
|
|
|
for (k, v) in MSG_TO_UI_FLUTTER_CHANNELS.iter() {
|
|
|
|
if channel & k != 0 {
|
|
|
|
let _res = flutter::push_global_event(v as _, event.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_peer_channel(channel) {
|
|
|
|
let _res = flutter::push_session_event(
|
|
|
|
&peer,
|
|
|
|
MSG_TO_UI_TYPE_PLUGIN_EVENT,
|
|
|
|
vec![("peer", &peer), ("content", &content)],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_option_to_ui(channel: u16, peer: &str, msg: &MsgToConfig, ui: &ConfigToUi) {
|
|
|
|
let v = [
|
|
|
|
("name", MSG_TO_UI_TYPE_PLUGIN_OPTION),
|
|
|
|
("id", &msg.id),
|
|
|
|
("location", &ui.location),
|
|
|
|
("key", &msg.key),
|
|
|
|
("value", &msg.value),
|
|
|
|
];
|
|
|
|
let event = serde_json::to_string(&HashMap::from(v)).unwrap_or("".to_string());
|
|
|
|
for (k, v) in MSG_TO_UI_FLUTTER_CHANNELS.iter() {
|
|
|
|
if channel & k != 0 {
|
|
|
|
let _res = flutter::push_global_event(v as _, event.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut v = v.to_vec();
|
|
|
|
v.push(("peer", &peer));
|
|
|
|
if is_peer_channel(channel) {
|
|
|
|
let _res = flutter::push_session_event(&peer, MSG_TO_UI_TYPE_PLUGIN_OPTION, v.to_vec());
|
|
|
|
}
|
|
|
|
}
|