Refactored unattended init and connection code.

This commit is contained in:
Jared Goodwin 2019-09-18 17:43:43 -07:00
parent 080fb69726
commit ba55347b60
10 changed files with 91 additions and 98 deletions

View File

@ -16,44 +16,15 @@ namespace Remotely.Agent
public class Program
{
public static bool IsDebug { get; set; }
public static void Main(string[] args)
{
try
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
#if DEBUG
IsDebug = true;
#endif
Task.Run(()=> { Init(args); });
SetWorkingDirectory();
var argDict = ProcessArgs(args);
JsonConvert.DefaultSettings = () =>
{
var settings = new JsonSerializerSettings();
settings.Error = (sender, arg) =>
{
arg.ErrorContext.Handled = true;
};
return settings;
};
if (argDict.ContainsKey("update"))
{
Updater.CoreUpdate();
}
if (!IsDebug && OSUtils.IsWindows)
{
Task.Run(() =>
{
ServiceBase.Run(new WindowsService());
});
}
HandleConnection();
Thread.Sleep(Timeout.Infinite);
}
catch (Exception ex)
@ -62,7 +33,52 @@ namespace Remotely.Agent
}
}
private static void HandleConnection()
private static async void Init(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
#if DEBUG
IsDebug = true;
#endif
SetWorkingDirectory();
var argDict = ProcessArgs(args);
JsonConvert.DefaultSettings = () =>
{
var settings = new JsonSerializerSettings();
settings.Error = (sender, arg) =>
{
arg.ErrorContext.Handled = true;
};
return settings;
};
if (argDict.ContainsKey("update"))
{
Updater.CoreUpdate();
}
if (!IsDebug && OSUtils.IsWindows)
{
_ = Task.Run(() =>
{
ServiceBase.Run(new WindowsService());
});
}
try
{
await DeviceSocket.Connect();
}
finally
{
await HandleConnection();
}
}
private static async Task HandleConnection()
{
while (true)
{
@ -72,16 +88,15 @@ namespace Remotely.Agent
{
var waitTime = new Random().Next(1000, 30000);
Logger.Write($"Websocket closed. Reconnecting in {waitTime / 1000} seconds...");
Task.Delay(waitTime).Wait();
DeviceSocket.Connect().Wait();
await Task.Delay(waitTime);
await DeviceSocket.Connect();
}
}
catch (Exception ex)
{
Logger.Write(ex);
HandleConnection();
}
Thread.Sleep(1000);
Thread.Sleep(100);
}
}

View File

@ -62,7 +62,6 @@ namespace Remotely.ScreenCast.Core.Capture
await conductor.CasterSocket.SendScreenSize(bounds.Width, bounds.Height, viewerID);
};
// TODO: SetThreadDesktop causes issues with input after switching.
var desktopName = string.Empty;
if (OSUtils.IsWindows)
{
@ -73,7 +72,6 @@ namespace Remotely.ScreenCast.Core.Capture
{
try
{
// TODO: SetThreadDesktop causes issues with input after switching.
var currentDesktopName = Win32Interop.GetCurrentDesktop();
if (desktopName.ToLower() != currentDesktopName.ToLower())
{

View File

@ -37,19 +37,18 @@ namespace Remotely.ScreenCast.Core
public string DeviceID { get; private 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<string, Viewer> Viewers { get; } = new ConcurrentDictionary<string, Viewer>();
public Task Connect()
public async Task Connect()
{
Connection = new HubConnectionBuilder()
.WithUrl($"{Host}/RCDeviceHub")
.AddMessagePackProtocol()
.Build();
return Connection.StartAsync();
await Connection.StartAsync();
}
public void ProcessArgs(string[] args)

View File

@ -25,18 +25,18 @@ namespace Remotely.ScreenCast.Linux
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Conductor = new Conductor();
Conductor.ProcessArgs(args);
Conductor.Connect().Wait();
Conductor.SetMessageHandlers(new X11Input());
Conductor.ScreenCastInitiated += ScreenCastInitiated;
Conductor.ClipboardTransferred += Conductor_ClipboardTransferred;
Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID).Wait();
Conductor.CasterSocket.NotifyRequesterUnattendedReady(Conductor.RequesterID).Wait();
Conductor.IdleTimer = new IdleTimer(Conductor.Viewers);
Conductor.IdleTimer.Start();
while (true)
Conductor.Connect().ContinueWith(async (task) =>
{
Thread.Sleep(1000);
}
Conductor.SetMessageHandlers(new X11Input());
Conductor.ScreenCastInitiated += ScreenCastInitiated;
Conductor.ClipboardTransferred += Conductor_ClipboardTransferred;
await Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await Conductor.CasterSocket.NotifyRequesterUnattendedReady(Conductor.RequesterID);
Conductor.IdleTimer = new IdleTimer(Conductor.Viewers);
Conductor.IdleTimer.Start();
});
Thread.Sleep(Timeout.Infinite);
}
catch (Exception ex)
{

View File

@ -48,20 +48,25 @@ namespace Remotely.ScreenCast.Win
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Conductor = new Conductor();
Conductor.ProcessArgs(args);
Conductor.Connect().Wait();
Conductor.SetMessageHandlers(new WinInput());
Conductor.ScreenCastInitiated += ScreenCastInitiated;
Conductor.AudioToggled += AudioToggled;
Conductor.ClipboardTransferred += Conductor_ClipboardTransferred;
AudioCapturer = new AudioCapturer(Conductor);
CursorIconWatcher = new CursorIconWatcher(Conductor);
CursorIconWatcher.OnChange += CursorIconWatcher_OnChange;
Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID).Wait();
CheckInitialDesktop();
CheckForRelaunch();
Conductor.IdleTimer = new IdleTimer(Conductor.Viewers);
Conductor.IdleTimer.Start();
HandleConnection(Conductor);
Conductor.Connect().ContinueWith(async (task) =>
{
Conductor.SetMessageHandlers(new WinInput());
Conductor.ScreenCastInitiated += ScreenCastInitiated;
Conductor.AudioToggled += AudioToggled;
Conductor.ClipboardTransferred += Conductor_ClipboardTransferred;
AudioCapturer = new AudioCapturer(Conductor);
CursorIconWatcher = new CursorIconWatcher(Conductor);
CursorIconWatcher.OnChange += CursorIconWatcher_OnChange;
await Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
CheckInitialDesktop();
await CheckForRelaunch();
Conductor.IdleTimer = new IdleTimer(Conductor.Viewers);
Conductor.IdleTimer.Start();
await HandleConnection(Conductor);
});
Thread.Sleep(Timeout.Infinite);
}
catch (Exception ex)
{
@ -82,7 +87,7 @@ namespace Remotely.ScreenCast.Win
}
}
private static void CheckForRelaunch()
private static async Task CheckForRelaunch()
{
if (Conductor.ArgDict.ContainsKey("relaunch"))
@ -90,11 +95,11 @@ namespace Remotely.ScreenCast.Win
Logger.Write($"Resuming after relaunch in desktop {Conductor.CurrentDesktopName}.");
var viewersString = Conductor.ArgDict["viewers"];
var viewerIDs = viewersString.Split(",".ToCharArray());
Conductor.CasterSocket.NotifyViewersRelaunchedScreenCasterReady(viewerIDs).Wait();
await Conductor.CasterSocket.NotifyViewersRelaunchedScreenCasterReady(viewerIDs);
}
else
{
Conductor.CasterSocket.NotifyRequesterUnattendedReady(Conductor.RequesterID).Wait();
await Conductor.CasterSocket.NotifyRequesterUnattendedReady(Conductor.RequesterID);
}
}
@ -135,7 +140,7 @@ namespace Remotely.ScreenCast.Win
Logger.Write((Exception)e.ExceptionObject);
}
private static void HandleConnection(Conductor conductor)
private static async Task HandleConnection(Conductor conductor)
{
while (true)
{
@ -144,19 +149,9 @@ namespace Remotely.ScreenCast.Win
{
conductor.CurrentDesktopName = desktopName;
Logger.Write($"Switching desktops to {desktopName}.");
// TODO: SetThreadDesktop causes issues with input after switching.
Win32Interop.SwitchToInputDesktop();
//conductor.IsSwitchingDesktops = true;
//conductor.Connection.InvokeAsync("SwitchingDesktops", conductor.Viewers.Keys.ToList()).Wait();
//var result = Win32Interop.OpenInteractiveProcess(Assembly.GetExecutingAssembly().Location + $" -mode {conductor.Mode.ToString()} -requester {conductor.RequesterID} -serviceid {conductor.ServiceID} -deviceid {conductor.DeviceID} -host {conductor.Host} -relaunch true -desktop {desktopName} -viewers {String.Join(",", conductor.Viewers.Keys.ToList())}", desktopName, true, out _);
//if (!result)
//{
// Logger.Write($"Desktop switch to {desktopName} failed.");
// conductor.CasterSocket.SendConnectionFailedToViewers(conductor.Viewers.Keys.ToList()).Wait();
//}
//conductor.Viewers.Clear();
}
Thread.Sleep(1000);
await Task.Delay(1000);
}
}
private static async void ScreenCastInitiated(object sender, ScreenCastRequest screenCastRequest)

View File

@ -1 +1 @@
2019.09.13.1515
2019.09.18.1743

View File

@ -213,14 +213,6 @@ namespace Remotely.Server.Services
await RCBrowserHub.Clients.Clients(viewerID).SendAsync("ViewerRemoved");
}
public async Task SwitchingDesktops(string[] viewerIDs)
{
lock (ViewerList)
{
ViewerList.Clear();
}
await RCBrowserHub.Clients.Clients(viewerIDs).SendAsync("SwitchingDesktops");
}
public void ViewerDisconnected(string viewerID)
{

View File

@ -161,9 +161,6 @@ export class RCBrowserSockets {
this.Connection.stop();
this.Connect();
});
hubConnection.on("SwitchingDesktops", () => {
UI.ShowMessage("Switching desktops...");
});
hubConnection.on("Reconnecting", () => {
UI.ShowMessage("Reconnecting...");
});

File diff suppressed because one or more lines are too long

View File

@ -169,9 +169,6 @@ export class RCBrowserSockets {
this.Connect();
});
hubConnection.on("SwitchingDesktops", () => {
UI.ShowMessage("Switching desktops...");
});
hubConnection.on("Reconnecting", () => {
UI.ShowMessage("Reconnecting...");
});