优化LoadCore()以适应多线程环境

This commit is contained in:
chao wan 2024-04-03 00:12:48 +08:00
parent ee3159b00e
commit abe3f64901

View file

@ -4,6 +4,7 @@ using System.Reactive.Linq;
using System.Text; using System.Text;
using v2rayN.Models; using v2rayN.Models;
using v2rayN.Resx; using v2rayN.Resx;
using Windows.ApplicationModel.Store.LicenseManagement;
namespace v2rayN.Handler namespace v2rayN.Handler
{ {
@ -16,61 +17,57 @@ namespace v2rayN.Handler
private Process? _process; private Process? _process;
private Process? _processPre; private Process? _processPre;
private Action<bool, string> _updateFunc; private Action<bool, string> _updateFunc;
private bool isLoading;
private List<AutoResetEvent> loadResetEvents;
public CoreHandler(Config config, Action<bool, string> update) public CoreHandler(Config config, Action<bool, string> update)
{ {
_config = config; _config = config;
_updateFunc = update; _updateFunc = update;
loadResetEvents = new();
Environment.SetEnvironmentVariable("v2ray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("v2ray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("xray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("xray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process);
} }
public void LoadCore() public void LoadCore(bool isForce = false)
{ {
List<AutoResetEvent> executingResetEvents;
lock (loadResetEvents)
{
if (isForce && isLoading)
{
var loadResetEvent = new AutoResetEvent(false);
lock (loadResetEvents) loadResetEvents.Add(loadResetEvent);
loadResetEvent.WaitOne();
return;
}
isLoading = true;
executingResetEvents = new(loadResetEvents);
loadResetEvents.Clear();
}
var node = ConfigHandler.GetDefaultServer(_config); var node = ConfigHandler.GetDefaultServer(_config);
if (node == null)
{
ShowMsg(false, ResUI.CheckServerSettings);
return;
}
string fileName = Utils.GetConfigPath(Global.CoreConfigFileName); string fileName = Utils.GetConfigPath(Global.CoreConfigFileName);
if (CoreConfigHandler.GenerateClientConfig(node, fileName, out string msg, out string content) != 0) var result = CoreConfigHandler.GenerateClientConfig(node!, fileName, out string msg, out string content);
ShowMsg(false, msg);
if(result == 0)
{ {
ShowMsg(false, msg); ShowMsg(true, $"{node!.GetSummary()}");
}
else
{
ShowMsg(false, msg);
ShowMsg(true, $"{node.GetSummary()}");
CoreStop(); CoreStop();
if (_config.tunModeItem.enableTun) Utils.RemoveTunDevice();
{
Thread.Sleep(1000);
Utils.RemoveTunDevice();
}
CoreStart(node); CoreStart(node);
//In tun mode, do a delay check and restart the core
if (_config.tunModeItem.enableTun)
{
Observable.Range(1, 1)
.Delay(TimeSpan.FromSeconds(15))
.Subscribe(x =>
{
{
if (_process == null || _process.HasExited)
{
CoreStart(node);
ShowMsg(false, "Tun mode restart the core once");
Logging.SaveLog("Tun mode restart the core once");
}
}
});
}
} }
executingResetEvents.ForEach(result => result.Set());
lock(loadResetEvents)
{
if(loadResetEvents.Count > 0)
{
Task.Run(() => LoadCore(true));
return;
}
isLoading = false;
}
} }
public int LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds) public int LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)