Refactor CoreHandler

pull/5829/head
2dust 2024-10-07 11:18:24 +08:00
parent 4ccc7aa92d
commit 7a839063d0
4 changed files with 17 additions and 21 deletions

View File

@ -8,12 +8,14 @@ namespace ServiceLib.Handler
/// </summary> /// </summary>
public class CoreHandler public class CoreHandler
{ {
private static readonly Lazy<CoreHandler> _instance = new(() => new());
public static CoreHandler Instance => _instance.Value;
private Config _config; private Config _config;
private Process? _process; private Process? _process;
private Process? _processPre; private Process? _processPre;
private Action<bool, string> _updateFunc; private Action<bool, string> _updateFunc;
public CoreHandler(Config config, Action<bool, string> update) public void Init(Config config, Action<bool, string> update)
{ {
_config = config; _config = config;
_updateFunc = update; _updateFunc = update;

View File

@ -7,16 +7,15 @@ namespace ServiceLib.Services
public class SpeedtestService public class SpeedtestService
{ {
private Config? _config; private Config? _config;
private CoreHandler _coreHandler;
private List<ServerTestItem> _selecteds; private List<ServerTestItem> _selecteds;
private ESpeedActionType _actionType; private ESpeedActionType _actionType;
private Action<SpeedTestResult> _updateFunc; private Action<SpeedTestResult> _updateFunc;
private bool _exitLoop = false; private bool _exitLoop = false;
public SpeedtestService(Config config, CoreHandler coreHandler, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> update) public SpeedtestService(Config config, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> update)
{ {
_config = config; _config = config;
_coreHandler = coreHandler;
_actionType = actionType; _actionType = actionType;
_updateFunc = update; _updateFunc = update;
@ -137,7 +136,7 @@ namespace ServiceLib.Services
{ {
string msg = string.Empty; string msg = string.Empty;
pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds); pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
@ -185,7 +184,7 @@ namespace ServiceLib.Services
{ {
if (pid > 0) if (pid > 0)
{ {
_coreHandler.CoreStopPid(pid); CoreHandler.Instance.CoreStopPid(pid);
} }
ProfileExHandler.Instance.SaveTo(); ProfileExHandler.Instance.SaveTo();
} }
@ -201,7 +200,7 @@ namespace ServiceLib.Services
// _selecteds = _selecteds.OrderBy(t => t.delay).ToList(); // _selecteds = _selecteds.OrderBy(t => t.delay).ToList();
//} //}
pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds); pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
@ -254,7 +253,7 @@ namespace ServiceLib.Services
if (pid > 0) if (pid > 0)
{ {
_coreHandler.CoreStopPid(pid); CoreHandler.Instance.CoreStopPid(pid);
} }
UpdateFunc("", ResUI.SpeedtestingCompleted); UpdateFunc("", ResUI.SpeedtestingCompleted);
ProfileExHandler.Instance.SaveTo(); ProfileExHandler.Instance.SaveTo();
@ -263,7 +262,7 @@ namespace ServiceLib.Services
private async Task RunSpeedTestMulti() private async Task RunSpeedTestMulti()
{ {
int pid = -1; int pid = -1;
pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds); pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0) if (pid < 0)
{ {
UpdateFunc("", ResUI.FailedToRunCore); UpdateFunc("", ResUI.FailedToRunCore);
@ -319,7 +318,7 @@ namespace ServiceLib.Services
if (pid > 0) if (pid > 0)
{ {
_coreHandler.CoreStopPid(pid); CoreHandler.Instance.CoreStopPid(pid);
} }
UpdateFunc("", ResUI.SpeedtestingCompleted); UpdateFunc("", ResUI.SpeedtestingCompleted);
ProfileExHandler.Instance.SaveTo(); ProfileExHandler.Instance.SaveTo();

View File

@ -13,7 +13,6 @@ namespace ServiceLib.ViewModels
{ {
#region private prop #region private prop
private CoreHandler _coreHandler;
private bool _isAdministrator { get; set; } private bool _isAdministrator { get; set; }
#endregion private prop #endregion private prop
@ -342,8 +341,7 @@ namespace ServiceLib.ViewModels
{ {
ConfigHandler.InitBuiltinRouting(_config); ConfigHandler.InitBuiltinRouting(_config);
ConfigHandler.InitBuiltinDNS(_config); ConfigHandler.InitBuiltinDNS(_config);
_coreHandler = new CoreHandler(_config, UpdateHandler); CoreHandler.Instance.Init(_config, UpdateHandler);
Locator.CurrentMutable.RegisterLazySingleton(() => _coreHandler, typeof(CoreHandler));
if (_config.guiItem.enableStatistics) if (_config.guiItem.enableStatistics)
{ {
@ -428,7 +426,7 @@ namespace ServiceLib.ViewModels
ProfileExHandler.Instance.SaveTo(); ProfileExHandler.Instance.SaveTo();
StatisticsHandler.Instance.SaveTo(); StatisticsHandler.Instance.SaveTo();
StatisticsHandler.Instance.Close(); StatisticsHandler.Instance.Close();
_coreHandler.CoreStop(); CoreHandler.Instance.CoreStop();
Logging.SaveLog("MyAppExit End"); Logging.SaveLog("MyAppExit End");
} }
@ -750,7 +748,7 @@ namespace ServiceLib.ViewModels
//} //}
var node = ConfigHandler.GetDefaultServer(_config); var node = ConfigHandler.GetDefaultServer(_config);
_coreHandler.LoadCore(node); CoreHandler.Instance.LoadCore(node);
}); });
} }
@ -760,7 +758,7 @@ namespace ServiceLib.ViewModels
ChangeSystemProxyStatusAsync(ESysProxyType.ForcedClear, false); ChangeSystemProxyStatusAsync(ESysProxyType.ForcedClear, false);
_coreHandler.CoreStop(); CoreHandler.Instance.CoreStop();
} }
#endregion core job #endregion core job

View File

@ -667,11 +667,8 @@ namespace ServiceLib.ViewModels
return; return;
} }
//ClearTestResult(); //ClearTestResult();
var coreHandler = Locator.Current.GetService<CoreHandler>();
if (coreHandler != null) _speedtestHandler = new SpeedtestService(_config, lstSelecteds, actionType, UpdateSpeedtestHandler);
{
_speedtestHandler = new SpeedtestService(_config, coreHandler, lstSelecteds, actionType, UpdateSpeedtestHandler);
}
} }
public void ServerSpeedtestStop() public void ServerSpeedtestStop()