From fe69c5a67bea006669aaa03a6738327ed39c6162 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Tue, 5 Jan 2021 17:53:16 +0800 Subject: [PATCH] Refactor show Bandwidth --- Netch/Forms/MainForm.Control.cs | 2 +- Netch/Forms/MainForm.Status.cs | 19 ++++++------------- Netch/Utils/Bandwidth.cs | 9 ++++++++- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Netch/Forms/MainForm.Control.cs b/Netch/Forms/MainForm.Control.cs index d67d9996..ec6b54f0 100644 --- a/Netch/Forms/MainForm.Control.cs +++ b/Netch/Forms/MainForm.Control.cs @@ -42,7 +42,7 @@ namespace Netch.Forms if (await MainController.Start(server, mode)) { State = State.Started; - _ = Task.Run(() => { Bandwidth.NetTraffic(server, mode); }); + _ = Task.Run(() => { Bandwidth.NetTraffic(); }); // 如果勾选启动后最小化 if (Global.Settings.MinimizeWhenStarted) { diff --git a/Netch/Forms/MainForm.Status.cs b/Netch/Forms/MainForm.Status.cs index d2844072..74820be6 100644 --- a/Netch/Forms/MainForm.Status.cs +++ b/Netch/Forms/MainForm.Status.cs @@ -70,25 +70,13 @@ namespace Netch.Forms ProfileGroupBox.Enabled = true; - //Socks5 - Boolean s5BwFlag = true; - if (MainController.Server is Socks5) - { - Socks5 SocksServer = (Socks5) MainController.Server; - - if (!SocksServer.Auth()) s5BwFlag = false; - } - - //Socks5无身份验证且为网页代理模式时无法统计流量不显示流量状态栏,Socks5有身份验证时将统计V2ray的流量 - if (s5BwFlag || Models.ModeExtension.TestNatRequired(MainController.Mode)) - UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = Global.Flags.IsWindows10Upper; break; case State.Stopping: ControlButton.Enabled = false; ControlButton.Text = "..."; ProfileGroupBox.Enabled = false; - UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = false; + BandwidthState(false); NatTypeStatusText(); break; case State.Stopped: @@ -110,6 +98,11 @@ namespace Netch.Forms } } + public void BandwidthState(bool state) + { + UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = state; + } + public void NatTypeStatusText(string text = "", string country = "") { if (InvokeRequired) diff --git a/Netch/Utils/Bandwidth.cs b/Netch/Utils/Bandwidth.cs index 47d8b16a..852f4032 100644 --- a/Netch/Utils/Bandwidth.cs +++ b/Netch/Utils/Bandwidth.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.Diagnostics.Tracing.Parsers; using Microsoft.Diagnostics.Tracing.Session; using Netch.Controllers; +using Netch.Forms; using Netch.Models; using Netch.Servers.Shadowsocks; using Netch.Servers.Socks5; @@ -54,7 +55,7 @@ namespace Netch.Utils /// /// 根据程序名统计流量 /// - public static void NetTraffic(in Server server, in Mode mode) + public static void NetTraffic() { if (!Global.Flags.IsWindows10Upper) return; @@ -101,6 +102,12 @@ namespace Netch.Utils instances.Select(instance => $"({instance.Id})" + instance.ProcessName).ToArray())); received = 0; + + if (!instances.Any()) + return; + + Global.MainForm.BandwidthState(true); + Task.Run(() => { tSession = new TraceEventSession("KernelAndClrEventsSession");