From 5cbb18f4b3ac78ba3531f3f1bab74b3f0d38ba02 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Mon, 18 Dec 2023 20:01:18 +0100 Subject: [PATCH] Add additional saveguards --- .../Commands/StreamingCommands.cs | 1 + .../Networking/NetworkClient.cs | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/HyperbolicDownloaderApi/Commands/StreamingCommands.cs b/HyperbolicDownloaderApi/Commands/StreamingCommands.cs index e11acd9..ba7847d 100644 --- a/HyperbolicDownloaderApi/Commands/StreamingCommands.cs +++ b/HyperbolicDownloaderApi/Commands/StreamingCommands.cs @@ -156,6 +156,7 @@ public class StreamingCommands(HostsManager hostsManager) ApiManager.SendNotificationMessageNewLine($"File name: {fileName}"); ApiManager.SendNotificationMessageNewLine($"Starting stream..."); + ApiManager.SendNotificationMessageNewLine("Controls: [p]lay, [s]top"); BufferedWaveProvider bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(sampleRate, bitsPerSample, channels)); using WaveOutEvent player = new WaveOutEvent(); diff --git a/HyperbolicDownloaderApi/Networking/NetworkClient.cs b/HyperbolicDownloaderApi/Networking/NetworkClient.cs index 3853c5e..8a67b42 100644 --- a/HyperbolicDownloaderApi/Networking/NetworkClient.cs +++ b/HyperbolicDownloaderApi/Networking/NetworkClient.cs @@ -257,7 +257,21 @@ internal class NetworkClient(FilesManager filesManager) { ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Accepting file stream [{Path.GetFileName(hyperFileInfo.FilePath)}] [{hash}]", NotificationMessageType.Log); - using WaveFileReader reader = new WaveFileReader(hyperFileInfo.FilePath); + WaveFileReader reader; + + try + { + reader = new WaveFileReader(hyperFileInfo.FilePath); + } + catch (FormatException ex) + { + ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Error streaming file {ex.Message} [{hash}]", NotificationMessageType.Log); + Debug.WriteLine(ex); + bytesToSend = Encoding.ASCII.GetBytes("Invalid file format!"); + await nwStream.WriteAsync(bytesToSend); + + return; + } bytesToSend = Encoding.ASCII.GetBytes($"{reader.Length}/{Path.GetFileName(hyperFileInfo.FilePath)}/{reader.WaveFormat.SampleRate}/{reader.WaveFormat.BitsPerSample}/{reader.WaveFormat.Channels}"); @@ -274,6 +288,8 @@ internal class NetworkClient(FilesManager filesManager) } ApiManager.SendNotificationMessageNewLine($"{(client.Client.RemoteEndPoint as IPEndPoint)?.Address} > Completed stream of file [{Path.GetFileName(hyperFileInfo.FilePath)}] [{hash}]", NotificationMessageType.Log); + + reader.Close(); } else {