From b3c2084b76a6725a56bbd6daf8c68d3bb37339f8 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:50:42 +0800 Subject: [PATCH] Code optimization, function asynchrony --- v2rayN/ServiceLib/Handler/ProfileExHandler.cs | 9 +++++++-- v2rayN/ServiceLib/Handler/StatisticsHandler.cs | 4 ++-- v2rayN/ServiceLib/Handler/TaskHandler.cs | 4 ---- v2rayN/ServiceLib/Services/DownloadService.cs | 18 +++++++++--------- v2rayN/ServiceLib/Services/SpeedtestService.cs | 10 +++------- .../Statistics/StatisticsSingboxService.cs | 2 +- .../ViewModels/MainWindowViewModel.cs | 2 +- v2rayN/v2rayN/Views/BackupAndRestoreView.xaml | 2 +- v2rayN/v2rayN/Views/CheckUpdateView.xaml | 4 ++-- v2rayN/v2rayN/Views/QrcodeView.xaml | 4 ++-- 10 files changed, 28 insertions(+), 31 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/ProfileExHandler.cs b/v2rayN/ServiceLib/Handler/ProfileExHandler.cs index f9c34193..0636d0b1 100644 --- a/v2rayN/ServiceLib/Handler/ProfileExHandler.cs +++ b/v2rayN/ServiceLib/Handler/ProfileExHandler.cs @@ -14,9 +14,14 @@ namespace ServiceLib.Handler public ProfileExHandler() { + Init(); + } + + private async Task Init() + { + await InitData(); Task.Run(async () => { - await Init(); while (true) { await SaveQueueIndexIds(); @@ -25,7 +30,7 @@ namespace ServiceLib.Handler }); } - private async Task Init() + private async Task InitData() { await SQLiteHelper.Instance.ExecuteAsync($"delete from ProfileExItem where indexId not in ( select indexId from ProfileItem )"); diff --git a/v2rayN/ServiceLib/Handler/StatisticsHandler.cs b/v2rayN/ServiceLib/Handler/StatisticsHandler.cs index ababd01f..249f5e69 100644 --- a/v2rayN/ServiceLib/Handler/StatisticsHandler.cs +++ b/v2rayN/ServiceLib/Handler/StatisticsHandler.cs @@ -14,7 +14,7 @@ public List ServerStat => _lstServerStat; - public void Init(Config config, Action updateFunc) + public async Task Init(Config config, Action updateFunc) { _config = config; _updateFunc = updateFunc; @@ -23,7 +23,7 @@ return; } - InitData(); + await InitData(); _statisticsV2Ray = new StatisticsV2rayService(config, UpdateServerStatHandler); _statisticsSingbox = new StatisticsSingboxService(config, UpdateServerStatHandler); diff --git a/v2rayN/ServiceLib/Handler/TaskHandler.cs b/v2rayN/ServiceLib/Handler/TaskHandler.cs index 2166f036..edff7e5e 100644 --- a/v2rayN/ServiceLib/Handler/TaskHandler.cs +++ b/v2rayN/ServiceLib/Handler/TaskHandler.cs @@ -5,10 +5,6 @@ private static readonly Lazy _instance = new(() => new()); public static TaskHandler Instance => _instance.Value; - public TaskHandler() - { - } - public void RegUpdateTask(Config config, Action updateFunc) { Task.Run(() => UpdateTaskRunSubscription(config, updateFunc)); diff --git a/v2rayN/ServiceLib/Services/DownloadService.cs b/v2rayN/ServiceLib/Services/DownloadService.cs index f266824b..88b01772 100644 --- a/v2rayN/ServiceLib/Services/DownloadService.cs +++ b/v2rayN/ServiceLib/Services/DownloadService.cs @@ -59,7 +59,7 @@ namespace ServiceLib.Services UpdateCompleted?.Invoke(this, new RetResult(value > 100, $"...{value}%")); }; - var webProxy = GetWebProxy(blProxy); + var webProxy = await GetWebProxy(blProxy); await DownloaderHelper.Instance.DownloadFileAsync(webProxy, url, fileName, @@ -84,7 +84,7 @@ namespace ServiceLib.Services var webRequestHandler = new SocketsHttpHandler { AllowAutoRedirect = false, - Proxy = GetWebProxy(blProxy) + Proxy = await GetWebProxy(blProxy) }; HttpClient client = new(webRequestHandler); @@ -151,7 +151,7 @@ namespace ServiceLib.Services try { SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13); - var webProxy = GetWebProxy(blProxy); + var webProxy = await GetWebProxy(blProxy); var client = new HttpClient(new SocketsHttpHandler() { Proxy = webProxy, @@ -197,7 +197,7 @@ namespace ServiceLib.Services { SetSecurityProtocol(AppHandler.Instance.Config.guiItem.enableSecurityProtocolTls13); - var webProxy = GetWebProxy(blProxy); + var webProxy = await GetWebProxy(blProxy); if (Utils.IsNullOrEmpty(userAgent)) { @@ -222,7 +222,7 @@ namespace ServiceLib.Services { try { - webProxy ??= GetWebProxy(true); + webProxy ??= await GetWebProxy(true); try { @@ -274,14 +274,14 @@ namespace ServiceLib.Services return responseTime; } - private WebProxy? GetWebProxy(bool blProxy) + private async Task GetWebProxy(bool blProxy) { if (!blProxy) { return null; } var httpPort = AppHandler.Instance.GetLocalPort(EInboundProtocol.http); - if (!SocketCheck(Global.Loopback, httpPort)) + if (await SocketCheck(Global.Loopback, httpPort) == false) { return null; } @@ -289,13 +289,13 @@ namespace ServiceLib.Services return new WebProxy(Global.Loopback, httpPort); } - private bool SocketCheck(string ip, int port) + private async Task SocketCheck(string ip, int port) { try { IPEndPoint point = new(IPAddress.Parse(ip), port); using Socket? sock = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - sock.Connect(point); + await sock.ConnectAsync(point); return true; } catch (Exception) diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index eaee4e4f..7132a600 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -99,11 +99,11 @@ namespace ServiceLib.Services { continue; } - tasks.Add(Task.Run(() => + tasks.Add(Task.Run(async () => { try { - int time = GetTcpingTime(it.Address, it.Port); + int time = await GetTcpingTime(it.Address, it.Port); var output = FormatOut(time, Global.DelayUnit); ProfileExHandler.Instance.SetTestDelay(it.IndexId, output); @@ -336,7 +336,7 @@ namespace ServiceLib.Services return FormatOut(responseTime, Global.DelayUnit); } - private int GetTcpingTime(string url, int port) + private async Task GetTcpingTime(string url, int port) { int responseTime = -1; @@ -370,10 +370,6 @@ namespace ServiceLib.Services private string FormatOut(object time, string unit) { - //if (time.ToString().Equals("-1")) - //{ - // return "Timeout"; - //} return $"{time}"; } diff --git a/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs b/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs index 3b7d0484..dc80d370 100644 --- a/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs +++ b/v2rayN/ServiceLib/Services/Statistics/StatisticsSingboxService.cs @@ -17,7 +17,7 @@ namespace ServiceLib.Services.Statistics _updateFunc = updateFunc; _exitFlag = false; - Task.Run(() => Run()); + Task.Run(Run); } private async void Init() diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 4110070e..1844516e 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -213,7 +213,7 @@ namespace ServiceLib.ViewModels if (_config.guiItem.enableStatistics) { - StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler); + await StatisticsHandler.Instance.Init(_config, UpdateStatisticsHandler); } await Reload(); diff --git a/v2rayN/v2rayN/Views/BackupAndRestoreView.xaml b/v2rayN/v2rayN/Views/BackupAndRestoreView.xaml index e0cecd10..7c48e9c2 100644 --- a/v2rayN/v2rayN/Views/BackupAndRestoreView.xaml +++ b/v2rayN/v2rayN/Views/BackupAndRestoreView.xaml @@ -30,7 +30,7 @@ Content="{x:Static resx:ResUI.menuClose}" DockPanel.Dock="Right" IsCancel="True" - Style="{StaticResource MaterialDesignFlatButton}" /> + Style="{StaticResource DefButton}" /> + Style="{StaticResource DefButton}" />