]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Revert some bad code that caused OnNewConnection to be called multiple times for...
authorForest <chocozilla@gmail.com>
Thu, 25 Jul 2019 23:42:40 +0000 (16:42 -0700)
committerForest <chocozilla@gmail.com>
Thu, 25 Jul 2019 23:42:40 +0000 (16:42 -0700)
Hazel/MessageReader.cs
Hazel/ObjectPool.cs
Hazel/Udp/UdpConnectionListener.cs
Hazel/Udp/UdpServerConnection.cs

index 1914437227bb6ee30f2204325a46966034633de7..b5f53846fbd3bc92495a96ee9a911cf4e96591d1 100644 (file)
@@ -23,7 +23,7 @@ namespace Hazel
                 this.readHead = value + Offset;
             }
         }
-        
+
         private int _position;
         private int readHead;
         
@@ -123,7 +123,6 @@ namespace Hazel
             return output;
         }
 
-        ///
         public void Recycle()
         {
             ReaderPool.PutObject(this);
index 889969925b0056f200e0d734d99664f05af36c7b..725353eb2d854b1b1301f438bc060d99aaa18022 100644 (file)
@@ -72,10 +72,5 @@ namespace Hazel
                 throw new Exception("Duplicate add " + typeof(T).Name);
             }
         }
-
-        public bool IsObjectInUse(T item)
-        {
-            return inuse.ContainsKey(item);
-        }
     }
 }
index 09ee2ac3a9448cb2bff97368c6a4edb3367775dd..32a3e06a55ced6e358928e8d1c4a40f5a07d1132 100644 (file)
@@ -31,7 +31,7 @@ namespace Hazel.Udp
         /// <summary>
         ///     The connections we currently hold
         /// </summary>
-        ConcurrentDictionary<EndPoint, UdpServerConnection> allConnections = new ConcurrentDictionary<EndPoint, UdpServerConnection>();
+        private ConcurrentDictionary<EndPoint, UdpServerConnection> allConnections = new ConcurrentDictionary<EndPoint, UdpServerConnection>();
         
         public int ConnectionCount { get { return this.allConnections.Count; } }
 
@@ -194,30 +194,37 @@ namespace Hazel.Udp
             UdpServerConnection connection;
             if (!this.allConnections.TryGetValue(remoteEndPoint, out connection))
             {
-                //Check for malformed connection attempts
-                if (!isHello)
+                lock (this.allConnections)
                 {
-                    message.Recycle();
-                    Interlocked.Decrement(ref this.ActiveCallbacks);
-                    return;
-                }
-
-                if (AcceptConnection != null)
-                {
-                    if (!AcceptConnection(out var response))
+                    if (!this.allConnections.TryGetValue(remoteEndPoint, out connection))
                     {
-                        message.Recycle();
-                        SendData(response, response.Length, remoteEndPoint);
-                        Interlocked.Decrement(ref this.ActiveCallbacks);
-                        return;
+                        //Check for malformed connection attempts
+                        if (!isHello)
+                        {
+                            message.Recycle();
+                            Interlocked.Decrement(ref this.ActiveCallbacks);
+                            return;
+                        }
+
+                        if (AcceptConnection != null)
+                        {
+                            if (!AcceptConnection(out var response))
+                            {
+                                message.Recycle();
+                                SendData(response, response.Length, remoteEndPoint);
+                                Interlocked.Decrement(ref this.ActiveCallbacks);
+                                return;
+                            }
+                        }
+
+                        aware = false;
+                        connection = new UdpServerConnection(this, (IPEndPoint)remoteEndPoint, this.IPMode);
+                        if (!this.allConnections.TryAdd(remoteEndPoint, connection))
+                        {
+                            throw new HazelException("Failed to add a connection. This should never happen.");
+                        }
                     }
                 }
-
-                connection = this.allConnections.GetOrAdd(remoteEndPoint, (ep) =>
-                {
-                    aware = false;
-                    return new UdpServerConnection(this, (IPEndPoint)ep, this.IPMode);
-                });
             }
 
             //Inform the connection of the buffer (new connections need to send an ack back to client)
@@ -272,14 +279,7 @@ namespace Hazel.Udp
                     length,
                     SocketFlags.None,
                     endPoint,
-                    delegate (IAsyncResult result)
-                    {
-                        try
-                        {
-                            socket.EndSendTo(result);
-                        }
-                        catch { }
-                    },
+                    SendCallback,
                     null
                 );
             }
@@ -294,6 +294,15 @@ namespace Hazel.Udp
             }
         }
 
+        private void SendCallback(IAsyncResult result)
+        {
+            try
+            {
+                socket.EndSendTo(result);
+            }
+            catch { }
+        }
+
         /// <summary>
         ///     Sends data from the listener socket.
         /// </summary>
index 530920d3086ca90912662a5a6297e25d0cb68147..5641ca3d95c08913d1aa8a438f9a6b6261f4ff40 100644 (file)
@@ -7,7 +7,7 @@ namespace Hazel.Udp
     ///     Represents a servers's connection to a client that uses the UDP protocol.
     /// </summary>
     /// <inheritdoc/>
-    sealed class UdpServerConnection : UdpConnection
+    internal sealed class UdpServerConnection : UdpConnection
     {
         /// <summary>
         ///     The connection listener that we use the socket of.
@@ -17,7 +17,7 @@ namespace Hazel.Udp
         ///     created this connection and is hence the listener this conenction sends and receives via.
         /// </remarks>
         public UdpConnectionListener Listener { get; private set; }
-        
+
         /// <summary>
         ///     Creates a UdpConnection for the virtual connection to the endpoint.
         /// </summary>