mirror of
https://github.com/Stone-Red-Code/HyperbolicDownloader.git
synced 2026-09-06 15:56:22 +02:00
Add new log command and some general command improvments
This commit is contained in:
@@ -11,9 +11,8 @@ internal class BroadcastClient
|
||||
{
|
||||
public event EventHandler<BroadcastRecivedEventArgs>? OnBroadcastRecived;
|
||||
|
||||
public bool IsListening { get; private set; } = false;
|
||||
|
||||
private UdpClient? udpListener;
|
||||
public bool IsListening { get; private set; } = false;
|
||||
|
||||
public static void Send(int port, string message)
|
||||
{
|
||||
@@ -58,7 +57,7 @@ internal class BroadcastClient
|
||||
udpListener = new UdpClient(port);
|
||||
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
|
||||
|
||||
_ = Task.Run(() =>
|
||||
_ = new TaskFactory().StartNew(() =>
|
||||
{
|
||||
while (IsListening)
|
||||
{
|
||||
@@ -67,7 +66,7 @@ internal class BroadcastClient
|
||||
|
||||
OnBroadcastRecived?.Invoke(this, new BroadcastRecivedEventArgs(groupEP, message));
|
||||
}
|
||||
});
|
||||
}, TaskCreationOptions.LongRunning);
|
||||
}
|
||||
|
||||
public void StopListening()
|
||||
|
||||
@@ -12,16 +12,7 @@ public class HostsManager
|
||||
|
||||
public int AddRange(IEnumerable<NetworkSocket> hosts)
|
||||
{
|
||||
int newHosts = 0;
|
||||
|
||||
foreach (NetworkSocket host in hosts)
|
||||
{
|
||||
if (Add(host))
|
||||
{
|
||||
newHosts++;
|
||||
}
|
||||
}
|
||||
|
||||
int newHosts = hosts.Count(Add);
|
||||
SaveHosts();
|
||||
|
||||
return newHosts;
|
||||
@@ -44,14 +35,14 @@ public class HostsManager
|
||||
{
|
||||
if (DateTime.Now - host.LastActive >= new TimeSpan(24, 0, 0) || forceRemove)
|
||||
{
|
||||
_ = hosts.RemoveAll(x => x.IPAddress == host.IPAddress && x.Port == host.Port);
|
||||
_ = hosts.RemoveAll(x => x.Equals(host));
|
||||
}
|
||||
SaveHosts();
|
||||
}
|
||||
|
||||
public bool Contains(NetworkSocket host)
|
||||
{
|
||||
return hosts.Any(x => x.IPAddress == host.IPAddress && x.Port == host.Port);
|
||||
return hosts.Exists(x => x.Equals(host));
|
||||
}
|
||||
|
||||
public int CheckHostsActivity()
|
||||
@@ -65,7 +56,7 @@ public class HostsManager
|
||||
|
||||
try
|
||||
{
|
||||
_ = tcpClient.ConnectAsync(host.IPAddress, host.Port).Wait(500);
|
||||
_ = tcpClient.ConnectAsync(host.IPAddress, host.Port).Wait(1000);
|
||||
Console.CursorLeft = 0;
|
||||
if (tcpClient.Connected)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using HyperbolicDownloaderApi.FileProcessing;
|
||||
using HyperbolicDownloaderApi.Managment;
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
@@ -10,11 +11,10 @@ namespace HyperbolicDownloaderApi.Networking;
|
||||
|
||||
internal class NetworkClient
|
||||
{
|
||||
public bool IsListening { get; private set; } = false;
|
||||
|
||||
private TcpListener? tcpListener;
|
||||
private readonly FilesManager filesManager;
|
||||
private readonly Dictionary<string, (Type type, Delegate method)> events = new();
|
||||
private TcpListener? tcpListener;
|
||||
public bool IsListening { get; private set; } = false;
|
||||
|
||||
public NetworkClient(FilesManager filesManager)
|
||||
{
|
||||
@@ -98,7 +98,7 @@ internal class NetworkClient
|
||||
tcpListener.Start();
|
||||
IsListening = true;
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
_ = new TaskFactory().StartNew(async () =>
|
||||
{
|
||||
while (IsListening)
|
||||
{
|
||||
@@ -151,45 +151,7 @@ internal class NetworkClient
|
||||
}
|
||||
}
|
||||
tcpListener.Stop();
|
||||
});
|
||||
}
|
||||
|
||||
private async Task Upload(TcpClient client, string hash)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] bytesToSend;
|
||||
hash = hash.Trim();
|
||||
NetworkStream nwStream = client.GetStream();
|
||||
client.SendBufferSize = 64000;
|
||||
if (filesManager.TryGet(hash, out PrivateHyperFileInfo? hyperFileInfo) && File.Exists(hyperFileInfo?.FilePath))
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(hyperFileInfo.FilePath);
|
||||
|
||||
bytesToSend = Encoding.ASCII.GetBytes($"{fileInfo.Length}/{Path.GetFileName(hyperFileInfo.FilePath)}");
|
||||
|
||||
Array.Resize(ref bytesToSend, 1000);
|
||||
|
||||
await nwStream.WriteAsync(bytesToSend);
|
||||
foreach (byte[]? chunk in FileCompressor.ReadChunks(hyperFileInfo.FilePath, 64000).Where(chunk => chunk is not null))
|
||||
{
|
||||
await nwStream.WriteAsync(chunk);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesToSend = Encoding.ASCII.GetBytes("File not found!");
|
||||
await nwStream.WriteAsync(bytesToSend);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
client.Close();
|
||||
}
|
||||
}, TaskCreationOptions.LongRunning);
|
||||
}
|
||||
|
||||
public void StopListening()
|
||||
@@ -202,4 +164,52 @@ internal class NetworkClient
|
||||
{
|
||||
events.Add(eventName, (typeof(T), eventHandler));
|
||||
}
|
||||
|
||||
private async Task Upload(TcpClient client, string hash)
|
||||
{
|
||||
try
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Requesting file download [{hash}]", NotificationMessageType.Log);
|
||||
byte[] bytesToSend;
|
||||
hash = hash.Trim();
|
||||
NetworkStream nwStream = client.GetStream();
|
||||
client.SendBufferSize = 64000;
|
||||
if (filesManager.TryGet(hash, out PrivateHyperFileInfo? hyperFileInfo) && File.Exists(hyperFileInfo?.FilePath))
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Accepting file download [{Path.GetFileName(hyperFileInfo.FilePath)}] [{hash}]", NotificationMessageType.Log);
|
||||
|
||||
FileInfo fileInfo = new FileInfo(hyperFileInfo.FilePath);
|
||||
|
||||
bytesToSend = Encoding.ASCII.GetBytes($"{fileInfo.Length}/{Path.GetFileName(hyperFileInfo.FilePath)}");
|
||||
|
||||
Array.Resize(ref bytesToSend, 1000);
|
||||
|
||||
await nwStream.WriteAsync(bytesToSend);
|
||||
|
||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Starting file download of file [{Path.GetFileName(hyperFileInfo.FilePath)}] [{hash}]", NotificationMessageType.Log);
|
||||
|
||||
foreach (byte[]? chunk in FileCompressor.ReadChunks(hyperFileInfo.FilePath, 64000).Where(chunk => chunk is not null))
|
||||
{
|
||||
await nwStream.WriteAsync(chunk);
|
||||
}
|
||||
|
||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Completed download of file [{Path.GetFileName(hyperFileInfo.FilePath)}] [{hash}]", NotificationMessageType.Log);
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesToSend = Encoding.ASCII.GetBytes("File not found!");
|
||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > File not found [{hash}]", NotificationMessageType.Log);
|
||||
await nwStream.WriteAsync(bytesToSend);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Error downloading file {ex.Message} [{hash}]", NotificationMessageType.Log);
|
||||
Debug.WriteLine(ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
client.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user