win, clipboard, debug

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-06-20 00:50:01 +08:00
parent 0094d306a8
commit 7740492fb0
5 changed files with 24 additions and 46 deletions

View File

@ -1,27 +1,19 @@
use crate::cliprdr::*; use crate::cliprdr::*;
use hbb_common::log; use hbb_common::log;
use std::sync::{Arc, Mutex}; use std::sync::Mutex;
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref CONTEXT_SEND: Arc<Mutex<ContextSend>> = Arc::new(Mutex::new(ContextSend::new())); static ref CONTEXT_SEND: ContextSend = ContextSend{addr: Mutex::new(0)};
} }
pub struct ContextSend { pub struct ContextSend {
cm_enabled: bool, addr: Mutex<u64>,
addr: u64,
} }
impl ContextSend { impl ContextSend {
fn new() -> Self {
Self {
cm_enabled: false,
addr: 0,
}
}
#[inline] #[inline]
pub fn is_cm_enabled() -> bool { pub fn is_enabled() -> bool {
CONTEXT_SEND.lock().unwrap().cm_enabled *CONTEXT_SEND.addr.lock().unwrap() != 0
} }
pub fn set_is_stopped() { pub fn set_is_stopped() {
@ -31,14 +23,14 @@ impl ContextSend {
}); });
} }
pub fn enable(enabled: bool, is_cm_side: bool, is_server_process: bool) { pub fn enable(enabled: bool) {
let mut lock = CONTEXT_SEND.lock().unwrap(); let mut lock = CONTEXT_SEND.addr.lock().unwrap();
if enabled { if enabled {
if lock.addr == 0 { if *lock == 0 {
match crate::create_cliprdr_context(true, false) { match crate::create_cliprdr_context(true, false) {
Ok(context) => { Ok(context) => {
log::info!("clipboard context for file transfer created."); log::info!("clipboard context for file transfer created.");
lock.addr = Box::into_raw(context) as _; *lock = Box::into_raw(context) as _;
} }
Err(err) => { Err(err) => {
log::error!( log::error!(
@ -48,28 +40,22 @@ impl ContextSend {
} }
} }
} }
if is_cm_side {
lock.cm_enabled = true;
}
} else { } else {
if lock.addr != 0 { if *lock != 0 {
if is_server_process {
unsafe { unsafe {
let _ = Box::from_raw(lock.addr as *mut CliprdrClientContext); let _ = Box::from_raw(*lock as *mut CliprdrClientContext);
} }
log::info!("clipboard context for file transfer destroyed."); log::info!("clipboard context for file transfer destroyed.");
lock.addr = 0; *lock = 0;
}
lock.cm_enabled = false;
} }
} }
} }
pub fn proc<F: FnOnce(&mut Box<CliprdrClientContext>) -> u32>(f: F) -> u32 { pub fn proc<F: FnOnce(&mut Box<CliprdrClientContext>) -> u32>(f: F) -> u32 {
let lock = CONTEXT_SEND.lock().unwrap(); let lock = CONTEXT_SEND.addr.lock().unwrap();
if lock.addr != 0 { if *lock != 0 {
unsafe { unsafe {
let mut context = Box::from_raw(lock.addr as *mut CliprdrClientContext); let mut context = Box::from_raw(*lock as *mut CliprdrClientContext);
let code = f(&mut context); let code = f(&mut context);
std::mem::forget(context); std::mem::forget(context);
code code

View File

@ -1550,7 +1550,7 @@ impl<T: InvokeUiSession> Remote<T> {
{ {
let enabled = *self.handler.server_file_transfer_enabled.read().unwrap() let enabled = *self.handler.server_file_transfer_enabled.read().unwrap()
&& self.handler.lc.read().unwrap().enable_file_transfer.v; && self.handler.lc.read().unwrap().enable_file_transfer.v;
ContextSend::enable(enabled, false, false); ContextSend::enable(enabled);
} }
} }

View File

@ -123,7 +123,7 @@ pub fn core_main() -> Option<Vec<String>> {
init_plugins(&args); init_plugins(&args);
if args.is_empty() { if args.is_empty() {
#[cfg(windows)] #[cfg(windows)]
clipboard::ContextSend::enable(true, false, false); clipboard::ContextSend::enable(true);
std::thread::spawn(move || crate::start_server(false)); std::thread::spawn(move || crate::start_server(false));
} else { } else {
#[cfg(windows)] #[cfg(windows)]

View File

@ -338,7 +338,7 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
#[cfg(windows)] #[cfg(windows)]
{ {
if ContextSend::is_cm_enabled() { if ContextSend::is_enabled() {
allow_err!( allow_err!(
self.stream self.stream
.send(&Data::ClipboardFile(clipboard::ClipboardFile::MonitorReady)) .send(&Data::ClipboardFile(clipboard::ClipboardFile::MonitorReady))
@ -404,7 +404,7 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
#[cfg(windows)] #[cfg(windows)]
{ {
let is_stopping_allowed = _clip.is_stopping_allowed_from_peer(); let is_stopping_allowed = _clip.is_stopping_allowed_from_peer();
let is_clipboard_enabled = ContextSend::is_cm_enabled(); let is_clipboard_enabled = ContextSend::is_enabled();
let file_transfer_enabled = self.file_transfer_enabled; let file_transfer_enabled = self.file_transfer_enabled;
let stop = !is_stopping_allowed && !(is_clipboard_enabled && file_transfer_enabled); let stop = !is_stopping_allowed && !(is_clipboard_enabled && file_transfer_enabled);
log::debug!( log::debug!(
@ -469,7 +469,7 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
#[cfg(windows)] #[cfg(windows)]
{ {
let is_stopping_allowed = _clip.is_stopping_allowed(); let is_stopping_allowed = _clip.is_stopping_allowed();
let is_clipboard_enabled = ContextSend::is_cm_enabled(); let is_clipboard_enabled = ContextSend::is_enabled();
let file_transfer_enabled = self.file_transfer_enabled; let file_transfer_enabled = self.file_transfer_enabled;
let file_transfer_enabled_peer = self.file_transfer_enabled_peer; let file_transfer_enabled_peer = self.file_transfer_enabled_peer;
let stop = is_stopping_allowed && !(is_clipboard_enabled && file_transfer_enabled && file_transfer_enabled_peer); let stop = is_stopping_allowed && !(is_clipboard_enabled && file_transfer_enabled && file_transfer_enabled_peer);
@ -537,11 +537,7 @@ pub async fn start_ipc<T: InvokeUiCM>(cm: ConnectionManager<T>) {
}); });
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
ContextSend::enable( ContextSend::enable(Config::get_option("enable-file-transfer").is_empty());
Config::get_option("enable-file-transfer").is_empty(),
true,
crate::is_server(),
);
match ipc::new_listener("_cm").await { match ipc::new_listener("_cm").await {
Ok(mut incoming) => { Ok(mut incoming) => {

View File

@ -882,11 +882,7 @@ async fn check_connect_status_(reconnect: bool, rx: mpsc::UnboundedReceiver<ipc:
{ {
let b = OPTIONS.lock().unwrap().get("enable-file-transfer").map(|x| x.to_string()).unwrap_or_default(); let b = OPTIONS.lock().unwrap().get("enable-file-transfer").map(|x| x.to_string()).unwrap_or_default();
if b != enable_file_transfer { if b != enable_file_transfer {
clipboard::ContextSend::enable( clipboard::ContextSend::enable(b.is_empty());
b.is_empty(),
true,
crate::is_server(),
);
enable_file_transfer = b; enable_file_transfer = b;
} }
} }