From 7a839063d0daa69a3d1fd17bfee32fa0d6a9fa41 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Mon, 7 Oct 2024 11:18:24 +0800 Subject: [PATCH] Refactor CoreHandler --- v2rayN/ServiceLib/Handler/CoreHandler.cs | 4 +++- v2rayN/ServiceLib/Services/SpeedtestService.cs | 17 ++++++++--------- .../ViewModels/MainWindowViewModel.cs | 10 ++++------ .../ServiceLib/ViewModels/ProfilesViewModel.cs | 7 ++----- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/v2rayN/ServiceLib/Handler/CoreHandler.cs b/v2rayN/ServiceLib/Handler/CoreHandler.cs index 9b48b35a..acc5097f 100644 --- a/v2rayN/ServiceLib/Handler/CoreHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreHandler.cs @@ -8,12 +8,14 @@ namespace ServiceLib.Handler /// public class CoreHandler { + private static readonly Lazy _instance = new(() => new()); + public static CoreHandler Instance => _instance.Value; private Config _config; private Process? _process; private Process? _processPre; private Action _updateFunc; - public CoreHandler(Config config, Action update) + public void Init(Config config, Action update) { _config = config; _updateFunc = update; diff --git a/v2rayN/ServiceLib/Services/SpeedtestService.cs b/v2rayN/ServiceLib/Services/SpeedtestService.cs index 76aa57d4..06c8b727 100644 --- a/v2rayN/ServiceLib/Services/SpeedtestService.cs +++ b/v2rayN/ServiceLib/Services/SpeedtestService.cs @@ -7,16 +7,15 @@ namespace ServiceLib.Services public class SpeedtestService { private Config? _config; - private CoreHandler _coreHandler; private List _selecteds; private ESpeedActionType _actionType; private Action _updateFunc; private bool _exitLoop = false; - public SpeedtestService(Config config, CoreHandler coreHandler, List selecteds, ESpeedActionType actionType, Action update) + public SpeedtestService(Config config, List selecteds, ESpeedActionType actionType, Action update) { _config = config; - _coreHandler = coreHandler; + _actionType = actionType; _updateFunc = update; @@ -137,7 +136,7 @@ namespace ServiceLib.Services { string msg = string.Empty; - pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds); + pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds); if (pid < 0) { UpdateFunc("", ResUI.FailedToRunCore); @@ -185,7 +184,7 @@ namespace ServiceLib.Services { if (pid > 0) { - _coreHandler.CoreStopPid(pid); + CoreHandler.Instance.CoreStopPid(pid); } ProfileExHandler.Instance.SaveTo(); } @@ -201,7 +200,7 @@ namespace ServiceLib.Services // _selecteds = _selecteds.OrderBy(t => t.delay).ToList(); //} - pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds); + pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds); if (pid < 0) { UpdateFunc("", ResUI.FailedToRunCore); @@ -254,7 +253,7 @@ namespace ServiceLib.Services if (pid > 0) { - _coreHandler.CoreStopPid(pid); + CoreHandler.Instance.CoreStopPid(pid); } UpdateFunc("", ResUI.SpeedtestingCompleted); ProfileExHandler.Instance.SaveTo(); @@ -263,7 +262,7 @@ namespace ServiceLib.Services private async Task RunSpeedTestMulti() { int pid = -1; - pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds); + pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds); if (pid < 0) { UpdateFunc("", ResUI.FailedToRunCore); @@ -319,7 +318,7 @@ namespace ServiceLib.Services if (pid > 0) { - _coreHandler.CoreStopPid(pid); + CoreHandler.Instance.CoreStopPid(pid); } UpdateFunc("", ResUI.SpeedtestingCompleted); ProfileExHandler.Instance.SaveTo(); diff --git a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs index 9df6744a..b863e64f 100644 --- a/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs @@ -13,7 +13,6 @@ namespace ServiceLib.ViewModels { #region private prop - private CoreHandler _coreHandler; private bool _isAdministrator { get; set; } #endregion private prop @@ -342,8 +341,7 @@ namespace ServiceLib.ViewModels { ConfigHandler.InitBuiltinRouting(_config); ConfigHandler.InitBuiltinDNS(_config); - _coreHandler = new CoreHandler(_config, UpdateHandler); - Locator.CurrentMutable.RegisterLazySingleton(() => _coreHandler, typeof(CoreHandler)); + CoreHandler.Instance.Init(_config, UpdateHandler); if (_config.guiItem.enableStatistics) { @@ -428,7 +426,7 @@ namespace ServiceLib.ViewModels ProfileExHandler.Instance.SaveTo(); StatisticsHandler.Instance.SaveTo(); StatisticsHandler.Instance.Close(); - _coreHandler.CoreStop(); + CoreHandler.Instance.CoreStop(); Logging.SaveLog("MyAppExit End"); } @@ -750,7 +748,7 @@ namespace ServiceLib.ViewModels //} var node = ConfigHandler.GetDefaultServer(_config); - _coreHandler.LoadCore(node); + CoreHandler.Instance.LoadCore(node); }); } @@ -760,7 +758,7 @@ namespace ServiceLib.ViewModels ChangeSystemProxyStatusAsync(ESysProxyType.ForcedClear, false); - _coreHandler.CoreStop(); + CoreHandler.Instance.CoreStop(); } #endregion core job diff --git a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs index 173d77d4..eaead25f 100644 --- a/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs +++ b/v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs @@ -667,11 +667,8 @@ namespace ServiceLib.ViewModels return; } //ClearTestResult(); - var coreHandler = Locator.Current.GetService(); - if (coreHandler != null) - { - _speedtestHandler = new SpeedtestService(_config, coreHandler, lstSelecteds, actionType, UpdateSpeedtestHandler); - } + + _speedtestHandler = new SpeedtestService(_config, lstSelecteds, actionType, UpdateSpeedtestHandler); } public void ServerSpeedtestStop()