Skip to content

Commit

Permalink
Refactor CoreHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 7, 2024
1 parent 4ccc7aa commit 7a83906
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
4 changes: 3 additions & 1 deletion v2rayN/ServiceLib/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ namespace ServiceLib.Handler
/// </summary>
public class CoreHandler
{
private static readonly Lazy<CoreHandler> _instance = new(() => new());
public static CoreHandler Instance => _instance.Value;
private Config _config;
private Process? _process;
private Process? _processPre;
private Action<bool, string> _updateFunc;

public CoreHandler(Config config, Action<bool, string> update)
public void Init(Config config, Action<bool, string> update)
{
_config = config;
_updateFunc = update;
Expand Down
17 changes: 8 additions & 9 deletions v2rayN/ServiceLib/Services/SpeedtestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ namespace ServiceLib.Services
public class SpeedtestService
{
private Config? _config;
private CoreHandler _coreHandler;
private List<ServerTestItem> _selecteds;
private ESpeedActionType _actionType;
private Action<SpeedTestResult> _updateFunc;
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;
_coreHandler = coreHandler;

_actionType = actionType;
_updateFunc = update;

Expand Down Expand Up @@ -137,7 +136,7 @@ private Task RunRealPing()
{
string msg = string.Empty;

pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds);
pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0)
{
UpdateFunc("", ResUI.FailedToRunCore);
Expand Down Expand Up @@ -185,7 +184,7 @@ private Task RunRealPing()
{
if (pid > 0)
{
_coreHandler.CoreStopPid(pid);
CoreHandler.Instance.CoreStopPid(pid);
}
ProfileExHandler.Instance.SaveTo();
}
Expand All @@ -201,7 +200,7 @@ private async Task RunSpeedTestAsync()
// _selecteds = _selecteds.OrderBy(t => t.delay).ToList();
//}

pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds);
pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0)
{
UpdateFunc("", ResUI.FailedToRunCore);
Expand Down Expand Up @@ -254,7 +253,7 @@ await downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) =>

if (pid > 0)
{
_coreHandler.CoreStopPid(pid);
CoreHandler.Instance.CoreStopPid(pid);
}
UpdateFunc("", ResUI.SpeedtestingCompleted);
ProfileExHandler.Instance.SaveTo();
Expand All @@ -263,7 +262,7 @@ await downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) =>
private async Task RunSpeedTestMulti()
{
int pid = -1;
pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds);
pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
if (pid < 0)
{
UpdateFunc("", ResUI.FailedToRunCore);
Expand Down Expand Up @@ -319,7 +318,7 @@ private async Task RunSpeedTestMulti()

if (pid > 0)
{
_coreHandler.CoreStopPid(pid);
CoreHandler.Instance.CoreStopPid(pid);
}
UpdateFunc("", ResUI.SpeedtestingCompleted);
ProfileExHandler.Instance.SaveTo();
Expand Down
10 changes: 4 additions & 6 deletions v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class MainWindowViewModel : MyReactiveObject
{
#region private prop

private CoreHandler _coreHandler;
private bool _isAdministrator { get; set; }

#endregion private prop
Expand Down Expand Up @@ -342,8 +341,7 @@ private void Init()
{
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)
{
Expand Down Expand Up @@ -428,7 +426,7 @@ public async Task MyAppExitAsync(bool blWindowsShutDown)
ProfileExHandler.Instance.SaveTo();
StatisticsHandler.Instance.SaveTo();
StatisticsHandler.Instance.Close();
_coreHandler.CoreStop();
CoreHandler.Instance.CoreStop();

Logging.SaveLog("MyAppExit End");
}
Expand Down Expand Up @@ -750,7 +748,7 @@ await Task.Run(() =>
//}
var node = ConfigHandler.GetDefaultServer(_config);
_coreHandler.LoadCore(node);
CoreHandler.Instance.LoadCore(node);
});
}

Expand All @@ -760,7 +758,7 @@ public void CloseCore()

ChangeSystemProxyStatusAsync(ESysProxyType.ForcedClear, false);

_coreHandler.CoreStop();
CoreHandler.Instance.CoreStop();
}

#endregion core job
Expand Down
7 changes: 2 additions & 5 deletions v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,8 @@ public void ServerSpeedtest(ESpeedActionType actionType)
return;
}
//ClearTestResult();
var coreHandler = Locator.Current.GetService<CoreHandler>();
if (coreHandler != null)
{
_speedtestHandler = new SpeedtestService(_config, coreHandler, lstSelecteds, actionType, UpdateSpeedtestHandler);
}

_speedtestHandler = new SpeedtestService(_config, lstSelecteds, actionType, UpdateSpeedtestHandler);
}

public void ServerSpeedtestStop()
Expand Down

0 comments on commit 7a83906

Please sign in to comment.