Cut OnlyInstance

This commit is contained in:
ChsBuffer
2021-03-05 16:07:21 +08:00
parent 84b412bc8c
commit 41491f8c20
4 changed files with 1 additions and 107 deletions

View File

@@ -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;

View File

@@ -272,11 +272,6 @@ namespace Netch.Models
/// </summary>
public TUNTAPConfig TUNTAP { get; set; } = new();
/// <summary>
/// UDP Socket 占用端口
/// </summary>
public ushort UDPSocketPort { get; set; } = 18291;
/// <summary>
/// 是否打开软件时更新订阅
/// </summary>

View File

@@ -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);

View File

@@ -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<Commands>? 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<Commands>(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());
}
}
}
}