From 06049cb06f22269926b666d2e49c744e5f4d7660 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Thu, 14 Jan 2021 19:52:23 +0800 Subject: [PATCH] Refactor Servers Test Delay Interval --- Netch/Forms/MainForm.Control.cs | 23 ++--- Netch/Forms/MainForm.Server_Mode.cs | 145 ++++++++++++---------------- Netch/Forms/MainForm.Status.cs | 24 +++-- Netch/Forms/MainForm.cs | 37 +++---- Netch/Utils/ServerHelper.cs | 49 +++++++++- 5 files changed, 145 insertions(+), 133 deletions(-) diff --git a/Netch/Forms/MainForm.Control.cs b/Netch/Forms/MainForm.Control.cs index ec6b54f0..68c3f7bc 100644 --- a/Netch/Forms/MainForm.Control.cs +++ b/Netch/Forms/MainForm.Control.cs @@ -16,6 +16,16 @@ namespace Netch.Forms { private bool _isFirstCloseWindow = true; + /// + /// 上一次下载的流量 + /// + public ulong LastDownloadBandwidth; + + /// + /// 上一次上传的流量 + /// + 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 } } - - /// - /// 上一次上传的流量 - /// - public ulong LastUploadBandwidth; - - /// - /// 上一次下载的流量 - /// - public ulong LastDownloadBandwidth; } } \ No newline at end of file diff --git a/Netch/Forms/MainForm.Server_Mode.cs b/Netch/Forms/MainForm.Server_Mode.cs index 079cff06..5fc4bb55 100644 --- a/Netch/Forms/MainForm.Server_Mode.cs +++ b/Netch/Forms/MainForm.Server_Mode.cs @@ -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 - /// - /// Init at + /// Init at /// 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 } } \ No newline at end of file diff --git a/Netch/Forms/MainForm.Status.cs b/Netch/Forms/MainForm.Status.cs index 1ac8dc5d..841fc7f5 100644 --- a/Netch/Forms/MainForm.Status.cs +++ b/Netch/Forms/MainForm.Status.cs @@ -13,8 +13,6 @@ namespace Netch.Forms partial class MainForm { - private bool IsWaiting => State == State.Waiting || State == State.Stopped; - private State _state = State.Waiting; /// @@ -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(); 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; diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index ab161cd7..95f6d09c 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -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(); } diff --git a/Netch/Utils/ServerHelper.cs b/Netch/Utils/ServerHelper.cs index 1c45afb3..a1f6b759 100644 --- a/Netch/Utils/ServerHelper.cs +++ b/Netch/Utils/ServerHelper.cs @@ -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 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 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 } } \ No newline at end of file