From 42e495866a357c4d9cbd93f31d92226f1e81c445 Mon Sep 17 00:00:00 2001 From: Julius Friedman Date: Tue, 21 Nov 2023 18:48:30 -0500 Subject: [PATCH] Fix check for non disposed socket (instead of connected) --- Common/Extensions/SocketExtensions.cs | 2 +- Rtp/RtpClient.Methods.cs | 4 +++- UnitTests/Program.cs | 14 +++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Common/Extensions/SocketExtensions.cs b/Common/Extensions/SocketExtensions.cs index 09c15c3c..4b1037b0 100644 --- a/Common/Extensions/SocketExtensions.cs +++ b/Common/Extensions/SocketExtensions.cs @@ -1214,7 +1214,7 @@ public static int AlignedReceive(byte[] buffer, int offset, int amount, System.N while (amount > 0) //poll write 0 { //Receive it into the buffer at the given offset taking into account what was already received - if (socket.Connected) justReceived = socket.Receive(buffer, offset, amount, System.Net.Sockets.SocketFlags.None, out error); + if (socket.IsNullOrDisposed() is false) justReceived = socket.Receive(buffer, offset, amount, System.Net.Sockets.SocketFlags.None, out error); else justReceived = 0; //Determine how to continue diff --git a/Rtp/RtpClient.Methods.cs b/Rtp/RtpClient.Methods.cs index 41948017..612ee459 100644 --- a/Rtp/RtpClient.Methods.cs +++ b/Rtp/RtpClient.Methods.cs @@ -33,6 +33,8 @@ The above copyright notice and this permission notice shall be included in all c * * v// */ +using Media.Common.Extensions.Socket; + namespace Media.Rtp { /// @@ -1471,7 +1473,7 @@ int ReadApplicationLayerFraming(ref int received, ref int sessionRequired, ref i //{ do received += justRecieved = socket.ReceiveFrom(buffer.Array, buffer.Offset + received, buffer.Count - received, System.Net.Sockets.SocketFlags.None, ref remote); - while (received is 0 /*|| justRecieved > 0 && received + justRecieved < pmax*/ && socket.Connected); + while (socket.IsNullOrDisposed() is false && received is 0 /*|| justRecieved > 0 && received + justRecieved < pmax && socket.Connected*/); ////Lookup the context to determine if the packet will fit //var context = GetContextBySocket(socket); diff --git a/UnitTests/Program.cs b/UnitTests/Program.cs index a5bdeeb8..c325249a 100644 --- a/UnitTests/Program.cs +++ b/UnitTests/Program.cs @@ -155,7 +155,7 @@ public static async Task Main(string[] args) RunTest(async () => { - await RunTestAsync(TestServerAsync); + await RunTestAsync(TestServerAsync, nameof(TestServerAsync)); }); } @@ -4629,10 +4629,10 @@ static void RunTest(Action test, int count = 1, bool waitForGoAhead = true) System.Console.Clear(); Console.WriteLine("About to run test: " + test.Method.Name); Console.WriteLine("Press Q to skip or any other key to continue."); - RunTestAsync(() => { test(); return Task.CompletedTask; }, count, waitForGoAhead).GetAwaiter().GetResult(); + RunTestAsync(() => { test(); return Task.CompletedTask; }, test.Method.Name, count, waitForGoAhead).GetAwaiter().GetResult(); } - static async Task RunTestAsync(Func test, int count = 1, bool waitForGoAhead = true) + static async Task RunTestAsync(Func test, string testMethodName, int count = 1, bool waitForGoAhead = true) { //If the debugger is attached get a ConsoleKey, the key is Q return. if (waitForGoAhead && /*System.Diagnostics.Debugger.IsAttached && */ Console.ReadKey(true).Key == ConsoleKey.Q) return; @@ -4652,12 +4652,12 @@ static async Task RunTestAsync(Func test, int count = 1, bool waitForGoAhe //Decrement remaining --remaining; - TraceMessage("Beginning Test '" + testIndex + "'"); + TraceMessage("Beginning Test '" + testIndex + "'" + testMethodName); //Run the test - await test(); + await test().ConfigureAwait(false); - TraceMessage("Completed Test'" + testIndex + "'"); + TraceMessage("Completed Test'" + testIndex + "'" + testMethodName); //Increment the success counter ++successes; @@ -4706,7 +4706,7 @@ static async Task RunTestAsync(Func test, int count = 1, bool waitForGoAhe { case ConsoleKey.W: { - await RunTestAsync(test, 1, false).ConfigureAwait(false); + await RunTestAsync(test, testMethodName, 1, false).ConfigureAwait(false); return; } case ConsoleKey.D: