switch sides

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2023-01-17 13:28:33 +08:00
parent 45c0e10102
commit 333092f983
49 changed files with 373 additions and 61 deletions

View File

@@ -40,6 +40,7 @@ lazy_static::lazy_static! {
static ref LOGIN_FAILURES: Arc::<Mutex<HashMap<String, (i32, i32, i32)>>> = Default::default();
static ref SESSIONS: Arc::<Mutex<HashMap<String, Session>>> = Default::default();
static ref ALIVE_CONNS: Arc::<Mutex<Vec<i32>>> = Default::default();
pub static ref SWITCH_SIDES_UUID: Arc::<Mutex<HashMap<String, (Instant, uuid::Uuid)>>> = Default::default();
}
pub static CLICK_TIME: AtomicI64 = AtomicI64::new(0);
pub static MOUSE_MOVE_TIME: AtomicI64 = AtomicI64::new(0);
@@ -102,6 +103,7 @@ pub struct Connection {
chat_unanswered: bool,
close_manually: bool,
elevation_requested: bool,
from_switch: bool,
}
impl Subscriber for ConnInner {
@@ -134,6 +136,7 @@ const MILLI1: Duration = Duration::from_millis(1);
const SEND_TIMEOUT_VIDEO: u64 = 12_000;
const SEND_TIMEOUT_OTHER: u64 = SEND_TIMEOUT_VIDEO * 10;
const SESSION_TIMEOUT: Duration = Duration::from_secs(30);
const SWITCH_SIDES_TIMEOUT: Duration = Duration::from_secs(30);
impl Connection {
pub async fn start(
@@ -198,6 +201,7 @@ impl Connection {
chat_unanswered: false,
close_manually: false,
elevation_requested: false,
from_switch: false,
};
#[cfg(not(any(target_os = "android", target_os = "ios")))]
tokio::spawn(async move {
@@ -362,6 +366,13 @@ impl Connection {
log::error!("Failed to start portable service from cm:{:?}", e);
}
}
ipc::Data::SwitchBack => {
let mut misc = Misc::new();
misc.set_switch_back(SwitchBack::default());
let mut msg = Message::new();
msg.set_misc(misc);
conn.send(msg).await;
}
_ => {}
}
},
@@ -954,6 +965,7 @@ impl Connection {
file_transfer_enabled: self.file_transfer_enabled(),
restart: self.restart,
recording: self.recording,
from_switch: self.from_switch,
});
}
@@ -1078,29 +1090,33 @@ impl Connection {
return Config::get_option(enable_prefix_option).is_empty();
}
async fn on_message(&mut self, msg: Message) -> bool {
if let Some(message::Union::LoginRequest(lr)) = msg.union {
self.lr = lr.clone();
if let Some(o) = lr.option.as_ref() {
self.update_option(o).await;
if let Some(q) = o.video_codec_state.clone().take() {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::State(q),
);
} else {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::DisableHwIfNotExist,
);
}
async fn handle_login_request_without_validation(&mut self, lr: &LoginRequest) {
self.lr = lr.clone();
if let Some(o) = lr.option.as_ref() {
self.update_option(o).await;
if let Some(q) = o.video_codec_state.clone().take() {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::State(q),
);
} else {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::DisableHwIfNotExist,
);
}
self.video_ack_required = lr.video_ack_required;
} else {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::DisableHwIfNotExist,
);
}
self.video_ack_required = lr.video_ack_required;
}
async fn on_message(&mut self, msg: Message) -> bool {
if let Some(message::Union::LoginRequest(lr)) = msg.union {
self.handle_login_request_without_validation(&lr).await;
if self.authorized {
return true;
}
@@ -1247,6 +1263,21 @@ impl Connection {
.unwrap()
.update_network_delay(new_delay);
}
} else if let Some(message::Union::SwitchSidesResponse(_s)) = msg.union {
#[cfg(feature = "flutter")]
if let Some(lr) = _s.lr.clone().take() {
self.handle_login_request_without_validation(&lr).await;
let uuid_old = SWITCH_SIDES_UUID.lock().unwrap().remove(&lr.my_id);
if let Ok(uuid) = uuid::Uuid::from_slice(_s.uuid.to_vec().as_ref()) {
if let Some((instant, uuid_old)) = uuid_old {
if instant.elapsed() < SWITCH_SIDES_TIMEOUT && uuid == uuid_old {
self.from_switch = true;
self.try_start_cm(lr.my_id.clone(), lr.my_name.clone(), true);
self.send_logon_response().await;
}
}
}
}
} else if self.authorized {
match msg.union {
Some(message::Union::MouseEvent(me)) => {
@@ -1536,6 +1567,11 @@ impl Connection {
}
_ => {}
},
#[cfg(feature = "flutter")]
Some(misc::Union::SwitchSidesRequest(s)) => {
crate::flutter::switch_sides(&self.lr.my_id, &s.uuid);
return false;
}
_ => {}
},
_ => {}