use uuid as session id

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2023-06-06 07:39:44 +08:00
parent 71838ad821
commit 2ececed0c1
36 changed files with 706 additions and 546 deletions

View File

@@ -1,11 +1,11 @@
use crate::{
client::*,
flutter_ffi::EventToUI,
flutter_ffi::{EventToUI, SessionID},
ui_session_interface::{io_loop, InvokeUiSession, Session},
};
use flutter_rust_bridge::StreamSink;
use hbb_common::{
bail, config::LocalConfig, get_version_number, log, message_proto::*,
anyhow::anyhow, bail, config::LocalConfig, get_version_number, log, message_proto::*,
rendezvous_proto::ConnType, ResultType,
};
#[cfg(feature = "flutter_texture_render")]
@@ -24,6 +24,7 @@ use std::{
collections::HashMap,
ffi::CString,
os::raw::{c_char, c_int},
str::FromStr,
sync::{Arc, RwLock},
};
@@ -40,8 +41,8 @@ pub(crate) const APP_TYPE_DESKTOP_FILE_TRANSFER: &str = "file transfer";
pub(crate) const APP_TYPE_DESKTOP_PORT_FORWARD: &str = "port forward";
lazy_static::lazy_static! {
pub(crate) static ref CUR_SESSION_ID: RwLock<String> = Default::default();
pub(crate) static ref SESSIONS: RwLock<HashMap<String, Session<FlutterHandler>>> = Default::default();
pub(crate) static ref CUR_SESSION_ID: RwLock<SessionID> = Default::default();
pub(crate) static ref SESSIONS: RwLock<HashMap<SessionID, Session<FlutterHandler>>> = Default::default();
static ref GLOBAL_EVENT_STREAM: RwLock<HashMap<String, StreamSink<String>>> = Default::default(); // rust to dart event channel
}
@@ -695,6 +696,7 @@ impl InvokeUiSession for FlutterHandler {
/// * `is_file_transfer` - If the session is used for file transfer.
/// * `is_port_forward` - If the session is used for port forward.
pub fn session_add(
session_id: &SessionID,
id: &str,
is_file_transfer: bool,
is_port_forward: bool,
@@ -703,11 +705,11 @@ pub fn session_add(
force_relay: bool,
password: String,
) -> ResultType<Session<FlutterHandler>> {
let session_id = get_session_id(id.to_owned());
LocalConfig::set_remote_id(&session_id);
LocalConfig::set_remote_id(&id);
let session: Session<FlutterHandler> = Session {
id: session_id.clone(),
session_id: session_id.clone(),
id: id.to_owned(),
password,
server_keyboard_enabled: Arc::new(RwLock::new(true)),
server_file_transfer_enabled: Arc::new(RwLock::new(true)),
@@ -737,13 +739,14 @@ pub fn session_add(
.lc
.write()
.unwrap()
.initialize(session_id, conn_type, switch_uuid, force_relay);
.initialize(id.to_owned(), conn_type, switch_uuid, force_relay);
if let Some(same_id_session) = SESSIONS
.write()
.unwrap()
.insert(id.to_owned(), session.clone())
.insert(session_id.to_owned(), session.clone())
{
log::error!("Should not happen");
same_id_session.close();
}
@@ -756,8 +759,12 @@ pub fn session_add(
///
/// * `id` - The identifier of the remote session with prefix. Regex: [\w]*[\_]*[\d]+
/// * `events2ui` - The events channel to ui.
pub fn session_start_(id: &str, event_stream: StreamSink<EventToUI>) -> ResultType<()> {
if let Some(session) = SESSIONS.write().unwrap().get_mut(id) {
pub fn session_start_(
session_id: &SessionID,
id: &str,
event_stream: StreamSink<EventToUI>,
) -> ResultType<()> {
if let Some(session) = SESSIONS.write().unwrap().get_mut(session_id) {
#[cfg(feature = "flutter_texture_render")]
log::info!(
"Session {} start, render by flutter texture rgba plugin",
@@ -788,8 +795,14 @@ pub fn update_text_clipboard_required() {
#[inline]
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn other_sessions_running(id: &str) -> bool {
SESSIONS.read().unwrap().keys().filter(|k| *k != id).count() != 0
pub fn other_sessions_running(session_id: &SessionID) -> bool {
SESSIONS
.read()
.unwrap()
.keys()
.filter(|k| *k != session_id)
.count()
!= 0
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
@@ -928,15 +941,6 @@ pub mod connection_manager {
}
}
#[inline]
pub fn get_session_id(id: String) -> String {
return if let Some(index) = id.find('_') {
id[index + 1..].to_string()
} else {
id
};
}
pub fn make_fd_flutter(id: i32, entries: &Vec<FileEntry>, only_count: bool) -> String {
let mut m = serde_json::Map::new();
m.insert("id".into(), json!(id));
@@ -964,13 +968,13 @@ pub fn make_fd_flutter(id: i32, entries: &Vec<FileEntry>, only_count: bool) -> S
serde_json::to_string(&m).unwrap_or("".into())
}
pub fn get_cur_session_id() -> String {
pub fn get_cur_session_id() -> SessionID {
CUR_SESSION_ID.read().unwrap().clone()
}
pub fn set_cur_session_id(id: String) {
if get_cur_session_id() != id {
*CUR_SESSION_ID.write().unwrap() = id;
pub fn set_cur_session_id(session_id: SessionID) {
if get_cur_session_id() != session_id {
*CUR_SESSION_ID.write().unwrap() = session_id;
}
}
@@ -995,12 +999,17 @@ fn serialize_resolutions(resolutions: &Vec<Resolution>) -> String {
serde_json::ser::to_string(&v).unwrap_or("".to_string())
}
fn char_to_session_id(c: *const char) -> ResultType<SessionID> {
let cstr = unsafe { std::ffi::CStr::from_ptr(c as _) };
let str = cstr.to_str()?;
SessionID::from_str(str).map_err(|e| anyhow!("{:?}", e))
}
#[no_mangle]
#[cfg(not(feature = "flutter_texture_render"))]
pub fn session_get_rgba_size(id: *const char) -> usize {
let id = unsafe { std::ffi::CStr::from_ptr(id as _) };
if let Ok(id) = id.to_str() {
if let Some(session) = SESSIONS.read().unwrap().get(id) {
pub fn session_get_rgba_size(_session_uuid_str: *const char) -> usize {
#[cfg(not(feature = "flutter_texture_render"))]
if let Ok(session_id) = char_to_session_id(_session_uuid_str) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
return session.rgba.read().unwrap().len();
}
}
@@ -1008,27 +1017,20 @@ pub fn session_get_rgba_size(id: *const char) -> usize {
}
#[no_mangle]
#[cfg(feature = "flutter_texture_render")]
pub fn session_get_rgba_size(_id: *const char) -> usize {
0
}
#[no_mangle]
pub fn session_get_rgba(id: *const char) -> *const u8 {
let id = unsafe { std::ffi::CStr::from_ptr(id as _) };
if let Ok(id) = id.to_str() {
if let Some(session) = SESSIONS.read().unwrap().get(id) {
pub fn session_get_rgba(session_uuid_str: *const char) -> *const u8 {
if let Ok(session_id) = char_to_session_id(session_uuid_str) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
return session.get_rgba();
}
}
std::ptr::null()
}
#[no_mangle]
pub fn session_next_rgba(id: *const char) {
let id = unsafe { std::ffi::CStr::from_ptr(id as _) };
if let Ok(id) = id.to_str() {
if let Some(session) = SESSIONS.read().unwrap().get(id) {
pub fn session_next_rgba(session_uuid_str: *const char) {
if let Ok(session_id) = char_to_session_id(session_uuid_str) {
if let Some(session) = SESSIONS.read().unwrap().get(&session_id) {
return session.next_rgba();
}
}
@@ -1036,24 +1038,26 @@ pub fn session_next_rgba(id: *const char) {
#[inline]
#[no_mangle]
#[cfg(feature = "flutter_texture_render")]
pub fn session_register_texture(id: *const char, ptr: usize) {
let id = unsafe { std::ffi::CStr::from_ptr(id as _) };
if let Ok(id) = id.to_str() {
if let Some(session) = SESSIONS.write().unwrap().get_mut(id) {
return session.register_texture(ptr);
pub fn session_register_texture(_session_uuid_str: *const char, _ptr: usize) {
#[cfg(feature = "flutter_texture_render")]
if let Ok(session_id) = char_to_session_id(_session_uuid_str) {
if let Some(session) = SESSIONS.write().unwrap().get_mut(&session_id) {
return session.register_texture(_ptr);
}
}
}
#[inline]
#[no_mangle]
#[cfg(not(feature = "flutter_texture_render"))]
pub fn session_register_texture(_id: *const char, _ptr: usize) {}
#[inline]
pub fn push_session_event(peer: &str, name: &str, event: Vec<(&str, &str)>) -> Option<bool> {
SESSIONS.read().unwrap().get(peer)?.push_event(name, event)
pub fn push_session_event(
session_id: &SessionID,
name: &str,
event: Vec<(&str, &str)>,
) -> Option<bool> {
SESSIONS
.read()
.unwrap()
.get(session_id)?
.push_event(name, event)
}
#[inline]