From 5774f40b650a376e9b622dba9df6c43589b0d398 Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Thu, 9 Apr 2020 00:29:06 +0300 Subject: Logs, Comments & General organization on FSE/PPC. Several improvements. --- .../RemoteDesktop/DefaultRemoteDesktopService.cs | 91 ++++++++++++---------- 1 file changed, 48 insertions(+), 43 deletions(-) (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs index 849befc27..7ba020174 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/RemoteDesktop/DefaultRemoteDesktopService.cs @@ -10,6 +10,7 @@ using System.Windows.Input; using Tango.Core; using Tango.Core.DI; using Tango.Integration.ExternalBridge; +using Tango.Logging; using Tango.PPC.Common.Application; using Tango.PPC.Common.ExternalBridge; using Tango.PPC.Common.OS; @@ -30,15 +31,6 @@ namespace Tango.PPC.Common.RemoteDesktop [TangoCreateWhenRegistered] public class DefaultRemoteDesktopService : ExtendedObject, IRemoteDesktopService, IExternalBridgeRequestHandler { - private class RemoteDesktopClient - { - public String Token { get; set; } - public ExternalBridgeReceiver Receiver { get; set; } - public bool InitialPacketSent { get; set; } - public WebRtcClient WebRtcClient { get; set; } - public bool IsWebRtcReady { get; set; } - } - private RemoteDesktopPacket _initialPacket; private RasterScreenCaptureEngine _engine; private PPCSettings _settings; @@ -47,14 +39,24 @@ namespace Tango.PPC.Common.RemoteDesktop private IOperationSystemManager _osManager; private IPPCApplicationManager _appManager; + /// + /// Gets or sets a value indicating whether this is enabled. + /// + public bool Enabled { get; set; } = true; private bool _isStarted; + /// + /// Gets a value indicating whether the remote desktop service has started. + /// public bool IsStarted { get { return _isStarted; } private set { _isStarted = value; RaisePropertyChangedAuto(); } } + /// + /// Gets a value indicating whether there is any active remote desktop session with a remote peer. + /// public bool InSession { get @@ -63,6 +65,12 @@ namespace Tango.PPC.Common.RemoteDesktop } } + /// + /// Initializes a new instance of the class. + /// + /// The application manager. + /// The external bridge. + /// The os manager. public DefaultRemoteDesktopService(IPPCApplicationManager applicationManager, IPPCExternalBridgeService externalBridge, IOperationSystemManager osManager) { _osManager = osManager; @@ -74,6 +82,8 @@ namespace Tango.PPC.Common.RemoteDesktop }; _settings = SettingsManager.Default.GetOrCreate(); + Enabled = _settings.EnableRemoteDesktop; + applicationManager.ApplicationReady += ApplicationManager_ApplicationReady; externalBridge.RegisterRequestHandler(this); @@ -118,14 +128,10 @@ namespace Tango.PPC.Common.RemoteDesktop _engine.Comparer.MaxDifferencesThrow = _engine.CaptureRegion.Width * _engine.CaptureRegion.Height / 2; } - [ExternalBridgeRequestHandlerMethod(typeof(StartRemoteDesktopSessionRequest))] - public async void OnStartRemoteDesktopSessionRequestReceived(StartRemoteDesktopSessionRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(StartRemoteDesktopSessionRequest), RequestHandlerLoggingMode.LogRequestName)] + public async Task OnStartRemoteDesktopSessionRequestReceived(StartRemoteDesktopSessionRequest request, String token, ExternalBridgeReceiver receiver) { - if (!_settings.EnableRemoteDesktop) - { - await receiver.SendErrorResponse(new AuthenticationException("Remote desktop is disabled on this machine."), token); - return; - } + this.ThrowIfDisabled(); var client = _clients.SingleOrDefault(x => x.Receiver == receiver); @@ -146,19 +152,17 @@ namespace Tango.PPC.Common.RemoteDesktop FrameRate = _engine.FrameRate }, token); - if (_settings.EnableRemoteDesktop) + + if (!_engine.IsStarted) { - if (!_engine.IsStarted) - { - _engine.Start(); - } + _engine.Start(); } RaisePropertyChanged(nameof(InSession)); } - [ExternalBridgeRequestHandlerMethod(typeof(WebRtcIceCandidateRequest))] - public async void OnWebRtcIceCandidateRequestReceived(WebRtcIceCandidateRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(WebRtcIceCandidateRequest), RequestHandlerLoggingMode.LogRequestName)] + public async Task OnWebRtcIceCandidateRequestReceived(WebRtcIceCandidateRequest request, String token, ExternalBridgeReceiver receiver) { var client = _clients.SingleOrDefault(x => x.Receiver == receiver); @@ -171,13 +175,13 @@ namespace Tango.PPC.Common.RemoteDesktop } catch (Exception ex) { - LogManager.Log($"Error adding WebRTC ice candidate received from the remote connection.\n{ex.FlattenMessage()}"); + LogManager.Log(ex, "Error adding WebRTC ice candidate received from the remote connection."); } } } - [ExternalBridgeRequestHandlerMethod(typeof(WebRtcOfferRequest))] - public async void OnWebRtcOfferRequestReceived(WebRtcOfferRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(WebRtcOfferRequest), RequestHandlerLoggingMode.LogRequestName)] + public async Task OnWebRtcOfferRequestReceived(WebRtcOfferRequest request, String token, ExternalBridgeReceiver receiver) { var client = _clients.SingleOrDefault(x => x.Receiver == receiver); @@ -196,7 +200,7 @@ namespace Tango.PPC.Common.RemoteDesktop } catch (Exception ex) { - LogManager.Log(ex, $"Error sending ice candidate to remote peer.\n{ex.FlattenMessage()}"); + LogManager.Log(ex, "Error sending ice candidate to remote peer."); } }; client.WebRtcClient.Ready += (x, e) => @@ -219,18 +223,18 @@ namespace Tango.PPC.Common.RemoteDesktop } catch (Exception ex) { - LogManager.Log($"Error initializing the web RTC client.\n{ex.FlattenMessage()}"); + throw LogManager.Log(ex, "Error initializing the WebRTC client."); } } catch (Exception ex) { - LogManager.Log($"Error responding to WebRTC offer request.\n{ex.FlattenMessage()}"); + throw LogManager.Log(ex, "Error responding to WebRTC offer request."); } } } - [ExternalBridgeRequestHandlerMethod(typeof(StopRemoteDesktopSessionRequest))] - public async void OnStopRemoteDesktopSessionRequestReceived(StopRemoteDesktopSessionRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(StopRemoteDesktopSessionRequest), RequestHandlerLoggingMode.LogRequestName)] + public async Task OnStopRemoteDesktopSessionRequestReceived(StopRemoteDesktopSessionRequest request, String token, ExternalBridgeReceiver receiver) { var client = _clients.SingleOrDefault(x => x.Receiver == receiver); @@ -260,7 +264,7 @@ namespace Tango.PPC.Common.RemoteDesktop } [ExternalBridgeRequestHandlerMethod(typeof(MouseStateRequest))] - public async void OnMouseStateRequestReceived(MouseStateRequest request, String token, ExternalBridgeReceiver receiver) + public async Task OnMouseStateRequestReceived(MouseStateRequest request, String token, ExternalBridgeReceiver receiver) { MouseController.SetCursorPosition((int)request.Location.X, (int)request.Location.Y); @@ -292,7 +296,7 @@ namespace Tango.PPC.Common.RemoteDesktop } [ExternalBridgeRequestHandlerMethod(typeof(KeyboardStateRequest))] - public async void OnKeyboardStateRequestReceived(KeyboardStateRequest request, String token, ExternalBridgeReceiver receiver) + public async Task OnKeyboardStateRequestReceived(KeyboardStateRequest request, String token, ExternalBridgeReceiver receiver) { if (request.EventType == KeyboardEventType.Down) { @@ -309,8 +313,8 @@ namespace Tango.PPC.Common.RemoteDesktop } } - [ExternalBridgeRequestHandlerMethod(typeof(RemoteDesktopCommandRequest))] - public async void OnRemoteDesktopCommandRequest(RemoteDesktopCommandRequest request, String token, ExternalBridgeReceiver receiver) + [ExternalBridgeRequestHandlerMethod(typeof(RemoteDesktopCommandRequest), RequestHandlerLoggingMode.LogRequestName)] + public async Task OnRemoteDesktopCommandRequest(RemoteDesktopCommandRequest request, String token, ExternalBridgeReceiver receiver) { switch (request.Command) { @@ -346,7 +350,7 @@ namespace Tango.PPC.Common.RemoteDesktop } catch (Exception ex) { - Debug.WriteLine(ex); + LogManager.Log(ex, LogCategory.Debug, "Error pushing remote desktop frame via WebRTC channel."); } } @@ -382,7 +386,6 @@ namespace Tango.PPC.Common.RemoteDesktop if (!e.Frame.DifferenceAvailable) { - Debug.WriteLine("Using Jpeg..."); packet = new RemoteDesktopPacket() { Bitmap = e.Frame.ToEncoder().ToArray(30) @@ -403,7 +406,7 @@ namespace Tango.PPC.Common.RemoteDesktop diffFrame.Dispose(); } - Debug.WriteLine($"Bitmap Size: {packet.Bitmap.Length / 1000} kb"); + Debug.WriteLine($"Remote Desktop Bitmap Size: {packet.Bitmap.Length / 1000} kb"); foreach (var client in _clients.ToList().Where(x => x.InitialPacketSent)) { @@ -429,7 +432,7 @@ namespace Tango.PPC.Common.RemoteDesktop e.Frame.Dispose(); } - private void WebRtcClient_TextMessageReceived(object sender, DataMessageReceivedEventArgs e) + private async void WebRtcClient_TextMessageReceived(object sender, DataMessageReceivedEventArgs e) { try { @@ -437,16 +440,16 @@ namespace Tango.PPC.Common.RemoteDesktop if (request.GetType() == typeof(MouseStateRequest)) { - OnMouseStateRequestReceived(request as MouseStateRequest, null, null); + await OnMouseStateRequestReceived(request as MouseStateRequest, null, null); } else if (request.GetType() == typeof(KeyboardStateRequest)) { - OnKeyboardStateRequestReceived(request as KeyboardStateRequest, null, null); + await OnKeyboardStateRequestReceived(request as KeyboardStateRequest, null, null); } } catch (Exception ex) { - LogManager.Log(ex, "Error deserializing incoming from message on the WebRTC data Channel."); + LogManager.Log(ex, "Error deserializing incoming message on the WebRTC data Channel."); } } @@ -456,6 +459,8 @@ namespace Tango.PPC.Common.RemoteDesktop if (client != null) { + LogManager.Log("Remote desktop client disconnected. Disposing WebRTC channel..."); + _clients.Remove(client); try @@ -467,7 +472,7 @@ namespace Tango.PPC.Common.RemoteDesktop } catch (Exception ex) { - LogManager.Log($"Error disposing the WebRTC client.\n{ex.FlattenMessage()}"); + LogManager.Log(ex, "Error disposing the WebRTC channel."); } } -- cgit v1.3.1