Skip to content

Commit

Permalink
Improved Action Invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 8, 2024
1 parent 3e74bb6 commit 53e19ec
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 126 deletions.
23 changes: 5 additions & 18 deletions v2rayN/ServiceLib/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static List<string> String2List(string str)
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
return new List<string>();
return [];
}
}

Expand All @@ -131,7 +131,7 @@ public static List<string> String2ListSorted(string str)
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
return new List<string>();
return [];
}
}

Expand Down Expand Up @@ -640,29 +640,16 @@ public static string GetDownloadFileName(string url)
return fileName;
}

public static IPAddress? GetDefaultGateway()
{
return NetworkInterface
.GetAllNetworkInterfaces()
.Where(n => n.OperationalStatus == OperationalStatus.Up)
.Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.SelectMany(n => n.GetIPProperties()?.GatewayAddresses)
.Select(g => g?.Address)
.Where(a => a != null)
// .Where(a => a.AddressFamily == AddressFamily.InterNetwork)
// .Where(a => Array.FindIndex(a.GetAddressBytes(), b => b != 0) >= 0)
.FirstOrDefault();
}

public static bool IsGuidByParse(string strSrc)
{
return Guid.TryParse(strSrc, out Guid g);
return Guid.TryParse(strSrc, out _);
}

public static void ProcessStart(string fileName, string arguments = "")
public static void ProcessStart(string? fileName, string arguments = "")
{
try
{
if (fileName.IsNullOrEmpty()) { return; }
Process.Start(new ProcessStartInfo(fileName, arguments) { UseShellExecute = true });
}
catch (Exception ex)
Expand Down
24 changes: 12 additions & 12 deletions v2rayN/ServiceLib/Handler/ClashApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public sealed class ClashApiHandler
private Dictionary<String, ProxiesItem>? _proxies;
public Dictionary<string, object> ProfileContent { get; set; }

public void GetClashProxies(Config config, Action<ClashProxies, ClashProviders> update)
public void GetClashProxies(Config config, Action<ClashProxies, ClashProviders> updateFunc)
{
Task.Run(() => GetClashProxiesAsync(config, update));
Task.Run(() => GetClashProxiesAsync(config, updateFunc));
}

private async Task GetClashProxiesAsync(Config config, Action<ClashProxies, ClashProviders> update)
private async Task GetClashProxiesAsync(Config config, Action<ClashProxies, ClashProviders> updateFunc)
{
for (var i = 0; i < 5; i++)
{
Expand All @@ -30,15 +30,15 @@ private async Task GetClashProxiesAsync(Config config, Action<ClashProxies, Clas
if (clashProxies != null || clashProviders != null)
{
_proxies = clashProxies?.proxies;
update(clashProxies, clashProviders);
updateFunc?.Invoke(clashProxies, clashProviders);
return;
}
Task.Delay(5000).Wait();
}
update(null, null);
updateFunc?.Invoke(null, null);
}

public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Action<ClashProxyModel?, string> update)
public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Action<ClashProxyModel?, string> updateFunc)
{
Task.Run(() =>
{
Expand Down Expand Up @@ -90,13 +90,13 @@ public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Ac
tasks.Add(Task.Run(async () =>
{
var result = await HttpClientHelper.Instance.TryGetAsync(url);
update(it, result);
updateFunc?.Invoke(it, result);
}));
}
Task.WaitAll(tasks.ToArray());
Task.Delay(1000).Wait();
update(null, "");
updateFunc?.Invoke(null, "");
});
}

Expand Down Expand Up @@ -164,20 +164,20 @@ public async void ClashConfigReload(string filePath)
}
}

public void GetClashConnections(Config config, Action<ClashConnections> update)
public void GetClashConnections(Config config, Action<ClashConnections> updateFunc)
{
Task.Run(() => GetClashConnectionsAsync(config, update));
Task.Run(() => GetClashConnectionsAsync(config, updateFunc));
}

private async Task GetClashConnectionsAsync(Config config, Action<ClashConnections> update)
private async Task GetClashConnectionsAsync(Config config, Action<ClashConnections> updateFunc)
{
try
{
var url = $"{GetApiUrl()}/connections";
var result = await HttpClientHelper.Instance.TryGetAsync(url);
var clashConnections = JsonUtils.Deserialize<ClashConnections>(result);

update(clashConnections);
updateFunc?.Invoke(clashConnections);
}
catch (Exception ex)
{
Expand Down
8 changes: 4 additions & 4 deletions v2rayN/ServiceLib/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public class CoreHandler
private Config _config;
private Process? _process;
private Process? _processPre;
private Action<bool, string> _updateFunc;
private Action<bool, string>? _updateFunc;

public void Init(Config config, Action<bool, string> update)
public void Init(Config config, Action<bool, string> updateFunc)
{
_config = config;
_updateFunc = update;
_updateFunc = updateFunc;

Environment.SetEnvironmentVariable("v2ray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("xray.location.asset", Utils.GetBinPath(""), EnvironmentVariableTarget.Process);
Expand Down Expand Up @@ -267,7 +267,7 @@ private int CoreStartSpeedtest(string configPath, ECoreType coreType)

private void ShowMsg(bool notify, string msg)
{
_updateFunc(notify, msg);
_updateFunc?.Invoke(notify, msg);
}

#endregion Private
Expand Down
8 changes: 4 additions & 4 deletions v2rayN/ServiceLib/Handler/StatisticsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ public class StatisticsHandler
private Config _config;
private ServerStatItem? _serverStatItem;
private List<ServerStatItem> _lstServerStat;
private Action<ServerSpeedItem> _updateFunc;
private Action<ServerSpeedItem>? _updateFunc;
private StatisticsV2rayService? _statisticsV2Ray;
private StatisticsSingboxService? _statisticsSingbox;

public List<ServerStatItem> ServerStat => _lstServerStat;

public void Init(Config config, Action<ServerSpeedItem> update)
public void Init(Config config, Action<ServerSpeedItem> updateFunc)
{
_config = config;
_updateFunc = update;
_updateFunc = updateFunc;
if (!config.guiItem.enableStatistics)
{
return;
Expand Down Expand Up @@ -95,7 +95,7 @@ private void UpdateServerStat(ServerSpeedItem server)
server.todayDown = _serverStatItem.todayDown;
server.totalUp = _serverStatItem.totalUp;
server.totalDown = _serverStatItem.totalDown;
_updateFunc(server);
_updateFunc?.Invoke(server);
}

private void GetServerStatItem(string indexId)
Expand Down
14 changes: 7 additions & 7 deletions v2rayN/ServiceLib/Handler/TaskHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public TaskHandler()
{
}

public void RegUpdateTask(Config config, Action<bool, string> update)
public void RegUpdateTask(Config config, Action<bool, string> updateFunc)
{
Task.Run(() => UpdateTaskRunSubscription(config, update));
Task.Run(() => UpdateTaskRunGeo(config, update));
Task.Run(() => UpdateTaskRunSubscription(config, updateFunc));
Task.Run(() => UpdateTaskRunGeo(config, updateFunc));
}

private async Task UpdateTaskRunSubscription(Config config, Action<bool, string> update)
private async Task UpdateTaskRunSubscription(Config config, Action<bool, string> updateFunc)
{
await Task.Delay(60000);
Logging.SaveLog("UpdateTaskRunSubscription");
Expand All @@ -33,7 +33,7 @@ private async Task UpdateTaskRunSubscription(Config config, Action<bool, string>
{
updateHandle.UpdateSubscriptionProcess(config, item.id, true, (bool success, string msg) =>
{
update(success, msg);
updateFunc?.Invoke(success, msg);
if (success)
Logging.SaveLog("subscription" + msg);
});
Expand All @@ -46,7 +46,7 @@ private async Task UpdateTaskRunSubscription(Config config, Action<bool, string>
}
}

private async Task UpdateTaskRunGeo(Config config, Action<bool, string> update)
private async Task UpdateTaskRunGeo(Config config, Action<bool, string> updateFunc)
{
var autoUpdateGeoTime = DateTime.Now;

Expand All @@ -65,7 +65,7 @@ private async Task UpdateTaskRunGeo(Config config, Action<bool, string> update)
{
await updateHandle.UpdateGeoFileAll(config, (bool success, string msg) =>
{
update(false, msg);
updateFunc?.Invoke(false, msg);
});
autoUpdateGeoTime = dtNow;
}
Expand Down
4 changes: 2 additions & 2 deletions v2rayN/ServiceLib/ServiceLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Downloader" Version="3.2.0" />
<PackageReference Include="Downloader" Version="3.2.1" />
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
<PackageReference Include="ReactiveUI" Version="20.1.63" />
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
<PackageReference Include="Splat.NLog" Version="15.2.22" />
<PackageReference Include="WebDav.Client" Version="2.8.0" />
<PackageReference Include="YamlDotNet" Version="16.1.2" />
<PackageReference Include="YamlDotNet" Version="16.1.3" />
<PackageReference Include="QRCoder" Version="1.6.0" />
<PackageReference Include="CliWrap" Version="3.6.6" />
</ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions v2rayN/ServiceLib/Services/DownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ResultEventArgs(bool success, string msg)
}
}

public async Task<int> DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Action<bool, string> update)
public async Task<int> DownloadDataAsync(string url, WebProxy webProxy, int downloadTimeout, Action<bool, string> updateFunc)
{
try
{
Expand All @@ -35,10 +35,10 @@ public async Task<int> DownloadDataAsync(string url, WebProxy webProxy, int down
var progress = new Progress<string>();
progress.ProgressChanged += (sender, value) =>
{
if (update != null)
if (updateFunc != null)
{
string msg = $"{value}";
update(false, msg);
updateFunc?.Invoke(false, msg);
}
};

Expand All @@ -49,10 +49,10 @@ await DownloaderHelper.Instance.DownloadDataAsync4Speed(webProxy,
}
catch (Exception ex)
{
update(false, ex.Message);
updateFunc?.Invoke(false, ex.Message);
if (ex.InnerException != null)
{
update(false, ex.InnerException.Message);
updateFunc?.Invoke(false, ex.InnerException.Message);
}
}
return 0;
Expand Down
8 changes: 4 additions & 4 deletions v2rayN/ServiceLib/Services/SpeedtestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ public class SpeedtestService
private Config? _config;
private List<ServerTestItem> _selecteds;
private ESpeedActionType _actionType;
private Action<SpeedTestResult> _updateFunc;
private Action<SpeedTestResult>? _updateFunc;
private bool _exitLoop = false;

public SpeedtestService(Config config, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> update)
public SpeedtestService(Config config, List<ProfileItem> selecteds, ESpeedActionType actionType, Action<SpeedTestResult> updateFunc)
{
_config = config;

_actionType = actionType;
_updateFunc = update;
_updateFunc = updateFunc;

_selecteds = new List<ServerTestItem>();
foreach (var it in selecteds)
Expand Down Expand Up @@ -383,7 +383,7 @@ private string FormatOut(object time, string unit)

private void UpdateFunc(string indexId, string delay, string speed = "")
{
_updateFunc(new() { IndexId = indexId, Delay = delay, Speed = speed });
_updateFunc?.Invoke(new() { IndexId = indexId, Delay = delay, Speed = speed });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public class StatisticsSingboxService
private bool _exitFlag;
private ClientWebSocket? webSocket;
private string url = string.Empty;
private Action<ServerSpeedItem> _updateFunc;
private Action<ServerSpeedItem>? _updateFunc;

public StatisticsSingboxService(Config config, Action<ServerSpeedItem> update)
public StatisticsSingboxService(Config config, Action<ServerSpeedItem> updateFunc)
{
_config = config;
_updateFunc = update;
_updateFunc = updateFunc;
_exitFlag = false;

Task.Run(() => Run());
Expand Down Expand Up @@ -92,7 +92,7 @@ private async void Run()
{
ParseOutput(result, out ulong up, out ulong down);

_updateFunc(new ServerSpeedItem()
_updateFunc?.Invoke(new ServerSpeedItem()
{
proxyUp = (long)(up / 1000),
proxyDown = (long)(down / 1000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class StatisticsV2rayService
private GrpcChannel? _channel;
private StatsService.StatsServiceClient? _client;
private bool _exitFlag;
private Action<ServerSpeedItem> _updateFunc;
private Action<ServerSpeedItem>? _updateFunc;

public StatisticsV2rayService(Models.Config config, Action<ServerSpeedItem> update)
public StatisticsV2rayService(Models.Config config, Action<ServerSpeedItem> updateFunc)
{
_config = config;
_updateFunc = update;
_updateFunc = updateFunc;
_exitFlag = false;

GrpcInit();
Expand Down Expand Up @@ -70,7 +70,7 @@ private async void Run()
if (res != null)
{
ParseOutput(res.Stat, out ServerSpeedItem server);
_updateFunc(server);
_updateFunc?.Invoke(server);
}
}
if (_channel != null)
Expand Down
Loading

0 comments on commit 53e19ec

Please sign in to comment.