From 876965ab4aad30d4150a51fd3583b44bc0a151f5 Mon Sep 17 00:00:00 2001 From: Forest Date: Sun, 17 Mar 2019 14:44:38 -0700 Subject: [PATCH] Fix a very dumb Position < Length bug, put a semaphore around something that supposedly could be a bug --- Hazel/Hazel.csproj | 10 ++++++++ Hazel/Tcp/TcpConnection.cs | 39 +++++++++++++++++++++++++---- Hazel/Udp/UdpConnection.Reliable.cs | 4 +-- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/Hazel/Hazel.csproj b/Hazel/Hazel.csproj index 3cdfc7b..23cd33e 100644 --- a/Hazel/Hazel.csproj +++ b/Hazel/Hazel.csproj @@ -45,6 +45,16 @@ + + true + bin\DebugClient\ + DEBUG;TRACE + true + full + AnyCPU + prompt + MinimumRecommendedRules.ruleset + diff --git a/Hazel/Tcp/TcpConnection.cs b/Hazel/Tcp/TcpConnection.cs index fc63ae7..aaac648 100644 --- a/Hazel/Tcp/TcpConnection.cs +++ b/Hazel/Tcp/TcpConnection.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; +using System.Threading; namespace Hazel.Tcp { @@ -127,10 +128,12 @@ namespace Hazel.Tcp try { - socket.BeginSend(fullBytes, 0, fullBytes.Length, SocketFlags.None, null, null); + this.sem.WaitOne(); + socket.BeginSend(fullBytes, 0, fullBytes.Length, SocketFlags.None, FinishSend, null); } catch (Exception e) { + try { this.sem.Set(); } catch (ObjectDisposedException) { } Disconnect("Could not send data as an occured: " + e.Message); } @@ -154,16 +157,32 @@ namespace Hazel.Tcp try { - socket.BeginSend(fullBytes, 0, fullBytes.Length, SocketFlags.None, null, null); + this.sem.WaitOne(); + socket.BeginSend(fullBytes, 0, fullBytes.Length, SocketFlags.None, FinishSend, null); } catch (Exception e) { + try { this.sem.Set(); } catch (ObjectDisposedException) { } Disconnect("Could not send data as an occured: " + e.Message); } Statistics.LogFragmentedSend(bytes.Length, fullBytes.Length); } - + + private AutoResetEvent sem = new AutoResetEvent(true); + private void FinishSend(IAsyncResult ar) + { + try + { + this.socket.EndSend(ar); + } + catch { } + finally + { + try { this.sem.Set(); } catch (ObjectDisposedException) { } + } + } + /// /// Starts waiting for a first handshake packet to be received. /// @@ -270,15 +289,15 @@ namespace Hazel.Tcp Statistics.LogFragmentedReceive(bytesRead, 0); - if (msg.Position < bytesRead) + if (msg.Position < msg.Length) { ListenForData(msg, callback); } else { - msg.Position = 0; try { + msg.Position = 0; callback(msg); } catch { } @@ -328,6 +347,16 @@ namespace Hazel.Tcp { if (disposing) { + try + { + if (this.sem != null) + { + this.sem.Dispose(); + this.sem = null; + } + } + catch { } + lock (this) { State = ConnectionState.NotConnected; diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 1b0c342..acac1bb 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -147,7 +147,7 @@ namespace Hazel.Udp { if (connection.reliableDataPacketsSent.TryRemove(this.Id, out Packet self)) { - connection.Disconnect($"Reliable packet {self.Id} was not ack'd after {lifetime}ms ({self.Retransmissions} resends)"); + connection.Disconnect($"Reliable packet {self.Id} (size={this.Length}) was not ack'd after {lifetime}ms ({self.Retransmissions} resends)"); self.Recycle(); } @@ -163,7 +163,7 @@ namespace Hazel.Udp { if (connection.reliableDataPacketsSent.TryRemove(this.Id, out Packet self)) { - connection.Disconnect($"Reliable packet {self.Id} was not ack'd after {self.Retransmissions} resends ({lifetime}ms)"); + connection.Disconnect($"Reliable packet {self.Id} (size={this.Length}) was not ack'd after {self.Retransmissions} resends ({lifetime}ms)"); self.Recycle(); } -- 2.39.5