Skip to content

Commit

Permalink
Add unattended mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxXor committed Jun 1, 2020
1 parent 85edbef commit 053890b
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 114 deletions.
2 changes: 2 additions & 0 deletions Quasar.Client/Config/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static class Settings
public static bool HIDEINSTALLSUBDIRECTORY = false;
public static string INSTALLPATH = "";
public static string LOGSPATH = "";
public static bool UNATTENDEDMODE = true;

public static bool Initialize()
{
Expand Down Expand Up @@ -67,6 +68,7 @@ public static bool Initialize()
public static bool HIDEINSTALLSUBDIRECTORY = false;
public static string INSTALLPATH = "";
public static string LOGSPATH = "";
public static bool UNATTENDEDMODE = false;

public static bool Initialize()
{
Expand Down
10 changes: 6 additions & 4 deletions Quasar.Client/Messages/FileManagerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Quasar.Client.Messages
{
public class FileManagerHandler : IMessageProcessor, IDisposable
public class FileManagerHandler : NotificationMessageProcessor, IDisposable
{
private readonly ConcurrentDictionary<int, FileSplit> _activeTransfers = new ConcurrentDictionary<int, FileSplit>();
private readonly Semaphore _limitThreads = new Semaphore(2, 2); // maximum simultaneous file downloads
Expand Down Expand Up @@ -52,17 +52,17 @@ private void OnClientStateChange(Networking.Client s, bool connected)
}
}

public bool CanExecute(IMessage message) => message is GetDrives ||
public override bool CanExecute(IMessage message) => message is GetDrives ||
message is GetDirectory ||
message is FileTransferRequest ||
message is FileTransferCancel ||
message is FileTransferChunk ||
message is DoPathDelete ||
message is DoPathRename;

public bool CanExecuteFrom(ISender sender) => true;
public override bool CanExecuteFrom(ISender sender) => true;

public void Execute(ISender sender, IMessage message)
public override void Execute(ISender sender, IMessage message)
{
switch (message)
{
Expand Down Expand Up @@ -229,6 +229,7 @@ private void Execute(ISender client, FileTransferRequest message)
using (var srcFile = new FileSplit(message.RemotePath, FileAccess.Read))
{
_activeTransfers[message.Id] = srcFile;
OnReport("File upload started");
foreach (var chunk in srcFile)
{
if (_token.IsCancellationRequested || !_activeTransfers.ContainsKey(message.Id))
Expand Down Expand Up @@ -301,6 +302,7 @@ private void Execute(ISender client, FileTransferChunk message)
}

_activeTransfers[message.Id] = new FileSplit(filePath, FileAccess.Write);
OnReport("File download started");
}

if (!_activeTransfers.ContainsKey(message.Id))
Expand Down
11 changes: 11 additions & 0 deletions Quasar.Client/Messages/NotificationMessageProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Quasar.Common.Messages;

namespace Quasar.Client.Messages
{
public abstract class NotificationMessageProcessor : MessageProcessorBase<string>
{
protected NotificationMessageProcessor() : base(true)
{
}
}
}
18 changes: 12 additions & 6 deletions Quasar.Client/Messages/RemoteDesktopHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

namespace Quasar.Client.Messages
{
public class RemoteDesktopHandler : IMessageProcessor, IDisposable
public class RemoteDesktopHandler : NotificationMessageProcessor, IDisposable
{
private UnsafeStreamCodec _streamCodec;

public bool CanExecute(IMessage message) => message is GetDesktop ||
public override bool CanExecute(IMessage message) => message is GetDesktop ||
message is DoMouseEvent ||
message is DoKeyboardEvent ||
message is GetMonitors;

public bool CanExecuteFrom(ISender sender) => true;
public override bool CanExecuteFrom(ISender sender) => true;

public void Execute(ISender sender, IMessage message)
public override void Execute(ISender sender, IMessage message)
{
switch (message)
{
Expand Down Expand Up @@ -52,8 +52,14 @@ private void Execute(ISender client, GetDesktop message)
if (_streamCodec == null)
_streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution);

if (message.CreateNew || _streamCodec.ImageQuality != message.Quality || _streamCodec.Monitor != message.DisplayIndex
|| _streamCodec.Resolution != resolution)
if (message.CreateNew)
{
_streamCodec?.Dispose();
_streamCodec = new UnsafeStreamCodec(message.Quality, message.DisplayIndex, resolution);
OnReport("Remote desktop session started");
}

if (_streamCodec.ImageQuality != message.Quality || _streamCodec.Monitor != message.DisplayIndex || _streamCodec.Resolution != resolution)
{
_streamCodec?.Dispose();

Expand Down
75 changes: 72 additions & 3 deletions Quasar.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,89 @@
using System.Net;
using Quasar.Client.IO;
using System;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace Quasar.Client
{
internal static class Program
{
[STAThread]
private static void Main(string[] args)
{
// enable TLS 1.2
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

// Set the unhandled exception mode to force all Windows Forms errors to go through our handler
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Add the event handler for handling UI thread exceptions
Application.ThreadException += HandleThreadException;

// Add the event handler for handling non-UI thread exceptions
AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException;

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (var app = new QuasarApplication())
Application.Run(new QuasarApplication());
}

private static void HandleThreadException(object sender, ThreadExceptionEventArgs e)
{
Debug.WriteLine(e);
try
{
string batchFile = BatchFile.CreateRestartBatch(Application.ExecutablePath);

ProcessStartInfo startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
FileName = batchFile
};
Process.Start(startInfo);
}
catch (Exception exception)
{
app.Run();
Debug.WriteLine(exception);
}
finally
{
Environment.Exit(0);
}
}

/// <summary>
/// Handles unhandled exceptions by restarting the application and hoping that they don't happen again.
/// </summary>
/// <param name="sender">The source of the unhandled exception event.</param>
/// <param name="e">The exception event arguments. </param>
private static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.IsTerminating)
{
Debug.WriteLine(e);
try
{
string batchFile = BatchFile.CreateRestartBatch(Application.ExecutablePath);

ProcessStartInfo startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true,
FileName = batchFile
};
Process.Start(startInfo);
}
catch (Exception exception)
{
Debug.WriteLine(exception);
}
finally
{
Environment.Exit(0);
}
}
}
}
Expand Down
Loading

0 comments on commit 053890b

Please sign in to comment.