switch sides: use ipc to pass msg from ui to server

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2023-01-17 20:16:36 +08:00
parent 81a60725f4
commit e57949d472
4 changed files with 46 additions and 15 deletions

View File

@ -208,7 +208,8 @@ pub enum Data {
Empty, Empty,
Disconnected, Disconnected,
DataPortableService(DataPortableService), DataPortableService(DataPortableService),
SwitchBack, SwitchSidesRequest(String),
SwitchSidesBack,
} }
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
@ -429,6 +430,15 @@ async fn handle(data: Data, stream: &mut Connection) {
Data::TestRendezvousServer => { Data::TestRendezvousServer => {
crate::test_rendezvous_server(); crate::test_rendezvous_server();
} }
Data::SwitchSidesRequest(id) => {
let uuid = uuid::Uuid::new_v4();
crate::server::insert_switch_sides_uuid(id, uuid.clone());
allow_err!(
stream
.send(&Data::SwitchSidesRequest(uuid.to_string()))
.await
);
}
_ => {} _ => {}
} }
} }

View File

@ -366,7 +366,7 @@ impl Connection {
log::error!("Failed to start portable service from cm:{:?}", e); log::error!("Failed to start portable service from cm:{:?}", e);
} }
} }
ipc::Data::SwitchBack => { ipc::Data::SwitchSidesBack => {
let mut misc = Misc::new(); let mut misc = Misc::new();
misc.set_switch_back(SwitchBack::default()); misc.set_switch_back(SwitchBack::default());
let mut msg = Message::new(); let mut msg = Message::new();
@ -1796,7 +1796,6 @@ impl Connection {
} }
} }
#[cfg(feature = "flutter")]
pub fn insert_switch_sides_uuid(id: String, uuid: uuid::Uuid) { pub fn insert_switch_sides_uuid(id: String, uuid: uuid::Uuid) {
SWITCH_SIDES_UUID SWITCH_SIDES_UUID
.lock() .lock()

View File

@ -248,7 +248,7 @@ pub fn get_clients_length() -> usize {
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
pub fn switch_back(id: i32) { pub fn switch_back(id: i32) {
if let Some(client) = CLIENTS.read().unwrap().get(&id) { if let Some(client) = CLIENTS.read().unwrap().get(&id) {
allow_err!(client.tx.send(Data::SwitchBack)); allow_err!(client.tx.send(Data::SwitchSidesBack));
}; };
} }

View File

@ -17,6 +17,7 @@ use hbb_common::{fs, get_version_number, log, Stream};
use rdev::{Event, EventType::*}; use rdev::{Event, EventType::*};
use std::collections::HashMap; use std::collections::HashMap;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, RwLock}; use std::sync::{Arc, Mutex, RwLock};
use uuid::Uuid; use uuid::Uuid;
@ -619,17 +620,38 @@ impl<T: InvokeUiSession> Session<T> {
self.send(Data::ElevateWithLogon(username, password)); self.send(Data::ElevateWithLogon(username, password));
} }
pub fn switch_sides(&self) { #[tokio::main(flavor = "current_thread")]
let uuid = Uuid::new_v4(); pub async fn switch_sides(&self) {
crate::server::insert_switch_sides_uuid(self.id.clone(), uuid.clone()); match crate::ipc::connect(1000, "").await {
let mut misc = Misc::new(); Ok(mut conn) => {
misc.set_switch_sides_request(SwitchSidesRequest { if conn
uuid: Bytes::from(uuid.as_bytes().to_vec()), .send(&crate::ipc::Data::SwitchSidesRequest(self.id.to_string()))
..Default::default() .await
}); .is_ok()
let mut msg_out = Message::new(); {
msg_out.set_misc(misc); if let Ok(Some(data)) = conn.next_timeout(1000).await {
self.send(Data::Message(msg_out)); match data {
crate::ipc::Data::SwitchSidesRequest(str_uuid) => {
if let Ok(uuid) = Uuid::from_str(&str_uuid) {
let mut misc = Misc::new();
misc.set_switch_sides_request(SwitchSidesRequest {
uuid: Bytes::from(uuid.as_bytes().to_vec()),
..Default::default()
});
let mut msg_out = Message::new();
msg_out.set_misc(misc);
self.send(Data::Message(msg_out));
}
}
_ => {}
}
}
}
}
Err(err) => {
log::info!("server not started (will try to start): {}", err);
}
}
} }
} }