]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Fix a very dumb Position < Length bug, put a semaphore around something that supposed...
authorForest <chocozilla@gmail.com>
Sun, 17 Mar 2019 21:44:38 +0000 (14:44 -0700)
committerForest <chocozilla@gmail.com>
Sun, 17 Mar 2019 21:44:38 +0000 (14:44 -0700)
Hazel/Hazel.csproj
Hazel/Tcp/TcpConnection.cs
Hazel/Udp/UdpConnection.Reliable.cs

index 3cdfc7b5aed03e6d630497773802d8ee82fb3537..23cd33e63d56516108340cefcc7402ab8b18cdb3 100644 (file)
     <AssemblyOriginatorKeyFile>
     </AssemblyOriginatorKeyFile>
   </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugClient|AnyCPU'">
+    <DebugSymbols>true</DebugSymbols>
+    <OutputPath>bin\DebugClient\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <DebugType>full</DebugType>
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <ErrorReport>prompt</ErrorReport>
+    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
+  </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
     <Reference Include="System.Core" />
index fc63ae7748eb6abc9b1fcd07e6a39f207fda3262..aaac6484cca1f963f879e6934aaa734fee4e268e 100644 (file)
@@ -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) { }
+            }
+        }
+
         /// <summary>
         ///     Starts waiting for a first handshake packet to be received.
         /// </summary>
@@ -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;
index 1b0c342fa0b194bfb5294ff30881be631dfbebf6..acac1bbfa05d36db1d1debb183cb2403ccff577c 100644 (file)
@@ -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();
                             }