diff --git a/HyperbolicDownloader/InputHandler.cs b/HyperbolicDownloader/InputHandler.cs index 89be2e7..327cb2f 100644 --- a/HyperbolicDownloader/InputHandler.cs +++ b/HyperbolicDownloader/InputHandler.cs @@ -28,7 +28,9 @@ internal class InputHandler _ = 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(hostCommands.Sync, (HelpText)"Syncs the host list", "sync"); - _ = commander.Register(logCommands.Log, (HelpText)"Displays live log.", "log"); + + Command logCommand = commander.Register((_) => logCommands.Log(false), (HelpText)"Displays live log.", "log"); + _ = logCommand.Register((_) => logCommands.Log(true), (HelpText)"Displays live log.", "debug"); 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"); diff --git a/HyperbolicDownloader/LogCommands.cs b/HyperbolicDownloader/LogCommands.cs index e47c0d8..88238eb 100644 --- a/HyperbolicDownloader/LogCommands.cs +++ b/HyperbolicDownloader/LogCommands.cs @@ -8,10 +8,23 @@ namespace HyperbolicDownloader; internal class LogCommands { - public void Log(string _) + private bool debug = false; + + public void Log(bool debug) { + this.debug = debug; + Console.Clear(); + if (debug) + { + Console.WriteLine("Debug log opened"); + } + else + { + Console.WriteLine("Log opened"); + } + ApiManager.OnNotificationMessageRecived += OnNotificationMessageRecived; char c = ' '; @@ -34,7 +47,7 @@ internal class LogCommands private void OnNotificationMessageRecived(object? sender, NotificationMessageEventArgs e) { - if (e.NotificationMessageType == NotificationMessageType.Log) + if (e.NotificationMessageType == NotificationMessageType.Log || (debug && e.NotificationMessageType == NotificationMessageType.Debug)) { lock (Console.Out) { diff --git a/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs b/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs index ad87cf4..f34753c 100644 --- a/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs +++ b/HyperbolicDownloaderApi/Managment/NotificationMessageEventArgs.cs @@ -10,6 +10,7 @@ public enum NotificationMessageType { Info, Log, + Debug, Success, Warning, Error diff --git a/HyperbolicDownloaderApi/Networking/NetworkClient.cs b/HyperbolicDownloaderApi/Networking/NetworkClient.cs index b53ca82..224c5a1 100644 --- a/HyperbolicDownloaderApi/Networking/NetworkClient.cs +++ b/HyperbolicDownloaderApi/Networking/NetworkClient.cs @@ -92,14 +92,25 @@ internal class NetworkClient(FilesManager filesManager) { try { + ApiManager.SendNotificationMessageNewLine("Listening for connections...", NotificationMessageType.Debug); + TcpClient client = tcpListener.AcceptTcpClient(); + + ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Connected", NotificationMessageType.Debug); + NetworkStream nwStream = client.GetStream(); byte[] buffer = new byte[client.ReceiveBufferSize]; + ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Reading data", NotificationMessageType.Debug); + int bytesRead = await nwStream.ReadAsync(buffer.AsMemory(0, client.ReceiveBufferSize)); + ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Data read", NotificationMessageType.Debug); + string dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead); + ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Data: {dataReceived}", NotificationMessageType.Debug); + if (dataReceived.StartsWith("Download")) { _ = Upload(client, dataReceived[8..]); @@ -129,13 +140,12 @@ internal class NetworkClient(FilesManager filesManager) } client.Close(); + + ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Disconnected", NotificationMessageType.Debug); } catch (SocketException ex) { - if (ex.SocketErrorCode != SocketError.Interrupted) - { - throw; - } + ApiManager.SendNotificationMessageNewLine(ex.Message, NotificationMessageType.Log); } } tcpListener.Stop();