Skip to content

Commit 7a83906

Browse files
committed
Refactor CoreHandler
1 parent 4ccc7aa commit 7a83906

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

v2rayN/ServiceLib/Handler/CoreHandler.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ namespace ServiceLib.Handler
88
/// </summary>
99
public class CoreHandler
1010
{
11+
private static readonly Lazy<CoreHandler> _instance = new(() => new());
12+
public static CoreHandler Instance => _instance.Value;
1113
private Config _config;
1214
private Process? _process;
1315
private Process? _processPre;
1416
private Action<bool, string> _updateFunc;
1517

16-
public CoreHandler(Config config, Action<bool, string> update)
18+
public void Init(Config config, Action<bool, string> update)
1719
{
1820
_config = config;
1921
_updateFunc = update;

v2rayN/ServiceLib/Services/SpeedtestService.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ namespace ServiceLib.Services
77
public class SpeedtestService
88
{
99
private Config? _config;
10-
private CoreHandler _coreHandler;
1110
private List<ServerTestItem> _selecteds;
1211
private ESpeedActionType _actionType;
1312
private Action<SpeedTestResult> _updateFunc;
1413
private bool _exitLoop = false;
1514

16-
public SpeedtestService(Config config, CoreHandler coreHandler, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> update)
15+
public SpeedtestService(Config config, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> update)
1716
{
1817
_config = config;
19-
_coreHandler = coreHandler;
18+
2019
_actionType = actionType;
2120
_updateFunc = update;
2221

@@ -137,7 +136,7 @@ private Task RunRealPing()
137136
{
138137
string msg = string.Empty;
139138

140-
pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds);
139+
pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
141140
if (pid < 0)
142141
{
143142
UpdateFunc("", ResUI.FailedToRunCore);
@@ -185,7 +184,7 @@ private Task RunRealPing()
185184
{
186185
if (pid > 0)
187186
{
188-
_coreHandler.CoreStopPid(pid);
187+
CoreHandler.Instance.CoreStopPid(pid);
189188
}
190189
ProfileExHandler.Instance.SaveTo();
191190
}
@@ -201,7 +200,7 @@ private async Task RunSpeedTestAsync()
201200
// _selecteds = _selecteds.OrderBy(t => t.delay).ToList();
202201
//}
203202

204-
pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds);
203+
pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
205204
if (pid < 0)
206205
{
207206
UpdateFunc("", ResUI.FailedToRunCore);
@@ -254,7 +253,7 @@ await downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) =>
254253

255254
if (pid > 0)
256255
{
257-
_coreHandler.CoreStopPid(pid);
256+
CoreHandler.Instance.CoreStopPid(pid);
258257
}
259258
UpdateFunc("", ResUI.SpeedtestingCompleted);
260259
ProfileExHandler.Instance.SaveTo();
@@ -263,7 +262,7 @@ await downloadHandle.DownloadDataAsync(url, webProxy, timeout, (success, msg) =>
263262
private async Task RunSpeedTestMulti()
264263
{
265264
int pid = -1;
266-
pid = _coreHandler.LoadCoreConfigSpeedtest(_selecteds);
265+
pid = CoreHandler.Instance.LoadCoreConfigSpeedtest(_selecteds);
267266
if (pid < 0)
268267
{
269268
UpdateFunc("", ResUI.FailedToRunCore);
@@ -319,7 +318,7 @@ private async Task RunSpeedTestMulti()
319318

320319
if (pid > 0)
321320
{
322-
_coreHandler.CoreStopPid(pid);
321+
CoreHandler.Instance.CoreStopPid(pid);
323322
}
324323
UpdateFunc("", ResUI.SpeedtestingCompleted);
325324
ProfileExHandler.Instance.SaveTo();

v2rayN/ServiceLib/ViewModels/MainWindowViewModel.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class MainWindowViewModel : MyReactiveObject
1313
{
1414
#region private prop
1515

16-
private CoreHandler _coreHandler;
1716
private bool _isAdministrator { get; set; }
1817

1918
#endregion private prop
@@ -342,8 +341,7 @@ private void Init()
342341
{
343342
ConfigHandler.InitBuiltinRouting(_config);
344343
ConfigHandler.InitBuiltinDNS(_config);
345-
_coreHandler = new CoreHandler(_config, UpdateHandler);
346-
Locator.CurrentMutable.RegisterLazySingleton(() => _coreHandler, typeof(CoreHandler));
344+
CoreHandler.Instance.Init(_config, UpdateHandler);
347345

348346
if (_config.guiItem.enableStatistics)
349347
{
@@ -428,7 +426,7 @@ public async Task MyAppExitAsync(bool blWindowsShutDown)
428426
ProfileExHandler.Instance.SaveTo();
429427
StatisticsHandler.Instance.SaveTo();
430428
StatisticsHandler.Instance.Close();
431-
_coreHandler.CoreStop();
429+
CoreHandler.Instance.CoreStop();
432430

433431
Logging.SaveLog("MyAppExit End");
434432
}
@@ -750,7 +748,7 @@ await Task.Run(() =>
750748
//}
751749

752750
var node = ConfigHandler.GetDefaultServer(_config);
753-
_coreHandler.LoadCore(node);
751+
CoreHandler.Instance.LoadCore(node);
754752
});
755753
}
756754

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

761759
ChangeSystemProxyStatusAsync(ESysProxyType.ForcedClear, false);
762760

763-
_coreHandler.CoreStop();
761+
CoreHandler.Instance.CoreStop();
764762
}
765763

766764
#endregion core job

v2rayN/ServiceLib/ViewModels/ProfilesViewModel.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,8 @@ public void ServerSpeedtest(ESpeedActionType actionType)
667667
return;
668668
}
669669
//ClearTestResult();
670-
var coreHandler = Locator.Current.GetService<CoreHandler>();
671-
if (coreHandler != null)
672-
{
673-
_speedtestHandler = new SpeedtestService(_config, coreHandler, lstSelecteds, actionType, UpdateSpeedtestHandler);
674-
}
670+
671+
_speedtestHandler = new SpeedtestService(_config, lstSelecteds, actionType, UpdateSpeedtestHandler);
675672
}
676673

677674
public void ServerSpeedtestStop()

0 commit comments

Comments
 (0)