Code cleanup

This commit is contained in:
Stone_Red
2023-02-06 15:15:47 +01:00
parent 7d662dc113
commit fb21a9f492
10 changed files with 235 additions and 237 deletions
+14 -14
View File
@@ -20,31 +20,31 @@ internal class InputHandler
DownloadCommands downloadCommands = new DownloadCommands(hostsManager, filesManager);
ClientCommands clientCommands = new ClientCommands();
commander.Register(input => commander.PrintHelp(input), "help");
commander.Register(_ => Console.Clear(), (HelpText)"Clears the console.", "clear", "cls");
commander.Register(_ => exit = true, (HelpText)"Exits the application.", "exit", "quit");
commander.Register(clientCommands.ShowInfo, (HelpText)"Displays the private and public IP address.", "info", "inf");
commander.Register(hostCommands.Discover, (HelpText)"Tries to find other active hosts on the local network.", "discover", "disc");
_ = commander.Register(input => commander.PrintHelp(input), "help");
_ = commander.Register(_ => Console.Clear(), (HelpText)"Clears the console.", "clear", "cls");
_ = commander.Register(_ => exit = true, (HelpText)"Exits the application.", "exit", "quit");
_ = commander.Register(clientCommands.ShowInfo, (HelpText)"Displays the private and public IP address.", "info", "inf");
_ = commander.Register(hostCommands.Discover, (HelpText)"Tries to find other active hosts on the local network.", "discover", "disc");
Command getCommand = commander.Register(downloadCommands.GetFile, (HelpText)"Attempts to retrieve a file from another host using a hash.", "get");
getCommand.Register(downloadCommands.GetFileFrom, (HelpText)"Attempts to retrieve a file from another host using a .hyper file.", "from");
_ = getCommand.Register(downloadCommands.GetFileFrom, (HelpText)"Attempts to retrieve a file from another host using a .hyper file.", "from");
Command generateCommad = commander.Register(fileCommands.GenerateFileFull, (HelpText)"Generates a .hyper file from a file hash.", "generate", "gen");
generateCommad.Register(fileCommands.GenerateFileSingle, (HelpText)"Generates a .hyper file from a file hash without checking the known hosts. This adds only the local host to the file.", "noscan");
_ = generateCommad.Register(fileCommands.GenerateFileSingle, (HelpText)"Generates a .hyper file from a file hash without checking the known hosts. This adds only the local host to the file.", "noscan");
Command addCommand = commander.Register(fileCommands.AddFile, (HelpText)"Adds a file to the tracking list.", "add");
addCommand.Register(fileCommands.AddFile, (HelpText)"Adds a file to the tracking list.", "file");
addCommand.Register(hostCommands.AddHost, (HelpText)"Adds a host to the list of known hosts.", "host");
_ = addCommand.Register(fileCommands.AddFile, (HelpText)"Adds a file to the tracking list.", "file");
_ = addCommand.Register(hostCommands.AddHost, (HelpText)"Adds a host to the list of known hosts.", "host");
Command removeCommand = commander.Register(fileCommands.RemoveFile, (HelpText)"Removes a file from the tracking list.", "remove", "rm");
removeCommand.Register(fileCommands.RemoveFile, (HelpText)"Removes a file from the tracking list.", "file");
removeCommand.Register(hostCommands.RemoveHost, (HelpText)"Removes a host from the list of known hosts.", "host");
_ = removeCommand.Register(fileCommands.RemoveFile, (HelpText)"Removes a file from the tracking list.", "file");
_ = removeCommand.Register(hostCommands.RemoveHost, (HelpText)"Removes a host from the list of known hosts.", "host");
Command listCommand = commander.Register(fileCommands.ListFiles, (HelpText)"Lists all files.", "list", "ls");
listCommand.Register(fileCommands.ListFiles, (HelpText)"Lists all files.", "files");
listCommand.Register(hostCommands.ListHosts, (HelpText)"lists all hosts.", "hosts");
_ = listCommand.Register(fileCommands.ListFiles, (HelpText)"Lists all files.", "files");
_ = listCommand.Register(hostCommands.ListHosts, (HelpText)"lists all hosts.", "hosts");
commander.Register(hostCommands.CheckActiveHosts, (HelpText)"Checks the status of known hosts.", "status", "check");
_ = commander.Register(hostCommands.CheckActiveHosts, (HelpText)"Checks the status of known hosts.", "status", "check");
}
public void ReadInput()
+9 -4
View File
@@ -45,6 +45,14 @@ internal static class Program
Console.WriteLine();
}
await Initialize();
inputHandler.ReadInput();
Close();
}
private static async Task Initialize()
{
Console.WriteLine("Searching for a UPnP/NAT-PMP device...");
_ = await ApiManager.OpenPorts();
@@ -54,7 +62,7 @@ internal static class Program
Console.WriteLine("Starting TCP listener...");
if (!apiManager.StartTcpListener())
{
Console.ReadLine();
_ = Console.ReadLine();
}
Console.WriteLine("Starting broadcast listener...");
@@ -77,9 +85,6 @@ internal static class Program
Console.WriteLine($"{activeHostsCount} active host(s).");
ConsoleExt.WriteLine("Ready", ConsoleColor.Green);
inputHandler.ReadInput();
Close();
}
private static void ApiManager_OnNotificationMessageRecived(object? sender, NotificationMessageEventArgs e)
@@ -152,7 +152,7 @@ public class DownloadCommands
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
_ = Directory.CreateDirectory(directoryPath);
}
using FileStream? fileStream = new FileStream(filePath, FileMode.Create);
@@ -72,7 +72,7 @@ public class FileCommands
string directoryPath = Path.Combine(ApiConfiguration.BasePath, "GeneratedFiles");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
_ = Directory.CreateDirectory(directoryPath);
}
hash = hash.Trim().ToLower();
@@ -111,7 +111,7 @@ public class FileCommands
string directoryPath = Path.Combine(ApiConfiguration.BasePath, "GeneratedFiles");
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
_ = Directory.CreateDirectory(directoryPath);
}
hash = hash.Trim().ToLower();
@@ -72,7 +72,7 @@ public class HostCommands
string portInput = parts[1];
_ = int.TryParse(portInput, out int port);
if (port < 1000 || port >= 6000)
if (port is < 1000 or >= 6000)
{
ApiManager.SendNotificationMessageNewLine("Invalid port number!", NotificationMessageType.Error);
return;
@@ -112,7 +112,7 @@ public class HostCommands
_ = int.TryParse(portInput, out int port);
if (port < 1000 || port >= 6000)
if (port is < 1000 or >= 6000)
{
ApiManager.SendNotificationMessageNewLine("Invalid port number!", NotificationMessageType.Error);
return;
@@ -1,6 +1,7 @@
using System.Reflection;
namespace HyperbolicDownloaderApi.Managment;
public static class ApiConfiguration
{
public const int BroadcastPort = 2155;
@@ -9,5 +10,4 @@ public static class ApiConfiguration
public static string BasePath { get; } = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location) ?? string.Empty;
public static string HostsFilePath { get; } = Path.Combine(BasePath, "Hosts.json");
public static string FilesInfoPath { get; } = Path.Combine(BasePath, "Files.json");
}
@@ -43,7 +43,7 @@ internal class BroadcastClient
byte[] sendbuf = Encoding.ASCII.GetBytes(message);
IPEndPoint ep = new IPEndPoint(broadcast, port);
socket.SendTo(sendbuf, ep);
_ = socket.SendTo(sendbuf, ep);
}
public void StartListening(int port)
@@ -58,7 +58,7 @@ internal class BroadcastClient
udpListener = new UdpClient(port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, port);
Task.Run(() =>
_ = Task.Run(() =>
{
while (IsListening)
{
@@ -1,7 +1,7 @@
namespace HyperbolicDownloaderApi
namespace HyperbolicDownloaderApi.Networking;
internal class DataContainer
{
internal class DataContainer
{
public string EventName { get; set; }
public string JsonData { get; set; }
@@ -10,5 +10,4 @@
JsonData = jsonData;
EventName = eventName;
}
}
}
@@ -3,10 +3,10 @@ using System.Net.Sockets;
using System.Text;
using System.Text.Json;
namespace HyperbolicDownloaderApi
namespace HyperbolicDownloaderApi.Networking;
internal class MessageRecivedEventArgs<T> : EventArgs
{
internal class MessageRecivedEventArgs<T> : EventArgs
{
private readonly NetworkStream networkStream;
public MessageRecivedEventArgs(NetworkStream networkStream, IPAddress ipAddress, T data)
@@ -30,5 +30,4 @@ namespace HyperbolicDownloaderApi
{
SendResponseAsync(response).GetAwaiter().GetResult();
}
}
}
@@ -6,10 +6,10 @@ using System.Net.Sockets;
using System.Text;
using System.Text.Json;
namespace HyperbolicDownloaderApi.Networking
namespace HyperbolicDownloaderApi.Networking;
internal class NetworkClient
{
internal class NetworkClient
{
public bool IsListening { get; private set; } = false;
private TcpListener? tcpListener;
@@ -98,7 +98,7 @@ namespace HyperbolicDownloaderApi.Networking
tcpListener.Start();
IsListening = true;
Task.Run(async () =>
_ = Task.Run(async () =>
{
while (IsListening)
{
@@ -137,7 +137,7 @@ namespace HyperbolicDownloaderApi.Networking
(client.Client.RemoteEndPoint as IPEndPoint)?.Address,
JsonSerializer.Deserialize(dataContainer.JsonData, type));
method?.DynamicInvoke(this, eventArgs);
_ = (method?.DynamicInvoke(this, eventArgs));
}
client.Close();
@@ -171,15 +171,11 @@ namespace HyperbolicDownloaderApi.Networking
Array.Resize(ref bytesToSend, 1000);
await nwStream.WriteAsync(bytesToSend);
foreach (byte[]? chunk in FileCompressor.ReadChunks(hyperFileInfo.FilePath, 64000))
{
if (chunk is not null)
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!");
@@ -206,5 +202,4 @@ namespace HyperbolicDownloaderApi.Networking
{
events.Add(eventName, (typeof(T), eventHandler));
}
}
}