Skip to content

Commit

Permalink
Optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Aug 18, 2024
1 parent a432852 commit bbe7c7b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 50 deletions.
29 changes: 9 additions & 20 deletions v2rayN/v2rayN/Handler/ClashApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,9 @@ public sealed class ClashApiHandler
private static readonly Lazy<ClashApiHandler> instance = new(() => new());
public static ClashApiHandler Instance => instance.Value;

private Dictionary<String, ProxiesItem> _proxies;
private Dictionary<String, ProxiesItem>? _proxies;
public Dictionary<string, object> ProfileContent { get; set; }

public void SetProxies(Dictionary<String, ProxiesItem> proxies)
{
_proxies = proxies;
}

public Dictionary<String, ProxiesItem> GetProxies()
{
return _proxies;
}

public void GetClashProxies(Config config, Action<ClashProxies, ClashProviders> update)
{
Task.Run(() => GetClashProxiesAsync(config, update));
Expand All @@ -40,10 +30,11 @@ private async Task GetClashProxiesAsync(Config config, Action<ClashProxies, Clas

if (clashProxies != null || clashProviders != null)
{
_proxies = clashProxies?.proxies;
update(clashProxies, clashProviders);
return;
}
Thread.Sleep(5000);
Task.Delay(5000).Wait();
}
update(null, null);
}
Expand All @@ -56,19 +47,18 @@ public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Ac
{
for (int i = 0; i < 5; i++)
{
if (GetProxies() != null)
if (_proxies != null)
{
break;
}
Thread.Sleep(5000);
Task.Delay(5000).Wait();
}
var proxies = GetProxies();
if (proxies == null)
if (_proxies == null)
{
return;
}
lstProxy = new List<ClashProxyModel>();
foreach (KeyValuePair<string, ProxiesItem> kv in proxies)
foreach (KeyValuePair<string, ProxiesItem> kv in _proxies)
{
if (Global.notAllowTestType.Contains(kv.Value.type.ToLower()))
{
Expand Down Expand Up @@ -106,7 +96,7 @@ public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Ac
}
Task.WaitAll(tasks.ToArray());
Thread.Sleep(1000);
Task.Delay(1000).Wait();
update(null, "");
});
}
Expand Down Expand Up @@ -148,8 +138,7 @@ public void ClashConfigUpdate(Dictionary<string, string> headers)
{
Task.Run(async () =>
{
var proxies = GetProxies();
if (proxies == null)
if (_proxies == null)
{
return;
}
Expand Down
6 changes: 0 additions & 6 deletions v2rayN/v2rayN/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ public void LoadCore(ProfileItem? node)
ShowMsg(false, msg);
ShowMsg(true, $"{node.GetSummary()}");
CoreStop();
//if (_config.tunModeItem.enableTun)
//{
// Thread.Sleep(1000);
// WindowsUtils.RemoveTunDevice();
//}

CoreStart(node);

//In tun mode, do a delay check and restart the core
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/ViewModels/ClashConnectionsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void Init()
GetClashConnections();
lastTime = dtNow;
}
Thread.Sleep(1000);
Task.Delay(1000).Wait();
}
});
}
Expand Down
39 changes: 19 additions & 20 deletions v2rayN/v2rayN/ViewModels/ClashProxiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace v2rayN.ViewModels
{
public class ClashProxiesViewModel : MyReactiveObject
{
private Dictionary<String, ProxiesItem>? proxies;
private Dictionary<String, ProvidersItem>? providers;
private int delayTimeout = 99999999;
private Dictionary<String, ProxiesItem>? _proxies;
private Dictionary<String, ProvidersItem>? _providers;
private int _delayTimeout = 99999999;

private IObservableCollection<ClashProxyModel> _proxyGroups = new ObservableCollectionExtended<ClashProxyModel>();
private IObservableCollection<ClashProxyModel> _proxyDetails = new ObservableCollectionExtended<ClashProxyModel>();
Expand Down Expand Up @@ -175,11 +175,10 @@ private void GetClashProxies(bool refreshUI)
ClashApiHandler.Instance.GetClashProxies(_config, (it, it2) =>
{
//UpdateHandler(false, "Refresh Clash Proxies");
proxies = it?.proxies;
providers = it2?.providers;
_proxies = it?.proxies;
_providers = it2?.providers;
ClashApiHandler.Instance.SetProxies(proxies);
if (proxies == null)
if (_proxies == null)
{
return;
}
Expand All @@ -200,11 +199,11 @@ public void RefreshProxyGroups()
{
foreach (var it in proxyGroups)
{
if (string.IsNullOrEmpty(it.name) || !proxies.ContainsKey(it.name))
if (string.IsNullOrEmpty(it.name) || !_proxies.ContainsKey(it.name))
{
continue;
}
var item = proxies[it.name];
var item = _proxies[it.name];
if (!Global.allowSelectType.Contains(item.type.ToLower()))
{
continue;
Expand All @@ -219,7 +218,7 @@ public void RefreshProxyGroups()
}

//from api
foreach (KeyValuePair<string, ProxiesItem> kv in proxies)
foreach (KeyValuePair<string, ProxiesItem> kv in _proxies)
{
if (!Global.allowSelectType.Contains(kv.Value.type.ToLower()))
{
Expand Down Expand Up @@ -267,12 +266,12 @@ private void RefreshProxyDetails(bool c)
{
return;
}
if (proxies == null)
if (_proxies == null)
{
return;
}

proxies.TryGetValue(name, out ProxiesItem proxy);
_proxies.TryGetValue(name, out ProxiesItem proxy);
if (proxy == null || proxy.all == null)
{
return;
Expand All @@ -298,7 +297,7 @@ private void RefreshProxyDetails(bool c)
isActive = isActive,
name = item,
type = proxy2.type,
delay = delay <= 0 ? delayTimeout : delay,
delay = delay <= 0 ? _delayTimeout : delay,
delayName = delay <= 0 ? string.Empty : $"{delay}ms",
});
}
Expand All @@ -321,17 +320,17 @@ private void RefreshProxyDetails(bool c)

private ProxiesItem? TryGetProxy(string name)
{
if (proxies is null)
if (_proxies is null)
return null;
proxies.TryGetValue(name, out ProxiesItem proxy2);
_proxies.TryGetValue(name, out ProxiesItem proxy2);
if (proxy2 != null)
{
return proxy2;
}
//from providers
if (providers != null)
if (_providers != null)
{
foreach (KeyValuePair<string, ProvidersItem> kv in providers)
foreach (KeyValuePair<string, ProvidersItem> kv in _providers)
{
if (Global.proxyVehicleType.Contains(kv.Value.vehicleType.ToLower()))
{
Expand Down Expand Up @@ -424,12 +423,12 @@ public void ProxiesDelayTestResult(SpeedTestResult result)
}
else if (dicResult != null && dicResult.ContainsKey("message"))
{
detail.delay = delayTimeout;
detail.delay = _delayTimeout;
detail.delayName = $"{dicResult["message"]}";
}
else
{
detail.delay = delayTimeout;
detail.delay = _delayTimeout;
detail.delayName = String.Empty;
}
_proxyDetails.Replace(detail, JsonUtils.DeepCopy(detail));
Expand Down Expand Up @@ -459,7 +458,7 @@ public void DelayTestTask()
ProxiesDelayTest();
lastTime = dtNow;
}
Thread.Sleep(1000);
Task.Delay(1000).Wait();
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,8 @@ await Task.Run(() =>
{
if (_config.tunModeItem.enableTun)
{
Thread.Sleep(1000);
//WindowsUtils.RemoveTunDevice();
Task.Delay(1000).Wait();
WindowsUtils.RemoveTunDevice();
}
var node = ConfigHandler.GetDefaultServer(_config);
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/v2rayN/v2rayN.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ItemGroup>
<PackageReference Include="Downloader" Version="3.1.2" />
<PackageReference Include="MaterialDesignThemes" Version="5.1.0" />
<PackageReference Include="H.NotifyIcon.Wpf" Version="2.1.0" />
<PackageReference Include="H.NotifyIcon.Wpf" Version="2.1.2" />
<PackageReference Include="QRCoder.Xaml" Version="1.6.0" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
<PackageReference Include="TaskScheduler" Version="2.11.0" />
Expand Down

0 comments on commit bbe7c7b

Please sign in to comment.