From a767d2486fc05779482e4776f1c9df3597d2c2f7 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Sat, 6 Jul 2019 16:50:29 -0700 Subject: [PATCH] Extracted idle timer into separate class. Rename namespace. --- .../ViewModels/MainWindowViewModel.cs | 2 +- Desktop.Win/App.xaml.cs | 2 +- Desktop.Win/ViewModels/MainWindowViewModel.cs | 2 +- ScreenCast.Core/Capture/ScreenCaster.cs | 2 +- ScreenCast.Core/Conductor.cs | 31 ++++---------- ScreenCast.Core/Services/IdleTimer.cs | 42 +++++++++++++++++++ .../{Utilities => Services}/Logger.cs | 2 +- ScreenCast.Core/Sockets/CasterSocket.cs | 2 +- ScreenCast.Linux/Capture/X11Capture.cs | 2 +- ScreenCast.Linux/Input/X11Input.cs | 2 +- ScreenCast.Linux/Program.cs | 5 ++- ScreenCast.Win/Capture/BitBltCapture.cs | 2 +- ScreenCast.Win/Capture/DXCapture.cs | 2 +- ScreenCast.Win/Program.cs | 5 ++- 14 files changed, 65 insertions(+), 38 deletions(-) create mode 100644 ScreenCast.Core/Services/IdleTimer.cs rename ScreenCast.Core/{Utilities => Services}/Logger.cs (98%) diff --git a/Desktop.Unix/ViewModels/MainWindowViewModel.cs b/Desktop.Unix/ViewModels/MainWindowViewModel.cs index 77c4d9f2..e9460b2a 100644 --- a/Desktop.Unix/ViewModels/MainWindowViewModel.cs +++ b/Desktop.Unix/ViewModels/MainWindowViewModel.cs @@ -7,7 +7,7 @@ using Remotely.Desktop.Unix.Services; using Remotely.ScreenCast.Core; using Remotely.ScreenCast.Core.Capture; using Remotely.ScreenCast.Core.Models; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using Remotely.ScreenCast.Linux.Capture; using Remotely.ScreenCast.Linux.Input; using Remotely.Shared.Models; diff --git a/Desktop.Win/App.xaml.cs b/Desktop.Win/App.xaml.cs index 89dfe34e..1211be3e 100644 --- a/Desktop.Win/App.xaml.cs +++ b/Desktop.Win/App.xaml.cs @@ -1,6 +1,6 @@ using Remotely.Desktop.Win.Services; using Remotely.Desktop.Win.ViewModels; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using System; using System.Collections.Generic; using System.Configuration; diff --git a/Desktop.Win/ViewModels/MainWindowViewModel.cs b/Desktop.Win/ViewModels/MainWindowViewModel.cs index c2024cb2..6a143988 100644 --- a/Desktop.Win/ViewModels/MainWindowViewModel.cs +++ b/Desktop.Win/ViewModels/MainWindowViewModel.cs @@ -4,7 +4,7 @@ using Remotely.Shared.Models; using Remotely.ScreenCast.Core; using Remotely.ScreenCast.Core.Capture; using Remotely.ScreenCast.Core.Models; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using Remotely.ScreenCast.Win; using Remotely.ScreenCast.Win.Capture; using Remotely.ScreenCast.Win.Input; diff --git a/ScreenCast.Core/Capture/ScreenCaster.cs b/ScreenCast.Core/Capture/ScreenCaster.cs index 4b8701b2..f6ee7757 100644 --- a/ScreenCast.Core/Capture/ScreenCaster.cs +++ b/ScreenCast.Core/Capture/ScreenCaster.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.SignalR.Client; using Remotely.ScreenCast.Core.Models; using Remotely.ScreenCast.Core.Sockets; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using System; using System.Collections.Generic; using System.Drawing; diff --git a/ScreenCast.Core/Conductor.cs b/ScreenCast.Core/Conductor.cs index 5d23f20c..53805db5 100644 --- a/ScreenCast.Core/Conductor.cs +++ b/ScreenCast.Core/Conductor.cs @@ -5,7 +5,7 @@ using Remotely.ScreenCast.Core.Enums; using Remotely.ScreenCast.Core.Input; using Remotely.ScreenCast.Core.Models; using Remotely.ScreenCast.Core.Sockets; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using System; using System.Collections.Concurrent; using System.Collections.Generic; @@ -35,12 +35,12 @@ namespace Remotely.ScreenCast.Core public HubConnection Connection { get; private set; } public string CurrentDesktopName { get; set; } public string Host { get; private set; } + public IdleTimer IdleTimer { get; set; } + public bool IsSwitchingDesktops { get; set; } public AppMode Mode { get; private set; } public string RequesterID { get; private set; } public string ServiceID { get; private set; } public ConcurrentDictionary Viewers { get; } = new ConcurrentDictionary(); - public bool IsSwitchingDesktops { get; set; } - public Task Connect() { Connection = new HubConnectionBuilder() @@ -86,37 +86,20 @@ namespace Remotely.ScreenCast.Core CasterSocket = new CasterSocket(Connection, this, keyboardMouse); } - public void StartWaitForViewerTimer() - { - var timer = new System.Timers.Timer(10000); - timer.AutoReset = false; - timer.Elapsed += (sender, arg) => - { - // Shut down if no viewers have connected within 10 seconds. - if (Viewers.Count == 0) - { - Logger.Write("No viewers connected after 10 seconds. Shutting down."); - Environment.Exit(0); - } - }; - timer.Start(); - } - internal void InvokeAudioToggled(bool toggleOn) { AudioToggled?.Invoke(null, toggleOn); } - internal void InvokeScreenCastInitiated(ScreenCastRequest viewerIdAndRequesterName) - { - ScreenCastInitiated?.Invoke(null, viewerIdAndRequesterName); - } - internal void InvokeClipboardTransfer(string transferText) { ClipboardTransferred?.Invoke(null, transferText); } + internal void InvokeScreenCastInitiated(ScreenCastRequest viewerIdAndRequesterName) + { + ScreenCastInitiated?.Invoke(null, viewerIdAndRequesterName); + } internal void InvokeScreenCastRequested(ScreenCastRequest viewerIdAndRequesterName) { ScreenCastRequested?.Invoke(null, viewerIdAndRequesterName); diff --git a/ScreenCast.Core/Services/IdleTimer.cs b/ScreenCast.Core/Services/IdleTimer.cs new file mode 100644 index 00000000..3ec11c14 --- /dev/null +++ b/ScreenCast.Core/Services/IdleTimer.cs @@ -0,0 +1,42 @@ +using Remotely.ScreenCast.Core.Models; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Text; +using System.Timers; + +namespace Remotely.ScreenCast.Core.Services +{ + public class IdleTimer + { + public IdleTimer(ConcurrentDictionary viewerList) + { + ViewerList = viewerList; + Timer.Elapsed += Timer_Elapsed; + } + + public ConcurrentDictionary ViewerList { get; } + + public DateTime ViewersLastSeen { get; private set; } = DateTime.Now; + + private Timer Timer { get; } = new Timer(100); + + public void Start() + { + Timer.Start(); + } + + private void Timer_Elapsed(object sender, ElapsedEventArgs e) + { + if (ViewerList.Count > 0) + { + ViewersLastSeen = DateTime.Now; + } + else if (DateTime.Now - ViewersLastSeen > TimeSpan.FromSeconds(10)) + { + Logger.Write("No viewers connected after 10 seconds. Shutting down."); + Environment.Exit(0); + } + } + } +} diff --git a/ScreenCast.Core/Utilities/Logger.cs b/ScreenCast.Core/Services/Logger.cs similarity index 98% rename from ScreenCast.Core/Utilities/Logger.cs rename to ScreenCast.Core/Services/Logger.cs index 212b6d58..0c17f4ee 100644 --- a/ScreenCast.Core/Utilities/Logger.cs +++ b/ScreenCast.Core/Services/Logger.cs @@ -8,7 +8,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; -namespace Remotely.ScreenCast.Core.Utilities +namespace Remotely.ScreenCast.Core.Services { public static class Logger { diff --git a/ScreenCast.Core/Sockets/CasterSocket.cs b/ScreenCast.Core/Sockets/CasterSocket.cs index a3507978..78c8dc30 100644 --- a/ScreenCast.Core/Sockets/CasterSocket.cs +++ b/ScreenCast.Core/Sockets/CasterSocket.cs @@ -11,7 +11,7 @@ using Remotely.ScreenCast.Core.Capture; using System.Diagnostics; using System.IO; using System.Net; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using Remotely.ScreenCast.Core.Input; using Remotely.Shared.Win32; diff --git a/ScreenCast.Linux/Capture/X11Capture.cs b/ScreenCast.Linux/Capture/X11Capture.cs index ca71d935..ebb90a36 100644 --- a/ScreenCast.Linux/Capture/X11Capture.cs +++ b/ScreenCast.Linux/Capture/X11Capture.cs @@ -1,5 +1,5 @@ using Remotely.ScreenCast.Core.Capture; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using Remotely.ScreenCast.Linux.X11Interop; using System; using System.Collections.Generic; diff --git a/ScreenCast.Linux/Input/X11Input.cs b/ScreenCast.Linux/Input/X11Input.cs index 7a27d373..d99d4f43 100644 --- a/ScreenCast.Linux/Input/X11Input.cs +++ b/ScreenCast.Linux/Input/X11Input.cs @@ -1,5 +1,5 @@ using Remotely.ScreenCast.Core.Input; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using Remotely.ScreenCast.Linux.X11Interop; using Remotely.ScreenCast.Core; using System; diff --git a/ScreenCast.Linux/Program.cs b/ScreenCast.Linux/Program.cs index b4f7ea98..1d5efe19 100644 --- a/ScreenCast.Linux/Program.cs +++ b/ScreenCast.Linux/Program.cs @@ -1,7 +1,7 @@ using Remotely.Shared.Models; using Remotely.ScreenCast.Core; using Remotely.ScreenCast.Core.Capture; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using Remotely.ScreenCast.Linux.Capture; using Remotely.ScreenCast.Linux.Input; using Remotely.ScreenCast.Linux.X11Interop; @@ -30,7 +30,8 @@ namespace Remotely.ScreenCast.Linux Conductor.ClipboardTransferred += Conductor_ClipboardTransferred; Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName).Wait(); Conductor.CasterSocket.NotifyRequesterUnattendedReady(Conductor.RequesterID).Wait(); - Conductor.StartWaitForViewerTimer(); + Conductor.IdleTimer = new IdleTimer(Conductor.Viewers); + Conductor.IdleTimer.Start(); while (true) { System.Threading.Thread.Sleep(100); diff --git a/ScreenCast.Win/Capture/BitBltCapture.cs b/ScreenCast.Win/Capture/BitBltCapture.cs index 171324d8..d308a442 100644 --- a/ScreenCast.Win/Capture/BitBltCapture.cs +++ b/ScreenCast.Win/Capture/BitBltCapture.cs @@ -11,7 +11,7 @@ using System.Collections.Generic; using System.Drawing.Drawing2D; using System.Diagnostics; using System.Runtime.Serialization.Formatters.Binary; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using System.Threading; using Remotely.ScreenCast.Core.Capture; diff --git a/ScreenCast.Win/Capture/DXCapture.cs b/ScreenCast.Win/Capture/DXCapture.cs index 8ae2aa09..b46a9155 100644 --- a/ScreenCast.Win/Capture/DXCapture.cs +++ b/ScreenCast.Win/Capture/DXCapture.cs @@ -1,5 +1,5 @@ using Remotely.ScreenCast.Core.Capture; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using SharpDX; using SharpDX.Direct3D11; using SharpDX.DXGI; diff --git a/ScreenCast.Win/Program.cs b/ScreenCast.Win/Program.cs index 1af1954d..1bca5498 100644 --- a/ScreenCast.Win/Program.cs +++ b/ScreenCast.Win/Program.cs @@ -6,7 +6,7 @@ using Remotely.ScreenCast.Core.Capture; using Remotely.ScreenCast.Core.Enums; using Remotely.ScreenCast.Core.Models; using Remotely.ScreenCast.Core.Sockets; -using Remotely.ScreenCast.Core.Utilities; +using Remotely.ScreenCast.Core.Services; using Remotely.ScreenCast.Win.Capture; using Remotely.ScreenCast.Win.Input; using System; @@ -85,7 +85,8 @@ namespace Remotely.ScreenCast.Win Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName).Wait(); CheckInitialDesktop(); CheckForRelaunch(); - Conductor.StartWaitForViewerTimer(); + Conductor.IdleTimer = new IdleTimer(Conductor.Viewers); + Conductor.IdleTimer.Start(); HandleConnection(Conductor).Wait(); } catch (Exception ex)