优化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 v2rayN.Models;
using v2rayN.Resx;
using Windows.ApplicationModel.Store.LicenseManagement;
namespace v2rayN.Handler
{
@ -16,61 +17,57 @@ namespace v2rayN.Handler
private Process? _process;
private Process? _processPre;
private Action<bool, string> _updateFunc;
private bool isLoading;
private List<AutoResetEvent> loadResetEvents;
public CoreHandler(Config config, Action<bool, string> update)
{
_config = config;
_updateFunc = update;
loadResetEvents = new();
Environment.SetEnvironmentVariable("v2ray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("xray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process);
}
public void LoadCore()
public void LoadCore(bool isForce = false)
{
var node = ConfigHandler.GetDefaultServer(_config);
if (node == null)
List<AutoResetEvent> executingResetEvents;
lock (loadResetEvents)
{
ShowMsg(false, ResUI.CheckServerSettings);
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);
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);
}
else
if(result == 0)
{
ShowMsg(false, msg);
ShowMsg(true, $"{node.GetSummary()}");
ShowMsg(true, $"{node!.GetSummary()}");
CoreStop();
if (_config.tunModeItem.enableTun)
{
Thread.Sleep(1000);
Utils.RemoveTunDevice();
CoreStart(node);
}
executingResetEvents.ForEach(result => result.Set());
lock(loadResetEvents)
{
if(loadResetEvents.Count > 0)
{
Task.Run(() => LoadCore(true));
return;
}
isLoading = false;
}
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");
}
}
});
}
}
}
public int LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)