From 4f45bcc1962bb5517349e63cd80d44248da4d898 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Wed, 22 Jan 2020 20:42:32 -0800 Subject: [PATCH] Add DI container in ScreenCast.Win. --- Agent/Program.cs | 75 +++++++++---------- Desktop.Win/ViewModels/MainWindowViewModel.cs | 2 +- ScreenCast.Core/ScreenCast.Core.csproj | 2 + ScreenCast.Win/Program.cs | 36 +++++++-- ScreenCast.Win/Services/CursorIconWatcher.cs | 4 +- ScreenCast.Win/Services/WinInput.cs | 2 - Server/Startup.cs | 2 +- 7 files changed, 72 insertions(+), 51 deletions(-) diff --git a/Agent/Program.cs b/Agent/Program.cs index 405f3ca2..9822334c 100644 --- a/Agent/Program.cs +++ b/Agent/Program.cs @@ -43,8 +43,7 @@ namespace Remotely.Agent var serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(builder => { - builder.AddConsole() - .AddEventLog(); + builder.AddConsole().AddEventLog(); }); serviceCollection.AddSingleton(); serviceCollection.AddScoped(); @@ -62,6 +61,42 @@ namespace Remotely.Agent Services = serviceCollection.BuildServiceProvider(); } + private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + Logger.Write(e.ExceptionObject as Exception); + if (OSUtils.IsWindows) + { + // Remove Secure Attention Sequence policy to allow app to simulate Ctrl + Alt + Del. + var subkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", true); + if (subkey.GetValue("SoftwareSASGeneration") != null) + { + subkey.DeleteValue("SoftwareSASGeneration"); + } + } + } + + private static async Task HandleConnection() + { + while (true) + { + try + { + if (!Services.GetRequiredService().IsConnected) + { + var waitTime = new Random().Next(1000, 30000); + Logger.Write($"Websocket closed. Reconnecting in {waitTime / 1000} seconds..."); + await Task.Delay(waitTime); + await Services.GetRequiredService().Connect(); + } + } + catch (Exception ex) + { + Logger.Write(ex); + } + Thread.Sleep(1000); + } + } + private static async void Init(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; @@ -95,42 +130,6 @@ namespace Remotely.Agent await HandleConnection(); } } - - private static async Task HandleConnection() - { - while (true) - { - try - { - if (!Services.GetRequiredService().IsConnected) - { - var waitTime = new Random().Next(1000, 30000); - Logger.Write($"Websocket closed. Reconnecting in {waitTime / 1000} seconds..."); - await Task.Delay(waitTime); - await Services.GetRequiredService().Connect(); - } - } - catch (Exception ex) - { - Logger.Write(ex); - } - Thread.Sleep(1000); - } - } - - private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) - { - Logger.Write(e.ExceptionObject as Exception); - if (OSUtils.IsWindows) - { - // Remove Secure Attention Sequence policy to allow app to simulate Ctrl + Alt + Del. - var subkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System", true); - if (subkey.GetValue("SoftwareSASGeneration") != null) - { - subkey.DeleteValue("SoftwareSASGeneration"); - } - } - } private static Dictionary ProcessArgs(string[] args) { var argDict = new Dictionary(); diff --git a/Desktop.Win/ViewModels/MainWindowViewModel.cs b/Desktop.Win/ViewModels/MainWindowViewModel.cs index 90cce8b2..99f98b7f 100644 --- a/Desktop.Win/ViewModels/MainWindowViewModel.cs +++ b/Desktop.Win/ViewModels/MainWindowViewModel.cs @@ -30,7 +30,7 @@ namespace Remotely.Desktop.Win.ViewModels { Current = this; - CursorIconWatcher = new CursorIconWatcher(Conductor); + CursorIconWatcher = new CursorIconWatcher(); CursorIconWatcher.OnChange += CursorIconWatcher_OnChange; var screenCaster = new WinScreenCaster(CursorIconWatcher); diff --git a/ScreenCast.Core/ScreenCast.Core.csproj b/ScreenCast.Core/ScreenCast.Core.csproj index a893240d..ea44bfc4 100644 --- a/ScreenCast.Core/ScreenCast.Core.csproj +++ b/ScreenCast.Core/ScreenCast.Core.csproj @@ -34,6 +34,8 @@ + + diff --git a/ScreenCast.Win/Program.cs b/ScreenCast.Win/Program.cs index 20aaf152..f61b5a38 100644 --- a/ScreenCast.Win/Program.cs +++ b/ScreenCast.Win/Program.cs @@ -11,6 +11,8 @@ using Remotely.ScreenCast.Win.Services; using Remotely.ScreenCast.Core.Interfaces; using Remotely.ScreenCast.Win.Capture; using Remotely.ScreenCast.Core.Communication; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace Remotely.ScreenCast.Win { @@ -18,6 +20,7 @@ namespace Remotely.ScreenCast.Win { public static Conductor Conductor { get; private set; } public static CursorIconWatcher CursorIconWatcher { get; private set; } + public static ServiceProvider Services { get; private set; } private static string CurrentDesktopName { get; set; } @@ -34,11 +37,12 @@ namespace Remotely.ScreenCast.Win try { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; - CursorIconWatcher = new CursorIconWatcher(Conductor); - var screenCaster = new WinScreenCaster(CursorIconWatcher); - var clipboardService = new WinClipboardService(); - var casterSocket = new CasterSocket(new WinInput(), screenCaster, new WinAudioCapturer(), clipboardService); - Conductor = new Conductor(casterSocket, screenCaster); + + BuildServices(); + + CursorIconWatcher = Services.GetRequiredService(); + + Conductor = Services.GetRequiredService(); Conductor.ProcessArgs(args); Conductor.Connect().ContinueWith(async (task) => @@ -64,7 +68,7 @@ namespace Remotely.ScreenCast.Win Conductor.IdleTimer = new IdleTimer(Conductor.Viewers); Conductor.IdleTimer.Start(); CursorIconWatcher.OnChange += CursorIconWatcher_OnChange; - clipboardService.BeginWatching(); + Services.GetRequiredService().BeginWatching(); }); Thread.Sleep(Timeout.Infinite); @@ -76,6 +80,26 @@ namespace Remotely.ScreenCast.Win } } + private static void BuildServices() + { + var serviceCollection = new ServiceCollection(); + serviceCollection.AddLogging(builder => + { + builder.AddConsole().AddEventLog(); + }); + + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); + serviceCollection.AddScoped(); + serviceCollection.AddScoped(); + serviceCollection.AddScoped(); + serviceCollection.AddScoped(); + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); + + Services = serviceCollection.BuildServiceProvider(); + } + private static async Task CheckForRelaunch() { diff --git a/ScreenCast.Win/Services/CursorIconWatcher.cs b/ScreenCast.Win/Services/CursorIconWatcher.cs index 43d2709d..99c48820 100644 --- a/ScreenCast.Win/Services/CursorIconWatcher.cs +++ b/ScreenCast.Win/Services/CursorIconWatcher.cs @@ -21,9 +21,8 @@ namespace Remotely.ScreenCast.Win.Services /// public class CursorIconWatcher { - public CursorIconWatcher(Conductor conductor) + public CursorIconWatcher() { - Conductor = conductor; ChangeTimer = new System.Timers.Timer(25); ChangeTimer.Elapsed += ChangeTimer_Elapsed; ChangeTimer.Start(); @@ -31,7 +30,6 @@ namespace Remotely.ScreenCast.Win.Services public event EventHandler OnChange; private System.Timers.Timer ChangeTimer { get; set; } private string PreviousCursorHandle { get; set; } - public Conductor Conductor { get; } private User32.CursorInfo cursorInfo; diff --git a/ScreenCast.Win/Services/WinInput.cs b/ScreenCast.Win/Services/WinInput.cs index 8a6bd5cb..d45e8eab 100644 --- a/ScreenCast.Win/Services/WinInput.cs +++ b/ScreenCast.Win/Services/WinInput.cs @@ -3,8 +3,6 @@ using Remotely.ScreenCast.Core.Models; using System; using Remotely.Shared.Win32; using static Remotely.Shared.Win32.User32; -using Remotely.ScreenCast.Core.Capture; -using Remotely.ScreenCast.Core.Interfaces; using System.Windows.Forms; namespace Remotely.ScreenCast.Win.Services diff --git a/Server/Startup.cs b/Server/Startup.cs index e8b8c01e..a734c03e 100644 --- a/Server/Startup.cs +++ b/Server/Startup.cs @@ -102,7 +102,7 @@ namespace Remotely.Server var trustedOrigins = Configuration.GetSection("ApplicationOptions:TrustedCorsOrigins").Get(); - + if (trustedOrigins != null) { services.AddCors(options =>