Merge pull request #883 from Kingtous/flutter_desktop
opt: add prefix identifier for each session & fix close and a read write error
This commit is contained in:
		
						commit
						4158a2d50e
					
				| @ -2,9 +2,13 @@ import 'dart:convert'; | ||||
| import 'dart:math'; | ||||
| 
 | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:flutter_hbb/common.dart'; | ||||
| import 'package:flutter_hbb/desktop/pages/remote_page.dart'; | ||||
| import 'package:flutter_hbb/desktop/widgets/titlebar_widget.dart'; | ||||
| import 'package:flutter_hbb/utils/multi_window_manager.dart'; | ||||
| import 'package:get/get.dart'; | ||||
| 
 | ||||
| import '../../models/model.dart'; | ||||
| 
 | ||||
| class ConnectionTabPage extends StatefulWidget { | ||||
|   final Map<String, dynamic> params; | ||||
| @ -51,6 +55,15 @@ class _ConnectionTabPageState extends State<ConnectionTabPage> | ||||
|             }); | ||||
|           } | ||||
|         }); | ||||
|       } else if (call.method == "onDestroy") { | ||||
|         print("executing onDestroy hook, closing ${connectionIds}"); | ||||
|         connectionIds.forEach((id) { | ||||
|           final tag = '${id}'; | ||||
|           ffi(tag).close().then((_) { | ||||
|             Get.delete<FFI>(tag: tag); | ||||
|           }); | ||||
|         }); | ||||
|         Get.back(); | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
|  | ||||
| @ -2,8 +2,10 @@ import 'dart:convert'; | ||||
| import 'dart:math'; | ||||
| 
 | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:flutter_hbb/common.dart'; | ||||
| import 'package:flutter_hbb/desktop/pages/file_manager_page.dart'; | ||||
| import 'package:flutter_hbb/desktop/widgets/titlebar_widget.dart'; | ||||
| import 'package:flutter_hbb/models/model.dart'; | ||||
| import 'package:flutter_hbb/utils/multi_window_manager.dart'; | ||||
| import 'package:get/get.dart'; | ||||
| 
 | ||||
| @ -21,7 +23,7 @@ class _FileManagerTabPageState extends State<FileManagerTabPage> | ||||
|     with SingleTickerProviderStateMixin { | ||||
|   // refactor List<int> when using multi-tab | ||||
|   // this singleton is only for test | ||||
|   List<String> connectionIds = List.empty(growable: true); | ||||
|   var connectionIds = List<String>.empty(growable: true).obs; | ||||
|   var initialIndex = 0.obs; | ||||
| 
 | ||||
|   _FileManagerTabPageState(Map<String, dynamic> params) { | ||||
| @ -47,6 +49,15 @@ class _FileManagerTabPageState extends State<FileManagerTabPage> | ||||
|           connectionIds.add(id); | ||||
|           initialIndex.value = connectionIds.length - 1; | ||||
|         } | ||||
|       } else if (call.method == "onDestroy") { | ||||
|         print("executing onDestroy hook, closing ${connectionIds}"); | ||||
|         connectionIds.forEach((id) { | ||||
|           final tag = 'ft_${id}'; | ||||
|           ffi(tag).close().then((_) { | ||||
|             Get.delete<FFI>(tag: tag); | ||||
|           }); | ||||
|         }); | ||||
|         Get.back(); | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
|  | ||||
| @ -924,6 +924,7 @@ class FFI { | ||||
|       imageModel._id = id; | ||||
|       cursorModel.id = id; | ||||
|     } | ||||
|     id = isFileTransfer ? 'ft_${id}' : id; | ||||
|     final stream = bind.sessionConnect(id: id, isFileTransfer: isFileTransfer); | ||||
|     final cb = ffiModel.startEventListener(id); | ||||
|     () async { | ||||
| @ -954,10 +955,10 @@ class FFI { | ||||
|   } | ||||
| 
 | ||||
|   /// Close the remote session. | ||||
|   void close() { | ||||
|   Future<void> close() async { | ||||
|     chatModel.close(); | ||||
|     if (imageModel.image != null && !isWebDesktop) { | ||||
|       savePreference(id, cursorModel.x, cursorModel.y, canvasModel.x, | ||||
|       await savePreference(id, cursorModel.x, cursorModel.y, canvasModel.x, | ||||
|           canvasModel.y, canvasModel.scale, ffiModel.pi.currentDisplay); | ||||
|     } | ||||
|     bind.sessionClose(id: id); | ||||
| @ -1085,8 +1086,8 @@ class PeerInfo { | ||||
|   List<Display> displays = []; | ||||
| } | ||||
| 
 | ||||
| void savePreference(String id, double xCursor, double yCursor, double xCanvas, | ||||
|     double yCanvas, double scale, int currentDisplay) async { | ||||
| Future<void> savePreference(String id, double xCursor, double yCursor, | ||||
|     double xCanvas, double yCanvas, double scale, int currentDisplay) async { | ||||
|   SharedPreferences prefs = await SharedPreferences.getInstance(); | ||||
|   final p = Map<String, dynamic>(); | ||||
|   p['xCursor'] = xCursor; | ||||
|  | ||||
| @ -62,7 +62,7 @@ dependencies: | ||||
|     desktop_multi_window: | ||||
|         git: | ||||
|             url: https://github.com/Kingtous/rustdesk_desktop_multi_window | ||||
|             ref: c7d97cb6615f2def34f8bad4def01af9e0077beb | ||||
|             ref: 7b72918710921f5fe79eae2dbaa411a66f5dfb45 | ||||
|     # bitsdojo_window: ^0.1.2 | ||||
|     freezed_annotation: ^2.0.3 | ||||
|     tray_manager: 0.1.7 | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| use crate::common::make_fd_to_json; | ||||
| use crate::{client::*, flutter_ffi::EventToUI}; | ||||
| use crate::common::{make_fd_to_json}; | ||||
| use flutter_rust_bridge::{StreamSink, ZeroCopyBuffer}; | ||||
| use hbb_common::{ | ||||
|     allow_err, | ||||
| @ -49,16 +49,17 @@ impl Session { | ||||
|     ///
 | ||||
|     /// # Arguments
 | ||||
|     ///
 | ||||
|     /// * `id` - The id of the remote session.
 | ||||
|     /// * `id` - The identifier of the remote session with prefix. Regex: [\w]*[\_]*[\d]+
 | ||||
|     /// * `is_file_transfer` - If the session is used for file transfer.
 | ||||
|     pub fn start(id: &str, is_file_transfer: bool, events2ui: StreamSink<EventToUI>) { | ||||
|         LocalConfig::set_remote_id(&id); | ||||
|     pub fn start(identifier: &str, is_file_transfer: bool, events2ui: StreamSink<EventToUI>) { | ||||
|         LocalConfig::set_remote_id(&identifier); | ||||
|         // TODO check same id
 | ||||
|         let session_id = get_session_id(identifier.to_owned()); | ||||
|         // TODO close
 | ||||
|         // Self::close();
 | ||||
|         let events2ui = Arc::new(RwLock::new(events2ui)); | ||||
|         let mut session = Session { | ||||
|             id: id.to_owned(), | ||||
|             id: session_id.clone(), | ||||
|             sender: Default::default(), | ||||
|             lc: Default::default(), | ||||
|             events2ui, | ||||
| @ -67,11 +68,11 @@ impl Session { | ||||
|             .lc | ||||
|             .write() | ||||
|             .unwrap() | ||||
|             .initialize(id.to_owned(), false, false); | ||||
|             .initialize(session_id.clone(), is_file_transfer, false); | ||||
|         SESSIONS | ||||
|             .write() | ||||
|             .unwrap() | ||||
|             .insert(id.to_owned(), session.clone()); | ||||
|             .insert(identifier.to_owned(), session.clone()); | ||||
|         std::thread::spawn(move || { | ||||
|             Connection::start(session, is_file_transfer); | ||||
|         }); | ||||
| @ -179,7 +180,6 @@ impl Session { | ||||
|     /// Close the session.
 | ||||
|     pub fn close(&self) { | ||||
|         self.send(Data::Close); | ||||
|         let _ = SESSIONS.write().unwrap().remove(&self.id); | ||||
|     } | ||||
| 
 | ||||
|     /// Reconnect to the current session.
 | ||||
| @ -1659,3 +1659,12 @@ 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 | ||||
|     }; | ||||
| } | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| use crate::client::file_trait::FileManager; | ||||
| use crate::common::make_fd_to_json; | ||||
| use crate::flutter::connection_manager::{self, get_clients_length, get_clients_state}; | ||||
| use crate::flutter::{self, Session, SESSIONS}; | ||||
| use crate::common::make_fd_to_json; | ||||
| use crate::start_server; | ||||
| use crate::ui_interface; | ||||
| use flutter_rust_bridge::{StreamSink, SyncReturn, ZeroCopyBuffer}; | ||||
| @ -133,6 +133,7 @@ pub fn session_close(id: String) { | ||||
|     if let Some(session) = SESSIONS.read().unwrap().get(&id) { | ||||
|         session.close(); | ||||
|     } | ||||
|     let _ = SESSIONS.write().unwrap().remove(&id); | ||||
| } | ||||
| 
 | ||||
| pub fn session_refresh(id: String) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user