move clipboard file service to cm module

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2022-02-22 14:17:50 +08:00
parent 69769cc123
commit 8834251eec
12 changed files with 720 additions and 391 deletions

View File

@@ -1,104 +0,0 @@
use super::*;
use clipboard::{create_cliprdr_context, get_rx_client_msg, server_msg, ConnID};
use hbb_common::{
log,
tokio::sync::{
mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
Mutex as TokioMutex,
},
tokio::time::{self, Duration, Instant},
ResultType,
};
use std::sync::atomic::{AtomicBool, Ordering};
pub const NAME: &'static str = "cliprdr";
lazy_static::lazy_static! {
static ref MSG_CHANNEL_SERVER: (UnboundedSender<(ConnID, Cliprdr)>, TokioMutex<UnboundedReceiver<(ConnID, Cliprdr)>>) = {
let (tx, rx) = unbounded_channel();
(tx, TokioMutex::new(rx))
};
}
static RUNNING: AtomicBool = AtomicBool::new(false);
pub fn new() -> GenericService {
let sp = GenericService::new(NAME, true);
sp.run::<_>(listen::run);
sp
}
pub fn handle_serve_cliprdr_msg(id: i32, msg: Cliprdr) {
if RUNNING.load(Ordering::SeqCst) {
log::debug!("handle handle_serve_cliprdr_msg");
MSG_CHANNEL_SERVER
.0
.send((
ConnID {
server_conn_id: id as u32,
remote_conn_id: 0,
},
msg,
))
.unwrap();
} else {
// should not reach this branch
}
}
mod listen {
use super::*;
static WAIT: Duration = Duration::from_millis(1500);
#[tokio::main]
pub async fn run(sp: GenericService) -> ResultType<()> {
let mut cliprdr_context = create_cliprdr_context(true, false)?;
RUNNING.store(false, Ordering::SeqCst);
let mut timer = time::interval_at(Instant::now() + WAIT, WAIT);
let mut client_rx = get_rx_client_msg().lock().await;
let mut server_rx = MSG_CHANNEL_SERVER.1.lock().await;
while sp.ok() {
RUNNING.store(true, Ordering::SeqCst);
tokio::select! {
msg = client_rx.recv() => {
match msg {
Some((conn_id, msg)) => {
if conn_id.server_conn_id == 0 {
sp.send(msg)
} else {
sp.send_to(msg, conn_id.server_conn_id as i32)
}
}
None => {
// unreachable!()
}
}
}
msg = server_rx.recv() => {
match msg {
Some((conn_id, msg)) => {
let res = server_msg(&mut cliprdr_context, conn_id, msg);
if res != 0 {
// log::warn!("failed to process message for {}", id);
}
}
None => {
// unreachable!()
}
}
}
_ = timer.tick() => {},
}
sp.snapshot(|_| Ok(()))?;
}
RUNNING.store(false, Ordering::SeqCst);
log::info!("Clipboard listener stopped!");
Ok(())
}
}

View File

@@ -1,4 +1,7 @@
use super::{input_service::*, *};
#[cfg(windows)]
use crate::{clipboard_file::*, common::update_clipboard, ipc};
#[cfg(not(windows))]
use crate::{common::update_clipboard, ipc};
use hbb_common::{
config::Config,
@@ -243,17 +246,17 @@ impl Connection {
} else if &name == "file" {
conn.file = enabled;
conn.send_permission(Permission::File, enabled).await;
#[cfg(windows)]
if let Some(s) = conn.server.upgrade() {
s.write().unwrap().subscribe(
super::clipboard_file_service::NAME,
conn.inner.clone(), conn.file_transfer_enabled());
}
}
}
ipc::Data::RawMessage(bytes) => {
allow_err!(conn.stream.send_raw(bytes).await);
}
ipc::Data::ClipbaordFile(_clip) => {
if conn.file_transfer_enabled() {
#[cfg(windows)]
allow_err!(conn.stream.send(&clip_2_msg(_clip)).await);
}
}
_ => {}
}
},
@@ -620,10 +623,6 @@ impl Connection {
if !self.audio_enabled() {
noperms.push(super::audio_service::NAME);
}
if !self.file_transfer_enabled() {
#[cfg(windows)]
noperms.push(super::clipboard_file_service::NAME);
}
s.write()
.unwrap()
.add_connection(self.inner.clone(), &noperms);
@@ -849,9 +848,13 @@ impl Connection {
update_clipboard(cb, None);
}
}
#[cfg(windows)]
Some(message::Union::cliprdr(clip)) => {
clipboard_file_service::handle_serve_cliprdr_msg(self.inner.id, clip)
Some(message::Union::cliprdr(_clip)) => {
if self.file_transfer_enabled() {
#[cfg(windows)]
if let Some(clip) = msg_2_clip(_clip) {
self.send_to_cm(ipc::Data::ClipbaordFile(clip))
}
}
}
Some(message::Union::file_action(fa)) => {
if self.file_transfer.is_some() {
@@ -1017,13 +1020,6 @@ impl Connection {
if let Ok(q) = o.enable_file_transfer.enum_value() {
if q != BoolOption::NotSet {
self.enable_file_transfer = q == BoolOption::Yes;
if let Some(s) = self.server.upgrade() {
s.write().unwrap().subscribe(
super::clipboard_file_service::NAME,
self.inner.clone(),
self.file_transfer_enabled(),
);
}
}
}
if let Ok(q) = o.disable_clipboard.enum_value() {