Skip to content

Commit

Permalink
Code optimization, function asynchrony
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 20, 2024
1 parent 50449df commit 394b657
Show file tree
Hide file tree
Showing 29 changed files with 440 additions and 499 deletions.
65 changes: 12 additions & 53 deletions v2rayN/ServiceLib/Common/SqliteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public sealed class SQLiteHelper
private string _connstr;
private SQLiteConnection _db;
private SQLiteAsyncConnection _dbAsync;
private static readonly object objLock = new();
private readonly string _configDB = "guiNDB.db";

public SQLiteHelper()
Expand All @@ -25,94 +24,54 @@ public CreateTableResult CreateTable<T>()
return _db.CreateTable<T>();
}

public int Insert(object model)
public async Task<int> InsertAllAsync(IEnumerable models)
{
return _db.Insert(model);
}

public int InsertAll(IEnumerable models)
{
lock (objLock)
{
return _db.InsertAll(models);
}
return await _dbAsync.InsertAllAsync(models);
}

public async Task<int> InsertAsync(object model)
{
return await _dbAsync.InsertAsync(model);
}

public int Replace(object model)
{
lock (objLock)
{
return _db.InsertOrReplace(model);
}
}

public async Task<int> ReplaceAsync(object model)
{
return await _dbAsync.InsertOrReplaceAsync(model);
}

public int Update(object model)
{
lock (objLock)
{
return _db.Update(model);
}
}

public async Task<int> UpdateAsync(object model)
{
return await _dbAsync.UpdateAsync(model);
}

public int UpdateAll(IEnumerable models)
public async Task<int> UpdateAllAsync(IEnumerable models)
{
lock (objLock)
{
return _db.UpdateAll(models);
}
}

public int Delete(object model)
{
lock (objLock)
{
return _db.Delete(model);
}
return await _dbAsync.UpdateAllAsync(models);
}

public async Task<int> DeleteAsync(object model)
{
return await _dbAsync.DeleteAsync(model);
}

public List<T> Query<T>(string sql) where T : new()
public async Task<int> DeleteAllAsync<T>()
{
return _db.Query<T>(sql);
return await _dbAsync.DeleteAllAsync<T>();
}

public async Task<List<T>> QueryAsync<T>(string sql) where T : new()
{
return await _dbAsync.QueryAsync<T>(sql);
}

public int Execute(string sql)
public async Task<int> ExecuteAsync(string sql)
{
return _db.Execute(sql);
return await _dbAsync.ExecuteAsync(sql);
}

public int DeleteAll<T>()
public List<T> Query<T>(string sql) where T : new()
{
return _db.DeleteAll<T>();
return _db.Query<T>(sql);
}

public async Task<int> ExecuteAsync(string sql)
public async Task<List<T>> QueryAsync<T>(string sql) where T : new()
{
return await _dbAsync.ExecuteAsync(sql);
return await _dbAsync.QueryAsync<T>(sql);
}

public TableQuery<T> Table<T>() where T : new()
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Enums/EPresetType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public enum EPresetType
Default = 0,
Russia = 1,
}
}
}
5 changes: 3 additions & 2 deletions v2rayN/ServiceLib/Handler/AppHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public bool IsAdministrator

public bool InitApp()
{
if (ConfigHandler.LoadConfig(ref _config) != 0)
_config = ConfigHandler.LoadConfig();
if (_config == null)
{
return false;
}
Expand Down Expand Up @@ -152,7 +153,7 @@ from ProfileItem a
{
filter = filter.Replace("'", "");
}
sql += String.Format(" and (a.remarks like '%{0}%' or a.address like '%{0}%') ", filter);
sql += string.Format(" and (a.remarks like '%{0}%' or a.address like '%{0}%') ", filter);
}

return SQLiteHelper.Instance.Query<ProfileItemModel>(sql).ToList();
Expand Down
134 changes: 62 additions & 72 deletions v2rayN/ServiceLib/Handler/ClashApiHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ public sealed class ClashApiHandler
private Dictionary<String, ProxiesItem>? _proxies;
public Dictionary<string, object> ProfileContent { get; set; }

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

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

await Task.Delay(5000);
}
updateFunc?.Invoke(null, null);

return null;
}

public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Action<ClashProxyModel?, string> updateFunc)
{
Task.Run(() =>
{
if (blAll)
{
if (blAll)
for (int i = 0; i < 5; i++)
{
for (int i = 0; i < 5; i++)
{
if (_proxies != null)
{
break;
}
Task.Delay(5000).Wait();
}
if (_proxies == null)
if (_proxies != null)
{
return;
}
lstProxy = new List<ClashProxyModel>();
foreach (KeyValuePair<string, ProxiesItem> kv in _proxies)
{
if (Global.notAllowTestType.Contains(kv.Value.type.ToLower()))
{
continue;
}
lstProxy.Add(new ClashProxyModel()
{
name = kv.Value.name,
type = kv.Value.type.ToLower(),
});
break;
}
Task.Delay(5000).Wait();
}

if (lstProxy == null)
if (_proxies == null)
{
return;
}
var urlBase = $"{GetApiUrl()}/proxies";
urlBase += @"/{0}/delay?timeout=10000&url=" + AppHandler.Instance.Config.speedTestItem.speedPingTestUrl;

List<Task> tasks = new List<Task>();
foreach (var it in lstProxy)
lstProxy = new List<ClashProxyModel>();
foreach (KeyValuePair<string, ProxiesItem> kv in _proxies)
{
if (Global.notAllowTestType.Contains(it.type.ToLower()))
if (Global.notAllowTestType.Contains(kv.Value.type.ToLower()))
{
continue;
}
var name = it.name;
var url = string.Format(urlBase, name);
tasks.Add(Task.Run(async () =>
lstProxy.Add(new ClashProxyModel()
{
var result = await HttpClientHelper.Instance.TryGetAsync(url);
updateFunc?.Invoke(it, result);
}));
name = kv.Value.name,
type = kv.Value.type.ToLower(),
});
}
Task.WaitAll(tasks.ToArray());
}

Task.Delay(1000).Wait();
updateFunc?.Invoke(null, "");
});
if (lstProxy == null)
{
return;
}
var urlBase = $"{GetApiUrl()}/proxies";
urlBase += @"/{0}/delay?timeout=10000&url=" + AppHandler.Instance.Config.speedTestItem.speedPingTestUrl;

List<Task> tasks = new List<Task>();
foreach (var it in lstProxy)
{
if (Global.notAllowTestType.Contains(it.type.ToLower()))
{
continue;
}
var name = it.name;
var url = string.Format(urlBase, name);
tasks.Add(Task.Run(async () =>
{
var result = await HttpClientHelper.Instance.TryGetAsync(url);
updateFunc?.Invoke(it, result);
}));
}
Task.WaitAll(tasks.ToArray());

Task.Delay(1000).Wait();
updateFunc?.Invoke(null, "");
});
}

public List<ProxiesItem>? GetClashProxyGroups()
Expand All @@ -118,7 +114,7 @@ public void ClashProxiesDelayTest(bool blAll, List<ClashProxyModel> lstProxy, Ac
}
}

public async void ClashSetActiveProxy(string name, string nameNode)
public async Task ClashSetActiveProxy(string name, string nameNode)
{
try
{
Expand All @@ -133,24 +129,21 @@ public async void ClashSetActiveProxy(string name, string nameNode)
}
}

public void ClashConfigUpdate(Dictionary<string, string> headers)
public async Task ClashConfigUpdate(Dictionary<string, string> headers)
{
Task.Run(async () =>
if (_proxies == null)
{
if (_proxies == null)
{
return;
}
return;
}

var urlBase = $"{GetApiUrl()}/configs";
var urlBase = $"{GetApiUrl()}/configs";

await HttpClientHelper.Instance.PatchAsync(urlBase, headers);
});
await HttpClientHelper.Instance.PatchAsync(urlBase, headers);
}

public async void ClashConfigReload(string filePath)
public async Task ClashConfigReload(string filePath)
{
ClashConnectionClose("");
await ClashConnectionClose("");
try
{
var url = $"{GetApiUrl()}/configs?force=true";
Expand All @@ -164,28 +157,25 @@ public async void ClashConfigReload(string filePath)
}
}

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

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

updateFunc?.Invoke(clashConnections);
return clashConnections;
}
catch (Exception ex)
{
Logging.SaveLog(ex.Message, ex);
}

return null;
}

public async void ClashConnectionClose(string id)
public async Task ClashConnectionClose(string id)
{
try
{
Expand Down
Loading

0 comments on commit 394b657

Please sign in to comment.