From 41491f8c207cd8c8728efe408282800f743bbbbb Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 5 Mar 2021 16:07:21 +0800 Subject: [PATCH] Cut OnlyInstance --- Netch/Forms/MainForm.cs | 17 -------- Netch/Models/Setting.cs | 5 --- Netch/Netch.cs | 8 +--- Netch/Utils/OnlyInstance.cs | 78 ------------------------------------- 4 files changed, 1 insertion(+), 107 deletions(-) delete mode 100644 Netch/Utils/OnlyInstance.cs diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index ee23dc8a..3ae4f9f6 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -85,8 +85,6 @@ namespace Netch.Forms private void MainForm_Load(object sender, EventArgs e) { - OnlyInstance.Called += OnCalled; - // 计算 ComboBox绘制 目标宽度 RecordSize(); @@ -1368,21 +1366,6 @@ namespace Netch.Forms Environment.Exit(Environment.ExitCode); } - private void OnCalled(object sender, OnlyInstance.Commands e) - { - switch (e) - { - case OnlyInstance.Commands.Show: - NotifyIcon_MouseDoubleClick(null, null); - break; - case OnlyInstance.Commands.Exit: - Exit(true); - break; - default: - throw new ArgumentOutOfRangeException(nameof(e), e, null); - } - } - #region FormClosingButton private bool _isFirstCloseWindow = true; diff --git a/Netch/Models/Setting.cs b/Netch/Models/Setting.cs index 5456fa9e..6c9e220c 100644 --- a/Netch/Models/Setting.cs +++ b/Netch/Models/Setting.cs @@ -272,11 +272,6 @@ namespace Netch.Models /// public TUNTAPConfig TUNTAP { get; set; } = new(); - /// - /// UDP Socket 占用端口 - /// - public ushort UDPSocketPort { get; set; } = 18291; - /// /// 是否打开软件时更新订阅 /// diff --git a/Netch/Netch.cs b/Netch/Netch.cs index d4ab6020..6218a05e 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -42,8 +42,7 @@ namespace Netch // 检查是否已经运行 if (!Global.Mutex.WaitOne(0, false)) { - OnlyInstance.Send(OnlyInstance.Commands.Show); - Logging.Info("唤起单实例"); + // TODO Active previous instance Form // 退出进程 Environment.Exit(1); @@ -72,11 +71,6 @@ namespace Netch Logging.Info($"版本: {UpdateChecker.Owner}/{UpdateChecker.Repo}@{UpdateChecker.Version}"); Task.Run(() => { Logging.Info($"主程序 SHA256: {Utils.Utils.SHA256CheckSum(Global.NetchExecutable)}"); }); - Task.Run(() => - { - Logging.Info("启动单实例"); - OnlyInstance.Server(); - }); // 绑定错误捕获 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); diff --git a/Netch/Utils/OnlyInstance.cs b/Netch/Utils/OnlyInstance.cs deleted file mode 100644 index 09f4a6e1..00000000 --- a/Netch/Utils/OnlyInstance.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System; -using System.Net; -using System.Net.Sockets; -using System.Text; - -namespace Netch.Utils -{ - public static class OnlyInstance - { - public enum Commands - { - Show, - Exit - } - - public static event EventHandler? Called; - - private static void OnCalled(Commands e) - { - Called?.Invoke(null, e); - } - - public static async void Server() - { - try - { - const int tryLimit = 3; - var i = tryLimit; - while (i > 0) - try - { - PortHelper.CheckPort(Global.Settings.UDPSocketPort, PortType.UDP); - if (i != tryLimit) - Configuration.Save(); - - break; - } - catch - { - Global.Settings.UDPSocketPort = PortHelper.GetAvailablePort(PortType.UDP); - i--; - } - - var data = new byte[1024]; - var newsock = new UdpClient(new IPEndPoint(IPAddress.Loopback, Global.Settings.UDPSocketPort)); - - while (true) - { - var result = await newsock.ReceiveAsync(); - data = result.Buffer; - if (Enum.TryParse(Encoding.ASCII.GetString(data, 0, data.Length), out var command)) - OnCalled(command); - } - } - catch (Exception e) - { - Logging.Error(e.ToString()); - } - } - - public static async void Send(Commands command) - { - try - { - using var udpClient = new UdpClient(new IPEndPoint(IPAddress.Loopback, Global.Settings.UDPSocketPort)); - udpClient.Connect(IPAddress.Loopback, Global.Settings.UDPSocketPort); - var sendBytes = Encoding.ASCII.GetBytes(command.ToString()); - await udpClient.SendAsync(sendBytes, sendBytes.Length); - - udpClient.Close(); - } - catch (Exception e) - { - Console.WriteLine(e.ToString()); - } - } - } -} \ No newline at end of file