mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-07 23:40:52 +02:00
Improve code structure
- Add separate API project - Add notification system
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
using HyperbolicDownloaderApi.Managment;
|
||||
using HyperbolicDownloaderApi.Networking;
|
||||
|
||||
using System.Net.Sockets;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace HyperbolicDownloaderApi;
|
||||
|
||||
public class HostsManager
|
||||
{
|
||||
private List<NetworkSocket> hosts = new List<NetworkSocket>();
|
||||
public int Count => hosts.Count;
|
||||
|
||||
public void AddRange(IEnumerable<NetworkSocket> hosts)
|
||||
{
|
||||
foreach (NetworkSocket host in hosts)
|
||||
{
|
||||
if (!Contains(host))
|
||||
{
|
||||
this.hosts.Add(host);
|
||||
}
|
||||
}
|
||||
|
||||
SaveHosts();
|
||||
}
|
||||
|
||||
public void Add(NetworkSocket host)
|
||||
{
|
||||
if (!Contains(host))
|
||||
{
|
||||
hosts.Add(host);
|
||||
}
|
||||
SaveHosts();
|
||||
}
|
||||
|
||||
public void Remove(NetworkSocket host, bool forceRemove = false)
|
||||
{
|
||||
if (DateTime.Now - host.LastActive >= new TimeSpan(24, 0, 0) || forceRemove)
|
||||
{
|
||||
hosts.RemoveAll(x => x.IPAddress == host.IPAddress && x.Port == host.Port);
|
||||
}
|
||||
SaveHosts();
|
||||
}
|
||||
|
||||
public bool Contains(NetworkSocket host)
|
||||
{
|
||||
return hosts.Any(x => x.IPAddress == host.IPAddress && x.Port == host.Port);
|
||||
}
|
||||
|
||||
public int CheckHostsActivity()
|
||||
{
|
||||
List<NetworkSocket> hostsToRemove = new List<NetworkSocket>();
|
||||
int activeHostsCount = 0;
|
||||
foreach (NetworkSocket host in hosts)
|
||||
{
|
||||
ApiManager.SendMessage($"{host.IPAddress}:{host.Port} > ???", NotificationMessageType.Warning);
|
||||
using TcpClient tcpClient = new TcpClient();
|
||||
|
||||
try
|
||||
{
|
||||
tcpClient.ConnectAsync(host.IPAddress, host.Port).Wait(1000);
|
||||
Console.CursorLeft = 0;
|
||||
if (tcpClient.Connected)
|
||||
{
|
||||
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Active", NotificationMessageType.Success);
|
||||
host.LastActive = DateTime.Now;
|
||||
activeHostsCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (DateTime.Now - host.LastActive >= new TimeSpan(24, 0, 0))
|
||||
{
|
||||
hostsToRemove.Add(host);
|
||||
}
|
||||
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (DateTime.Now - host.LastActive >= new TimeSpan(24, 0, 0))
|
||||
{
|
||||
hostsToRemove.Add(host);
|
||||
}
|
||||
Console.CursorLeft = 0;
|
||||
ApiManager.SendMessageNewLine($"{host.IPAddress}:{host.Port} > Inactive", NotificationMessageType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (NetworkSocket host in hostsToRemove)
|
||||
{
|
||||
hosts.Remove(host);
|
||||
}
|
||||
|
||||
SaveHosts();
|
||||
return activeHostsCount;
|
||||
}
|
||||
|
||||
public List<NetworkSocket> ToList()
|
||||
{
|
||||
return hosts.ToList();
|
||||
}
|
||||
|
||||
public void SaveHosts()
|
||||
{
|
||||
hosts = hosts.OrderByDescending(h => h.LastActive).ToList();
|
||||
File.WriteAllText(ApiConfiguration.HostsFilePath, JsonSerializer.Serialize(hosts));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user