Refactor Servers Test Delay Interval

This commit is contained in:
ChsBuffer
2021-01-14 19:52:23 +08:00
parent 8cf765de92
commit 06049cb06f
5 changed files with 145 additions and 133 deletions

View File

@@ -16,6 +16,16 @@ namespace Netch.Forms
{
private bool _isFirstCloseWindow = true;
/// <summary>
/// 上一次下载的流量
/// </summary>
public ulong LastDownloadBandwidth;
/// <summary>
/// 上一次上传的流量
/// </summary>
public ulong LastUploadBandwidth;
private async void ControlFun()
{
Configuration.Save();
@@ -59,7 +69,6 @@ namespace Netch.Forms
}
if (Global.Settings.StartedTcping)
{
// 自动检测延迟
_ = Task.Run(() =>
{
@@ -72,7 +81,6 @@ namespace Netch.Forms
Thread.Sleep(Global.Settings.StartedTcping_Interval * 1000);
}
});
}
}
else
{
@@ -86,7 +94,6 @@ namespace Netch.Forms
State = State.Stopping;
await MainController.Stop();
State = State.Stopped;
_ = Task.Run(TestServer);
}
}
@@ -113,15 +120,5 @@ namespace Netch.Forms
// ignored
}
}
/// <summary>
/// 上一次上传的流量
/// </summary>
public ulong LastUploadBandwidth;
/// <summary>
/// 上一次下载的流量
/// </summary>
public ulong LastDownloadBandwidth;
}
}

View File

@@ -1,8 +1,8 @@
using System;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Netch.Models;
using Netch.Utils;
namespace Netch.Forms
@@ -13,86 +13,8 @@ namespace Netch.Forms
partial class MainForm
{
#region Server
private void InitServer()
{
var comboBoxInitialized = _comboBoxInitialized;
_comboBoxInitialized = false;
ServerComboBox.Items.Clear();
ServerComboBox.Items.AddRange(Global.Settings.Server.ToArray());
SelectLastServer();
_comboBoxInitialized = comboBoxInitialized;
}
private static void TestServer()
{
try
{
Parallel.ForEach(Global.Settings.Server, new ParallelOptions {MaxDegreeOfParallelism = 16},
server => { server.Test(); });
}
catch (Exception)
{
// ignored
}
}
public void SelectLastServer()
{
// 如果值合法,选中该位置
if (Global.Settings.ServerComboBoxSelectedIndex > 0 &&
Global.Settings.ServerComboBoxSelectedIndex < ServerComboBox.Items.Count)
{
ServerComboBox.SelectedIndex = Global.Settings.ServerComboBoxSelectedIndex;
}
// 如果值非法,且当前 ServerComboBox 中有元素,选择第一个位置
else if (ServerComboBox.Items.Count > 0)
{
ServerComboBox.SelectedIndex = 0;
}
// 如果当前 ServerComboBox 中没元素,不做处理
}
#endregion
#region Mode
public void InitMode()
{
var comboBoxInitialized = _comboBoxInitialized;
_comboBoxInitialized = false;
ModeComboBox.Items.Clear();
ModeComboBox.Items.AddRange(Global.Modes.ToArray());
ModeComboBox.Tag = null;
SelectLastMode();
_comboBoxInitialized = comboBoxInitialized;
}
public void SelectLastMode()
{
// 如果值合法,选中该位置
if (Global.Settings.ModeComboBoxSelectedIndex > 0 &&
Global.Settings.ModeComboBoxSelectedIndex < ModeComboBox.Items.Count)
{
ModeComboBox.SelectedIndex = Global.Settings.ModeComboBoxSelectedIndex;
}
// 如果值非法,且当前 ModeComboBox 中有元素,选择第一个位置
else if (ModeComboBox.Items.Count > 0)
{
ModeComboBox.SelectedIndex = 0;
}
// 如果当前 ModeComboBox 中没元素,不做处理
}
#endregion
/// <summary>
/// Init at <see cref="MainForm_Load"/>
/// Init at <see cref="MainForm_Load" />
/// </summary>
private int _eWidth;
@@ -101,9 +23,7 @@ namespace Netch.Forms
try
{
if (!(sender is ComboBox cbx))
{
return;
}
// 绘制背景颜色
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
@@ -115,7 +35,7 @@ namespace Netch.Forms
switch (cbx.Items[e.Index])
{
case Models.Server item:
case Server item:
{
// 计算延迟底色
SolidBrush brush;
@@ -164,12 +84,69 @@ namespace Netch.Forms
{
Name = $"Add{fullName}ServerToolStripMenuItem",
Size = new Size(259, 22),
Text = i18N.TranslateFormat("Add [{0}] Server", fullName),
Text = i18N.TranslateFormat("Add [{0}] Server", fullName)
};
_mainFormText.Add(control.Name, new[] {"Add [{0}] Server", fullName});
control.Click += AddServerToolStripMenuItem_Click;
ServerToolStripMenuItem.DropDownItems.Add(control);
}
}
#region Server
private void InitServer()
{
var comboBoxInitialized = _comboBoxInitialized;
_comboBoxInitialized = false;
ServerComboBox.Items.Clear();
ServerComboBox.Items.AddRange(Global.Settings.Server.ToArray());
SelectLastServer();
_comboBoxInitialized = comboBoxInitialized;
}
public void SelectLastServer()
{
// 如果值合法,选中该位置
if (Global.Settings.ServerComboBoxSelectedIndex > 0 &&
Global.Settings.ServerComboBoxSelectedIndex < ServerComboBox.Items.Count)
ServerComboBox.SelectedIndex = Global.Settings.ServerComboBoxSelectedIndex;
// 如果值非法,且当前 ServerComboBox 中有元素,选择第一个位置
else if (ServerComboBox.Items.Count > 0)
ServerComboBox.SelectedIndex = 0;
// 如果当前 ServerComboBox 中没元素,不做处理
}
#endregion
#region Mode
public void InitMode()
{
var comboBoxInitialized = _comboBoxInitialized;
_comboBoxInitialized = false;
ModeComboBox.Items.Clear();
ModeComboBox.Items.AddRange(Global.Modes.ToArray());
ModeComboBox.Tag = null;
SelectLastMode();
_comboBoxInitialized = comboBoxInitialized;
}
public void SelectLastMode()
{
// 如果值合法,选中该位置
if (Global.Settings.ModeComboBoxSelectedIndex > 0 &&
Global.Settings.ModeComboBoxSelectedIndex < ModeComboBox.Items.Count)
ModeComboBox.SelectedIndex = Global.Settings.ModeComboBoxSelectedIndex;
// 如果值非法,且当前 ModeComboBox 中有元素,选择第一个位置
else if (ModeComboBox.Items.Count > 0)
ModeComboBox.SelectedIndex = 0;
// 如果当前 ModeComboBox 中没元素,不做处理
}
#endregion
}
}

View File

@@ -13,8 +13,6 @@ namespace Netch.Forms
partial class MainForm
{
private bool IsWaiting => State == State.Waiting || State == State.Stopped;
private State _state = State.Waiting;
/// <summary>
@@ -46,6 +44,8 @@ namespace Netch.Forms
_state = value;
ServerHelper.Timer.Enabled = IsWaiting(_state);
StatusText();
switch (value)
{
@@ -97,6 +97,15 @@ namespace Netch.Forms
}
}
private bool IsWaiting()
{
return State == State.Waiting || State == State.Stopped;
}
private static bool IsWaiting(State state)
{
return state == State.Waiting || state == State.Stopped;
}
public void BandwidthState(bool state)
{
UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = state;
@@ -205,8 +214,6 @@ namespace Netch.Forms
set => _socks5Port = value;
}
public static void UpdateShareLan() => _shareLan = Global.Settings.LocalAddress != "127.0.0.1";
public static string Value
{
get
@@ -214,14 +221,10 @@ namespace Netch.Forms
var strings = new List<string>();
if (_socks5Port != null)
{
strings.Add($"Socks5 {i18N.Translate("Local Port", ": ")}{_socks5Port}");
}
if (_httpPort != null)
{
strings.Add($"HTTP {i18N.Translate("Local Port", ": ")}{_httpPort}");
}
if (!strings.Any())
return string.Empty;
@@ -230,6 +233,11 @@ namespace Netch.Forms
}
}
public static void UpdateShareLan()
{
_shareLan = Global.Settings.LocalAddress != "127.0.0.1";
}
public static void Reset()
{
_httpPort = _socks5Port = null;

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
@@ -77,22 +76,6 @@ namespace Netch.Forms
if (Global.Settings.StartWhenOpened)
ControlButton.PerformClick();
// 自动检测延迟
Task.Run(() =>
{
while (true)
if (State == State.Waiting || State == State.Stopped)
{
TestServer();
Thread.Sleep(10000);
}
else
{
Thread.Sleep(200);
}
});
Task.Run(() =>
{
// 检查更新
@@ -258,7 +241,7 @@ namespace Netch.Forms
private void Exit(bool forceExit = false)
{
if (!IsWaiting && !Global.Settings.StopWhenExited && !forceExit)
if (!IsWaiting() && !Global.Settings.StopWhenExited && !forceExit)
{
MessageBoxX.Show(i18N.Translate("Please press Stop button first"));
@@ -268,7 +251,7 @@ namespace Netch.Forms
Hide();
NotifyIcon.Visible = false;
if (!IsWaiting)
if (!IsWaiting())
ControlFun();
Configuration.Save();
@@ -319,7 +302,7 @@ namespace Netch.Forms
switch (e.Mode)
{
case PowerModes.Suspend: //操作系统即将挂起
if (!IsWaiting)
if (!IsWaiting())
{
_resumeFlag = true;
Logging.Info("操作系统即将挂起,自动停止");
@@ -354,18 +337,20 @@ namespace Netch.Forms
Show();
}
private async void SpeedPictureBox_Click(object sender, EventArgs e)
private void SpeedPictureBox_Click(object sender, EventArgs e)
{
Enabled = false;
StatusText(i18N.Translate("Testing"));
try
ServerHelper.TestDelayFinished += OnTestDelayFinished;
_ = Task.Run(ServerHelper.TestAllDelay);
void OnTestDelayFinished(object o1, EventArgs e1)
{
await Task.Run(TestServer);
Refresh();
NotifyTip(i18N.Translate("Test done"));
}
finally
{
ServerHelper.TestDelayFinished -= OnTestDelayFinished;
Enabled = true;
StatusText();
}

View File

@@ -2,6 +2,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Timers;
using Netch.Models;
using Newtonsoft.Json.Linq;
@@ -9,14 +11,27 @@ namespace Netch.Utils
{
public static class ServerHelper
{
public static readonly IEnumerable<IServerUtil> ServerUtils;
static ServerHelper()
{
var serversUtilsTypes = Assembly.GetExecutingAssembly().GetExportedTypes().Where(type => type.GetInterfaces().Contains(typeof(IServerUtil)));
ServerUtils = serversUtilsTypes.Select(t => (IServerUtil) Activator.CreateInstance(t)).OrderBy(util => util.Priority);
Timer = new Timer
{
Interval = 10000,
AutoReset = true,
Enabled = false
};
Timer.Elapsed += (_, _) => TestAllDelay();
Timer.Start();
}
#region Handler
public static readonly IEnumerable<IServerUtil> ServerUtils;
public static Server ParseJObject(JObject o)
{
var handle = GetUtilByTypeName((string) o["Type"]);
@@ -47,5 +62,35 @@ namespace Netch.Utils
{
return ServerUtils.FirstOrDefault(i => i.UriScheme.Any(s => s.Equals(typeName)));
}
#endregion
#region Delay
public static readonly Timer Timer;
private static bool _mux;
public static event EventHandler TestDelayFinished;
public static bool TestAllDelay()
{
if (_mux)
return false;
try
{
_mux = true;
Parallel.ForEach(Global.Settings.Server, new ParallelOptions {MaxDegreeOfParallelism = 16},
server => { server.Test(); });
_mux = false;
TestDelayFinished?.Invoke(null, new EventArgs());
}
catch (Exception)
{
// ignored
}
return true;
}
#endregion
}
}