修复主控制器启动阻塞UI线程,保留设置来重新生成状态栏端口信息

This commit is contained in:
ChsBuffer
2020-08-07 11:22:13 +08:00
parent 3311115bda
commit 8f7f2e3d1c
4 changed files with 100 additions and 82 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Netch.Forms;
using Netch.Models;
@@ -22,11 +23,46 @@ namespace Netch.Controllers
public ModeController pModeController;
private Server _savedServer;
private Mode _savedMode;
public string PortInfo
{
get
{
if (_savedMode == null || _savedServer == null)
return string.Empty;
var text = new StringBuilder();
if (_savedServer.Type == "Socks5" && _savedMode.Type != 3 && _savedMode.Type != 5)
// 不可控Socks5, 不可控HTTP
return string.Empty;
if (_localAddress == "0.0.0.0")
text.Append(i18N.Translate("Allow other Devices to connect") + " ");
if (_savedServer.Type != "Socks5")
// 可控Socks5
text.Append($"Socks5 {i18N.Translate("Local Port", ": ")}{_socks5Port}");
if (_savedMode.Type == 3 || _savedMode.Type == 5)
// 有HTTP
text.Append($" | HTTP {i18N.Translate("Local Port", ": ")}{_httpPort}");
return $" ({text})";
}
}
/// <summary>
/// NTT 控制器
/// </summary>
public NTTController pNTTController = new NTTController();
private string _localAddress;
private int _redirectorTCPPort;
private int _httpPort;
private int _socks5Port;
[DllImport("dnsapi", EntryPoint = "DnsFlushResolverCache")]
public static extern uint FlushDNSResolverCache();
@@ -39,6 +75,18 @@ namespace Netch.Controllers
public bool Start(Server server, Mode mode)
{
Logging.Info($"启动主控制器: {server.Type} [{mode.Type}]{mode.Remark}");
#region Record Settings
_httpPort = Global.Settings.HTTPLocalPort;
_socks5Port = Global.Settings.Socks5LocalPort;
_redirectorTCPPort = Global.Settings.RedirectorTCPPort;
_localAddress = Global.Settings.LocalAddress;
_savedServer = server;
_savedMode = mode;
#endregion
FlushDNSResolverCache();
bool result;
@@ -117,6 +165,25 @@ namespace Netch.Controllers
if (result)
{
#region Add UsingPorts
switch (mode.Type)
{
// 成功启动
case 3:
case 5:
UsingPorts.Add(Global.Settings.HTTPLocalPort);
break;
case 0:
UsingPorts.Add(Global.Settings.RedirectorTCPPort);
break;
}
if (server.Type != "Socks5")
UsingPorts.Add(Global.Settings.Socks5LocalPort);
#endregion
switch (mode.Type)
{
case 0:
@@ -148,16 +215,15 @@ namespace Netch.Controllers
/// <summary>
/// 停止
/// </summary>
public void Stop()
public async void Stop()
{
var tasks = new[]
await Task.WhenAll(new[]
{
Task.Factory.StartNew(() => pEncryptedProxyController?.Stop()),
Task.Factory.StartNew(() => UsingPorts.Clear()),
Task.Factory.StartNew(() => pModeController?.Stop()),
Task.Factory.StartNew(() => pNTTController.Stop())
};
Task.WaitAll(tasks);
Task.Run(() => pEncryptedProxyController?.Stop()),
Task.Run(() => UsingPorts.Clear()),
Task.Run(() => pModeController?.Stop()),
Task.Run(() => pNTTController.Stop())
});
}
public static void KillProcessByName(string name)

View File

@@ -44,25 +44,27 @@ namespace Netch.Forms
var server = ServerComboBox.SelectedItem as Models.Server;
var mode = ModeComboBox.SelectedItem as Models.Mode;
bool result;
var result = false;
try
await Task.Run(() =>
{
// TODO 完善控制器异常处理
result = _mainController.Start(server, mode);
}
catch (Exception e)
{
if (e is DllNotFoundException || e is FileNotFoundException)
MessageBoxX.Show(e.Message + "\n\n" + i18N.Translate("Missing File or runtime components"), owner: this);
throw;
}
try
{
// TODO 完善控制器异常处理
result = _mainController.Start(server, mode);
}
catch (Exception e)
{
if (e is DllNotFoundException || e is FileNotFoundException)
MessageBoxX.Show(e.Message + "\n\n" + i18N.Translate("Missing File or runtime components"), owner: Global.MainForm);
Netch.Application_OnException(null, new ThreadExceptionEventArgs(e));
}
});
if (result)
{
State = State.Started;
StatusTextAppend(LocalPortText(server.Type, mode.Type));
await Task.Run(() => { Bandwidth.NetTraffic(server, mode, _mainController); });
_ = Task.Run(() => { Bandwidth.NetTraffic(server, mode, _mainController); });
// 如果勾选启动后最小化
if (Global.Settings.MinimizeWhenStarted)
{
@@ -83,20 +85,13 @@ namespace Netch.Forms
// 自动检测延迟
await Task.Run(() =>
{
while (true)
while (State == State.Started)
{
if (State == State.Started)
{
server.Test();
// 重载服务器列表
InitServer();
server.Test();
// 重载服务器列表
InitServer();
Thread.Sleep(Global.Settings.StartedTcping_Interval * 1000);
}
else
{
break;
}
Thread.Sleep(Global.Settings.StartedTcping_Interval * 1000);
}
});
}
@@ -109,58 +104,14 @@ namespace Netch.Forms
}
else
{
// 停止
State = State.Stopping;
await Task.Run(async () =>
{
// 停止
_mainController.Stop();
State = State.Stopped;
await Task.Run(TestServer);
});
_mainController.Stop();
State = State.Stopped;
_ = Task.Run(TestServer);
}
}
private static string LocalPortText(string serverType, int modeType)
{
var text = new StringBuilder(" (");
if (Global.Settings.LocalAddress == "0.0.0.0")
text.Append(i18N.Translate("Allow other Devices to connect") + " ");
if (serverType == "Socks5")
{
// 不可控Socks5
if (modeType == 3 || modeType == 5)
{
// 可控HTTP
MainController.UsingPorts.Add(Global.Settings.HTTPLocalPort);
text.Append($"HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
else
{
// 不可控HTTP
return string.Empty;
}
}
else
{
// 可控Socks5
MainController.UsingPorts.Add(Global.Settings.Socks5LocalPort);
text.Append($"Socks5 {i18N.Translate("Local Port", ": ")}{Global.Settings.Socks5LocalPort}");
if (modeType == 3 || modeType == 5)
{
// 有HTTP
MainController.UsingPorts.Add(Global.Settings.HTTPLocalPort);
text.Append($" | HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
}
if (modeType == 0)
MainController.UsingPorts.Add(Global.Settings.RedirectorTCPPort);
text.Append(")");
return text.ToString();
}
public void OnBandwidthUpdated(long download)
{
try

View File

@@ -257,7 +257,6 @@ namespace Netch.Forms
};
State = State.Starting;
_mainController.Start(ServerComboBox.SelectedItem as Models.Server, mode);
// State = State.Started;
}
NotifyTip(i18N.Translate("Updating in the background"));

View File

@@ -60,6 +60,8 @@ namespace Netch.Forms
ControlButton.Enabled = true;
ControlButton.Text = i18N.Translate("Stop");
StatusTextAppend(_mainController.PortInfo);
ProfileGroupBox.Enabled = true;
UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = true;