Merge pull request #544 from Heap-Hop/event_channel

use new event channel for mobile and web
This commit is contained in:
RustDesk
2022-05-17 22:09:03 +08:00
committed by GitHub
26 changed files with 1875 additions and 491 deletions

View File

@@ -1,8 +1,10 @@
#[cfg(not(any(target_os = "ios")))]
/// cbindgen:ignore
pub mod platform;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub use platform::{get_cursor, get_cursor_data, get_cursor_pos, start_os_service};
#[cfg(not(any(target_os = "ios")))]
/// cbindgen:ignore
mod server;
#[cfg(not(any(target_os = "ios")))]
pub use self::server::*;
@@ -11,29 +13,32 @@ mod client;
mod rendezvous_mediator;
#[cfg(not(any(target_os = "ios")))]
pub use self::rendezvous_mediator::*;
/// cbindgen:ignore
pub mod common;
#[cfg(not(any( target_os = "ios")))]
#[cfg(not(any(target_os = "ios")))]
pub mod ipc;
#[cfg(not(any(target_os = "android", target_os = "ios", feature = "cli")))]
pub mod ui;
mod version;
pub use version::*;
#[cfg(any(target_os = "android", target_os = "ios"))]
mod bridge_generated;
#[cfg(any(target_os = "android", target_os = "ios"))]
pub mod mobile;
#[cfg(any(target_os = "android", target_os = "ios"))]
pub mod mobile_ffi;
use common::*;
#[cfg(feature = "cli")]
pub mod cli;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
mod port_forward;
#[cfg(all(windows, feature = "hbbs"))]
mod hbbs;
mod lang;
#[cfg(windows)]
mod license;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
mod port_forward;
#[cfg(windows)]
mod tray;
mod lang;
#[cfg(windows)]
pub mod clipboard_file;

View File

@@ -1,4 +1,5 @@
use crate::client::*;
use flutter_rust_bridge::StreamSink;
use hbb_common::{
allow_err,
compress::decompress,
@@ -21,6 +22,7 @@ use std::{
lazy_static::lazy_static! {
static ref SESSION: Arc<RwLock<Option<Session>>> = Default::default();
pub static ref EVENT_STREAM: RwLock<Option<StreamSink<String>>> = Default::default(); // rust to dart channel
}
#[derive(Clone, Default)]
@@ -49,13 +51,6 @@ impl Session {
});
}
pub fn ensure_init_event_queue() {
let mut s = SESSION.write().unwrap();
if s.is_none() {
s.replace(Session::default());
}
}
pub fn get() -> Arc<RwLock<Option<Session>>> {
SESSION.clone()
}
@@ -214,10 +209,10 @@ impl Session {
let mut h: HashMap<&str, &str> = event.iter().cloned().collect();
assert!(h.get("name").is_none());
h.insert("name", name);
self.events2ui
.write()
.unwrap()
.push_back(serde_json::ser::to_string(&h).unwrap_or("".to_owned()));
if let Some(s) = EVENT_STREAM.read().unwrap().as_ref() {
s.add(serde_json::ser::to_string(&h).unwrap_or("".to_owned()));
};
}
#[inline]
@@ -255,7 +250,15 @@ impl Session {
}
}
pub fn input_key(name: &str, down: bool, press: bool, alt: bool, ctrl: bool, shift: bool, command: bool) {
pub fn input_key(
name: &str,
down: bool,
press: bool,
alt: bool,
ctrl: bool,
shift: bool,
command: bool,
) {
if let Some(session) = SESSION.read().unwrap().as_ref() {
let chars: Vec<char> = name.chars().collect();
if chars.len() == 1 {
@@ -277,7 +280,16 @@ impl Session {
Self::send_msg_static(msg_out);
}
fn _input_key(&self, key: Key, down: bool, press: bool, alt: bool, ctrl: bool, shift: bool, command: bool) {
fn _input_key(
&self,
key: Key,
down: bool,
press: bool,
alt: bool,
ctrl: bool,
shift: bool,
command: bool,
) {
let v = if press {
3
} else if down {
@@ -946,16 +958,21 @@ pub fn make_fd_to_json(fd: FileDirectory) -> String {
#[cfg(target_os = "android")]
pub mod connection_manager {
use std::{
collections::HashMap,
iter::FromIterator,
rc::{Rc, Weak},
sync::{Mutex, RwLock},
};
use super::*;
use crate::ipc;
use crate::ipc::Data;
use crate::server::Connection as Conn;
use hbb_common::{
allow_err, log,
allow_err,
config::Config,
fs, log,
message_proto::*,
protobuf::Message as _,
tokio::{
self,
sync::mpsc::{UnboundedReceiver, UnboundedSender},
@@ -965,6 +982,8 @@ pub mod connection_manager {
use scrap::android::call_main_service_set_by_name;
use serde_derive::Serialize;
use super::EVENT_STREAM;
#[derive(Debug, Serialize, Clone)]
struct Client {
id: i32,
@@ -1030,10 +1049,7 @@ pub mod connection_manager {
log::debug!("call_service_set_by_name fail,{}", e);
}
// send to UI,refresh widget
if let Some(session) = Session::get().read().unwrap().as_ref() {
session
.push_event("on_client_authorized", vec![("client", &client_json)]);
};
push_event("on_client_authorized", vec![("client", &client_json)]);
} else {
let client_json = serde_json::to_string(&client).unwrap_or("".into());
// send to Android service,active notification no matter UI is shown or not.
@@ -1045,12 +1061,7 @@ pub mod connection_manager {
log::debug!("call_service_set_by_name fail,{}", e);
}
// send to UI,refresh widget
if let Some(session) = Session::get().read().unwrap().as_ref() {
session.push_event(
"try_start_without_auth",
vec![("client", &client_json)],
);
};
push_event("try_start_without_auth", vec![("client", &client_json)]);
}
CLIENTS.write().unwrap().insert(id, client);
}
@@ -1072,6 +1083,16 @@ pub mod connection_manager {
remove_connection(current_id);
}
fn push_event(name: &str, event: Vec<(&str, &str)>) {
let mut h: HashMap<&str, &str> = event.iter().cloned().collect();
assert!(h.get("name").is_none());
h.insert("name", name);
if let Some(s) = EVENT_STREAM.read().unwrap().as_ref() {
s.add(serde_json::ser::to_string(&h).unwrap_or("".to_owned()));
};
}
pub fn get_clients_state() -> String {
let clients = CLIENTS.read().unwrap();
let res = Vec::from_iter(clients.values().cloned());
@@ -1115,19 +1136,15 @@ pub mod connection_manager {
}
}
if let Some(session) = Session::get().read().unwrap().as_ref() {
session.push_event("on_client_remove", vec![("id", &id.to_string())]);
};
push_event("on_client_remove", vec![("id", &id.to_string())]);
}
// server mode handle chat from other peers
fn handle_chat(id: i32, text: String) {
if let Some(session) = Session::get().read().unwrap().as_ref() {
session.push_event(
"chat_server_mode",
vec![("id", &id.to_string()), ("text", &text)],
);
};
push_event(
"chat_server_mode",
vec![("id", &id.to_string()), ("text", &text)],
);
}
// server mode send chat to peer

View File

@@ -1,9 +1,10 @@
use crate::client::file_trait::FileManager;
#[cfg(target_os = "android")]
use crate::mobile::connection_manager::{self, get_clients_length, get_clients_state};
use crate::mobile::{make_fd_to_json, Session};
use crate::mobile::{self, make_fd_to_json, Session};
use flutter_rust_bridge::StreamSink;
use hbb_common::ResultType;
use hbb_common::{
config::{self, Config, PeerConfig, ONLINE, LocalConfig},
config::{self, Config, LocalConfig, PeerConfig, ONLINE},
fs, log,
};
use serde_json::{Number, Value};
@@ -34,6 +35,11 @@ fn initialize(app_dir: &str) {
crate::common::check_software_update();
}
pub fn start_event_stream(s: StreamSink<String>) -> ResultType<()> {
let _ = mobile::EVENT_STREAM.write().unwrap().insert(s);
Ok(())
}
#[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();
@@ -417,9 +423,6 @@ unsafe extern "C" fn set_by_name(name: *const c_char, value: *const c_char) {
}
}
// Server Side
"ensure_init_event_queue" => {
Session::ensure_init_event_queue();
}
"update_password" => {
if value.is_empty() {
Config::set_password(&Config::get_auto_password());