refactor all [setByName] [getByName] to async bridge function

This commit is contained in:
csf
2022-08-08 22:27:27 +08:00
parent c5d0628291
commit e420178750
18 changed files with 332 additions and 470 deletions

View File

@@ -1280,7 +1280,7 @@ impl LoginConfigHandler {
/// Create a [`Message`] for login.
fn create_login_msg(&self, password: Vec<u8>) -> Message {
#[cfg(any(target_os = "android", target_os = "ios"))]
let my_id = Config::get_id_or(crate::common::FLUTTER_INFO1.lock().unwrap().clone());
let my_id = Config::get_id_or(crate::common::DEVICE_ID.lock().unwrap().clone());
#[cfg(not(any(target_os = "android", target_os = "ios")))]
let my_id = Config::get_id();
let mut lr = LoginRequest {

View File

@@ -28,8 +28,8 @@ lazy_static::lazy_static! {
}
lazy_static::lazy_static! {
pub static ref FLUTTER_INFO1: Arc<Mutex<String>> = Default::default();
pub static ref FLUTTER_INFO2: Arc<Mutex<String>> = Default::default();
pub static ref DEVICE_ID: Arc<Mutex<String>> = Default::default();
pub static ref DEVICE_NAME: Arc<Mutex<String>> = Default::default();
}
#[inline]
@@ -441,7 +441,7 @@ pub fn username() -> String {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
return whoami::username().trim_end_matches('\0').to_owned();
#[cfg(any(target_os = "android", target_os = "ios"))]
return FLUTTER_INFO2.lock().unwrap().clone();
return DEVICE_NAME.lock().unwrap().clone();
}
#[inline]

View File

@@ -26,7 +26,8 @@ use crate::ui_interface::{
get_connect_status, get_fav, get_id, get_lan_peers, get_license, get_local_option, get_option,
get_options, get_peer, get_peer_option, get_socks, get_sound_inputs, get_uuid, get_version,
has_rendezvous_service, post_request, set_local_option, set_option, set_options,
set_peer_option, set_socks, store_fav, test_if_valid_server, using_public_server,
set_peer_option, set_permanent_password, set_socks, store_fav, test_if_valid_server,
update_temporary_password, using_public_server,
};
fn initialize(app_dir: &str) {
@@ -235,38 +236,6 @@ pub fn session_send_chat(id: String, text: String) {
}
}
// if let Some(_type) = m.get("type") {
// mask = match _type.as_str() {
// "down" => 1,
// "up" => 2,
// "wheel" => 3,
// _ => 0,
// };
// }
// if let Some(buttons) = m.get("buttons") {
// mask |= match buttons.as_str() {
// "left" => 1,
// "right" => 2,
// "wheel" => 4,
// _ => 0,
// } << 3;
// }
// TODO
pub fn session_send_mouse(
id: String,
mask: i32,
x: i32,
y: i32,
alt: bool,
ctrl: bool,
shift: bool,
command: bool,
) {
if let Some(session) = SESSIONS.read().unwrap().get(&id) {
session.send_mouse(mask, x, y, alt, ctrl, shift, command);
}
}
pub fn session_peer_option(id: String, name: String, value: String) {
if let Some(session) = SESSIONS.read().unwrap().get(&id) {
session.set_option(name, value);
@@ -426,11 +395,7 @@ pub fn main_set_option(key: String, value: String) {
set_option(key, value);
#[cfg(target_os = "android")]
crate::rendezvous_mediator::RendezvousMediator::restart();
#[cfg(any(
target_os = "android",
target_os = "ios",
feature = "cli"
))]
#[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
crate::common::test_rendezvous_server();
} else {
set_option(key, value);
@@ -640,6 +605,143 @@ pub fn main_get_last_remote_id() -> String {
LocalConfig::get_remote_id()
}
pub fn main_get_software_update_url() -> String {
crate::common::SOFTWARE_UPDATE_URL.lock().unwrap().clone()
}
pub fn main_get_home_dir() -> String {
fs::get_home_as_string()
}
pub fn main_get_langs() -> String {
crate::lang::LANGS.to_string()
}
pub fn main_get_temporary_password() -> String {
ui_interface::temporary_password()
}
pub fn main_get_permanent_password() -> String {
ui_interface::permanent_password()
}
pub fn main_get_online_statue() -> i64 {
ONLINE.lock().unwrap().values().max().unwrap_or(&0).clone()
}
pub fn main_get_clients_state() -> String {
get_clients_state()
}
pub fn main_check_clients_length(length: usize) -> Option<String> {
if length != get_clients_length() {
Some(get_clients_state())
} else {
None
}
}
pub fn main_init(app_dir: String) {
initialize(&app_dir);
}
pub fn main_device_id(id: String) {
*crate::common::DEVICE_ID.lock().unwrap() = id;
}
pub fn main_device_name(name: String) {
*crate::common::DEVICE_NAME.lock().unwrap() = name;
}
pub fn main_remove_peer(id: String) {
PeerConfig::remove(&id);
}
// TODO
pub fn session_send_mouse(id: String, msg: String) {
if let Ok(m) = serde_json::from_str::<HashMap<String, String>>(&msg) {
let alt = m.get("alt").is_some();
let ctrl = m.get("ctrl").is_some();
let shift = m.get("shift").is_some();
let command = m.get("command").is_some();
let x = m
.get("x")
.map(|x| x.parse::<i32>().unwrap_or(0))
.unwrap_or(0);
let y = m
.get("y")
.map(|x| x.parse::<i32>().unwrap_or(0))
.unwrap_or(0);
let mut mask = 0;
if let Some(_type) = m.get("type") {
mask = match _type.as_str() {
"down" => 1,
"up" => 2,
"wheel" => 3,
_ => 0,
};
}
if let Some(buttons) = m.get("buttons") {
mask |= match buttons.as_str() {
"left" => 1,
"right" => 2,
"wheel" => 4,
_ => 0,
} << 3;
}
if let Some(session) = SESSIONS.read().unwrap().get(&id) {
session.send_mouse(mask, x, y, alt, ctrl, shift, command);
}
}
}
pub fn session_restart_remote_device(id: String) {
// TODO
// Session::restart_remote_device();
}
pub fn main_set_home_dir(home: String) {
*config::APP_HOME_DIR.write().unwrap() = home;
}
pub fn main_stop_service() {
#[cfg(target_os = "android")]
{
Config::set_option("stop-service".into(), "Y".into());
crate::rendezvous_mediator::RendezvousMediator::restart();
}
}
pub fn main_start_service() {
#[cfg(target_os = "android")]
{
Config::set_option("stop-service".into(), "".into());
crate::rendezvous_mediator::RendezvousMediator::restart();
}
#[cfg(not(target_os = "android"))]
std::thread::spawn(move || start_server(true));
}
pub fn main_update_temporary_password() {
update_temporary_password();
}
pub fn main_set_permanent_password(password: String) {
set_permanent_password(password);
}
pub fn server_send_chat(conn_id: i32, msg: String) {
connection_manager::send_chat(conn_id, msg);
}
pub fn server_login_res(conn_id: i32, res: bool) {
connection_manager::on_login_res(conn_id, res);
}
pub fn server_close_connection(conn_id: i32) {
connection_manager::close_conn(conn_id);
}
#[no_mangle]
unsafe extern "C" fn translate(name: *const c_char, locale: *const c_char) -> *const c_char {
let name = CStr::from_ptr(name);
@@ -652,241 +754,6 @@ unsafe extern "C" fn translate(name: *const c_char, locale: *const c_char) -> *c
CString::from_vec_unchecked(res.into_bytes()).into_raw()
}
/// FFI for **get** commands which are idempotent.
/// Return result in c string.
///
/// # Arguments
///
/// * `name` - name of the command
/// * `arg` - argument of the command
#[no_mangle]
unsafe extern "C" fn get_by_name(name: *const c_char, arg: *const c_char) -> *const c_char {
let mut res = "".to_owned();
let arg: &CStr = CStr::from_ptr(arg);
let name: &CStr = CStr::from_ptr(name);
if let Ok(name) = name.to_str() {
match name {
// "peers" => {
// if !config::APP_DIR.read().unwrap().is_empty() {
// let peers: Vec<(String, config::PeerInfoSerde)> = PeerConfig::peers()
// .drain(..)
// .map(|(id, _, p)| (id, p.info))
// .collect();
// res = serde_json::ser::to_string(&peers).unwrap_or("".to_owned());
// }
// }
// "remote_id" => {
// if !config::APP_DIR.read().unwrap().is_empty() {
// res = LocalConfig::get_remote_id();
// }
// }
// "test_if_valid_server" => {
// if let Ok(arg) = arg.to_str() {
// res = hbb_common::socket_client::test_if_valid_server(arg);
// }
// }
// "option" => {
// if let Ok(arg) = arg.to_str() {
// res = ui_interface::get_option(arg.to_owned());
// }
// }
"software_update_url" => {
res = crate::common::SOFTWARE_UPDATE_URL.lock().unwrap().clone()
}
// File Action
"get_home_dir" => {
res = fs::get_home_as_string();
}
// Server Side
"langs" => {
res = crate::lang::LANGS.to_string();
}
"temporary_password" => {
res = ui_interface::temporary_password();
}
"permanent_password" => {
res = ui_interface::permanent_password();
}
"connect_statue" => {
res = ONLINE
.lock()
.unwrap()
.values()
.max()
.unwrap_or(&0)
.clone()
.to_string();
}
#[cfg(not(any(target_os = "ios")))]
"clients_state" => {
res = get_clients_state();
}
#[cfg(not(any(target_os = "ios")))]
"check_clients_length" => {
if let Ok(value) = arg.to_str() {
if value.parse::<usize>().unwrap_or(usize::MAX) != get_clients_length() {
res = get_clients_state()
}
}
}
_ => {
log::error!("Unknown name of get_by_name: {}", name);
}
}
}
CString::from_vec_unchecked(res.into_bytes()).into_raw()
}
/// FFI for **set** commands which are not idempotent.
///
/// # Arguments
///
/// * `name` - name of the command
/// * `arg` - argument of the command
#[no_mangle]
unsafe extern "C" fn set_by_name(name: *const c_char, value: *const c_char) {
let value: &CStr = CStr::from_ptr(value);
if let Ok(value) = value.to_str() {
let name: &CStr = CStr::from_ptr(name);
if let Ok(name) = name.to_str() {
match name {
"init" => {
initialize(value);
}
"info1" => {
*crate::common::FLUTTER_INFO1.lock().unwrap() = value.to_owned();
}
"info2" => {
*crate::common::FLUTTER_INFO2.lock().unwrap() = value.to_owned();
}
"remove" => {
PeerConfig::remove(value);
}
// TODO
"send_mouse" => {
if let Ok(m) = serde_json::from_str::<HashMap<String, String>>(value) {
let id = m.get("id");
if id.is_none() {
return;
}
let id = id.unwrap();
let alt = m.get("alt").is_some();
let ctrl = m.get("ctrl").is_some();
let shift = m.get("shift").is_some();
let command = m.get("command").is_some();
let x = m
.get("x")
.map(|x| x.parse::<i32>().unwrap_or(0))
.unwrap_or(0);
let y = m
.get("y")
.map(|x| x.parse::<i32>().unwrap_or(0))
.unwrap_or(0);
let mut mask = 0;
if let Some(_type) = m.get("type") {
mask = match _type.as_str() {
"down" => 1,
"up" => 2,
"wheel" => 3,
_ => 0,
};
}
if let Some(buttons) = m.get("buttons") {
mask |= match buttons.as_str() {
"left" => 1,
"right" => 2,
"wheel" => 4,
_ => 0,
} << 3;
}
if let Some(session) = SESSIONS.read().unwrap().get(id) {
session.send_mouse(mask, x, y, alt, ctrl, shift, command);
}
}
}
// "option" => {
// if let Ok(m) = serde_json::from_str::<HashMap<String, String>>(value) {
// if let Some(name) = m.get("name") {
// if let Some(value) = m.get("value") {
// ui_interface::set_option(name.to_owned(), value.to_owned());
// if name == "custom-rendezvous-server" {
// #[cfg(target_os = "android")]
// crate::rendezvous_mediator::RendezvousMediator::restart();
// #[cfg(any(
// target_os = "android",
// target_os = "ios",
// feature = "cli"
// ))]
// crate::common::test_rendezvous_server();
// }
// }
// }
// }
// }
"restart_remote_device" => {
// TODO
// Session::restart_remote_device();
}
#[cfg(target_os = "android")]
"chat_server_mode" => {
if let Ok(m) = serde_json::from_str::<HashMap<String, Value>>(value) {
if let (Some(Value::Number(id)), Some(Value::String(text))) =
(m.get("id"), m.get("text"))
{
let id = id.as_i64().unwrap_or(0);
connection_manager::send_chat(id as i32, text.to_owned());
}
}
}
"home_dir" => {
*config::APP_HOME_DIR.write().unwrap() = value.to_owned();
}
#[cfg(target_os = "android")]
"login_res" => {
if let Ok(m) = serde_json::from_str::<HashMap<String, Value>>(value) {
if let (Some(Value::Number(id)), Some(Value::Bool(res))) =
(m.get("id"), m.get("res"))
{
let id = id.as_i64().unwrap_or(0);
connection_manager::on_login_res(id as i32, *res);
}
}
}
#[cfg(target_os = "android")]
"stop_service" => {
Config::set_option("stop-service".into(), "Y".into());
crate::rendezvous_mediator::RendezvousMediator::restart();
}
"start_service" => {
#[cfg(target_os = "android")]
{
Config::set_option("stop-service".into(), "".into());
crate::rendezvous_mediator::RendezvousMediator::restart();
}
#[cfg(not(target_os = "android"))]
std::thread::spawn(move || start_server(true));
}
#[cfg(target_os = "android")]
"close_conn" => {
if let Ok(id) = value.parse::<i32>() {
connection_manager::close_conn(id);
};
}
"temporary_password" => {
ui_interface::update_temporary_password();
}
"permanent_password" => {
ui_interface::set_permanent_password(value.to_owned());
}
_ => {
log::error!("Unknown name of set_by_name: {}", name);
}
}
}
}
}
fn handle_query_onlines(onlines: Vec<String>, offlines: Vec<String>) {
if let Some(s) = flutter::GLOBAL_EVENT_STREAM
.read()

View File

@@ -5,7 +5,7 @@ use crate::clipboard_file::*;
use crate::common::update_clipboard;
use crate::video_service;
#[cfg(any(target_os = "android", target_os = "ios"))]
use crate::{common::FLUTTER_INFO2, flutter::connection_manager::start_channel};
use crate::{common::DEVICE_NAME, flutter::connection_manager::start_channel};
use crate::{ipc, VERSION};
use hbb_common::{
config::Config,
@@ -643,7 +643,7 @@ impl Connection {
}
#[cfg(target_os = "android")]
{
pi.hostname = FLUTTER_INFO2.lock().unwrap().clone();
pi.hostname = DEVICE_NAME.lock().unwrap().clone();
pi.platform = "Android".into();
}
#[cfg(feature = "hwcodec")]