From b59d2727f3ef4bdbf71ddb7f0ed5777f495aa2e3 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 19 Mar 2021 18:40:50 +0000 Subject: [PATCH] Add restart toggler on client. --- MelonVPNClient/MainWindow.cs | 43 ++++++++++++++++++++++++++++-- MelonVPNCore/DaemonSocketServer.cs | 4 +-- MelonVPNCore/GUISocketServer.cs | 9 +++---- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/MelonVPNClient/MainWindow.cs b/MelonVPNClient/MainWindow.cs index 89adad6..188b4a4 100644 --- a/MelonVPNClient/MainWindow.cs +++ b/MelonVPNClient/MainWindow.cs @@ -21,8 +21,10 @@ public partial class MainWindow : Window private ThreadWrapper wrapper; private Indicator trayIcon; private PopupMenu trayMenu; + private Button restartToggleBtn; private bool ConnectedToVPN; private bool IsHidden; + private bool RestartMode = false; public static readonly string MelonIconImg = "MiniMelonVPNIcon.png"; public static readonly string MelonOnlineImg = "MiniMelonVPNOnline.png"; @@ -77,6 +79,15 @@ public partial class MainWindow : Window refreshBtn.Clicked += OnRefreshClicked; fixed1.Put(refreshBtn, 190, 40); + restartToggleBtn = new Button + { + Visible = true, + Label = "-" + }; + restartToggleBtn.SetSizeRequest(80, 30); + restartToggleBtn.Clicked += RestartToggleBtn_Clicked; + fixed1.Put(restartToggleBtn, 190, 5); + clientsListText = new TextView { Visible = true, @@ -181,7 +192,7 @@ public partial class MainWindow : Window } } } - return ""; + return "-"; } void StartupCheckMethod() @@ -233,7 +244,7 @@ public partial class MainWindow : Window ConnectedToVPN = false; break; case ClientResponseState.Online: - statusLabel.Text = "Online - " + GetVpnInternalIp(); + statusLabel.Text = "Online : " + GetVpnInternalIp(); ConnectedToVPN = true; break; case ClientResponseState.Offline: @@ -247,6 +258,14 @@ public partial class MainWindow : Window case ClientResponseState.Restarting: statusLabel.Text = "Restarting"; ConnectedToVPN = true; + break; + case ClientResponseState.RestartOn: + RestartMode = true; + restartToggleBtn.Label = "Restart [#]"; + break; + case ClientResponseState.RestartOff: + RestartMode = false; + restartToggleBtn.Label = "Restart [ ]"; break; } if (ConnectedToVPN) UpdateIcon(MelonOnlineImg); @@ -394,4 +413,24 @@ public partial class MainWindow : Window }; thread.Start(); } + + void RestartToggleBtn_Clicked(object sender, EventArgs e) + { + + ClientResponseState s = (RestartMode) ? Client.SendDataMessage(DataMessage.RestartOff) : Client.SendDataMessage(DataMessage.RestartOn); + switch (s) + { + case ClientResponseState.Error: + case ClientResponseState.ServerError: + case ClientResponseState.RestartOn: + RestartMode = true; + restartToggleBtn.Label = "Restart [#]"; + break; + case ClientResponseState.RestartOff: + RestartMode = false; + restartToggleBtn.Label = "Restart [ ]"; + break; + } + } + } diff --git a/MelonVPNCore/DaemonSocketServer.cs b/MelonVPNCore/DaemonSocketServer.cs index 942e8b4..7f28013 100644 --- a/MelonVPNCore/DaemonSocketServer.cs +++ b/MelonVPNCore/DaemonSocketServer.cs @@ -11,7 +11,7 @@ namespace MelonVPNCore { private static Process currentVpnProcess = null; private static bool shouldBeRunning = false; - private static bool shouldRestart = true; + private static bool shouldRestart = false; private static int startingTime = 3000; private static int restartDelay = 250; @@ -25,7 +25,7 @@ namespace MelonVPNCore { Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listener.Bind(localEndPoint); - listener.Listen(10); + listener.Listen(32); string lastClientUpdate = Messages.EOF; diff --git a/MelonVPNCore/GUISocketServer.cs b/MelonVPNCore/GUISocketServer.cs index 6d4817d..20a4d6b 100644 --- a/MelonVPNCore/GUISocketServer.cs +++ b/MelonVPNCore/GUISocketServer.cs @@ -9,7 +9,6 @@ namespace MelonVPNCore { public static event EventHandler Receive; public static event EventHandler ClientListUpdate; - public static event EventHandler RestartModeSync; public static void StartServer() { @@ -21,7 +20,7 @@ namespace MelonVPNCore { Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listener.Bind(localEndPoint); - listener.Listen(10); + listener.Listen(32); while (true) { @@ -44,9 +43,8 @@ namespace MelonVPNCore if (data == Messages.ErrorMsg) ret = ClientResponseState.ServerError; if (data == Messages.StartingMsg) ret = ClientResponseState.Starting; if (data == Messages.RestartingMsg) ret = ClientResponseState.Restarting; - bool rsm = false; - if (data == Messages.RestartOnMsg) rsm = true; - if (data == Messages.RestartOffMsg) rsm = false; + if (data == Messages.RestartOnMsg) ret = ClientResponseState.RestartOn; + if (data == Messages.RestartOffMsg) ret = ClientResponseState.RestartOff; Console.WriteLine(data); if (data.StartsWith(Messages.ClientListStartMsg, StringComparison.CurrentCulture)) { @@ -61,7 +59,6 @@ namespace MelonVPNCore handler.Close(); Receive?.Invoke(null, ret); - RestartModeSync?.Invoke(null, rsm); } } catch (Exception e)