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,
Disconnected,
DataPortableService(DataPortableService),
SwitchBack,
SwitchSidesRequest(String),
SwitchSidesBack,
}
#[tokio::main(flavor = "current_thread")]
@ -429,6 +430,15 @@ async fn handle(data: Data, stream: &mut Connection) {
Data::TestRendezvousServer => {
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);
}
}
ipc::Data::SwitchBack => {
ipc::Data::SwitchSidesBack => {
let mut misc = Misc::new();
misc.set_switch_back(SwitchBack::default());
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) {
SWITCH_SIDES_UUID
.lock()

View File

@ -248,7 +248,7 @@ pub fn get_clients_length() -> usize {
#[cfg(feature = "flutter")]
pub fn switch_back(id: i32) {
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 std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use uuid::Uuid;
@ -619,17 +620,38 @@ impl<T: InvokeUiSession> Session<T> {
self.send(Data::ElevateWithLogon(username, password));
}
pub fn switch_sides(&self) {
let uuid = Uuid::new_v4();
crate::server::insert_switch_sides_uuid(self.id.clone(), uuid.clone());
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));
#[tokio::main(flavor = "current_thread")]
pub async fn switch_sides(&self) {
match crate::ipc::connect(1000, "").await {
Ok(mut conn) => {
if conn
.send(&crate::ipc::Data::SwitchSidesRequest(self.id.to_string()))
.await
.is_ok()
{
if let Ok(Some(data)) = conn.next_timeout(1000).await {
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);
}
}
}
}