Add DI container in ScreenCast.Win.

This commit is contained in:
Jared Goodwin 2020-01-22 20:42:32 -08:00
parent f607d5b760
commit 4f45bcc196
7 changed files with 72 additions and 51 deletions

View File

@ -43,8 +43,7 @@ namespace Remotely.Agent
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging(builder =>
{
builder.AddConsole()
.AddEventLog();
builder.AddConsole().AddEventLog();
});
serviceCollection.AddSingleton<DeviceSocket>();
serviceCollection.AddScoped<Bash>();
@ -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<DeviceSocket>().IsConnected)
{
var waitTime = new Random().Next(1000, 30000);
Logger.Write($"Websocket closed. Reconnecting in {waitTime / 1000} seconds...");
await Task.Delay(waitTime);
await Services.GetRequiredService<DeviceSocket>().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<DeviceSocket>().IsConnected)
{
var waitTime = new Random().Next(1000, 30000);
Logger.Write($"Websocket closed. Reconnecting in {waitTime / 1000} seconds...");
await Task.Delay(waitTime);
await Services.GetRequiredService<DeviceSocket>().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<string,string> ProcessArgs(string[] args)
{
var argDict = new Dictionary<string, string>();

View File

@ -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);

View File

@ -34,6 +34,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.1.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.EventLog" Version="3.1.1" />
<PackageReference Include="Microsoft.MixedReality.WebRTC" Version="1.0.2" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
</ItemGroup>

View File

@ -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<CursorIconWatcher>();
Conductor = Services.GetRequiredService<Conductor>();
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<IClipboardService>().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<Conductor>();
serviceCollection.AddSingleton<CursorIconWatcher>();
serviceCollection.AddScoped<IScreenCaster, WinScreenCaster>();
serviceCollection.AddScoped<IKeyboardMouseInput, WinInput>();
serviceCollection.AddScoped<IClipboardService, WinClipboardService>();
serviceCollection.AddScoped<IAudioCapturer, WinAudioCapturer>();
serviceCollection.AddSingleton<CasterSocket>();
serviceCollection.AddSingleton<Conductor>();
Services = serviceCollection.BuildServiceProvider();
}
private static async Task CheckForRelaunch()
{

View File

@ -21,9 +21,8 @@ namespace Remotely.ScreenCast.Win.Services
/// </summary>
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<CursorInfo> OnChange;
private System.Timers.Timer ChangeTimer { get; set; }
private string PreviousCursorHandle { get; set; }
public Conductor Conductor { get; }
private User32.CursorInfo cursorInfo;

View File

@ -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

View File

@ -102,7 +102,7 @@ namespace Remotely.Server
var trustedOrigins = Configuration.GetSection("ApplicationOptions:TrustedCorsOrigins").Get<string[]>();
if (trustedOrigins != null)
{
services.AddCors(options =>