]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Allow for a little more instrumenting, maybe stop a race
authorForest <chocozilla@gmail.com>
Tue, 18 Dec 2018 23:57:24 +0000 (15:57 -0800)
committerForest <chocozilla@gmail.com>
Tue, 18 Dec 2018 23:57:24 +0000 (15:57 -0800)
Hazel/ObjectPool.cs
Hazel/Udp/UdpConnection.Reliable.cs
Hazel/Udp/UdpConnectionListener.cs

index b97907cd935a410a1845cd482c5e35747a88b11b..0da7dbf39e3e36e272cff993844d0aff0bd37cf1 100644 (file)
@@ -15,6 +15,7 @@ namespace Hazel
     {
         private int numberCreated;
         public int NumberCreated { get { return numberCreated; } }
+        public int NumberInUse { get { return this.inuse.Count; } }
 
         public int Size { get { return this.pool.Count; } }
 
index 16554dacb7d573e3fcba7ff29e08b63b62ce3204..316990bf3152aa98c1253067201c4a1dd71c0f57 100644 (file)
@@ -79,12 +79,12 @@ namespace Hazel.Udp
         /// <summary>
         ///     Class to hold packet data
         /// </summary>
-        class Packet : IRecyclable, IDisposable
+        public class Packet : IRecyclable, IDisposable
         {
             /// <summary>
             ///     Object pool for this event.
             /// </summary>
-            static readonly ObjectPool<Packet> objectPool = new ObjectPool<Packet>(() => new Packet());
+            public static readonly ObjectPool<Packet> PacketPool = new ObjectPool<Packet>(() => new Packet());
 
             /// <summary>
             ///     Returns an instance of this object from the pool.
@@ -92,7 +92,7 @@ namespace Hazel.Udp
             /// <returns></returns>
             internal static Packet GetObject()
             {
-                return objectPool.GetObject();
+                return PacketPool.GetObject();
             }
 
             public ushort Id;
@@ -143,7 +143,7 @@ namespace Hazel.Udp
                     }
                 }
 
-                objectPool.PutObject(this);
+                PacketPool.PutObject(this);
             }
 
             /// <summary>
@@ -223,7 +223,7 @@ namespace Hazel.Udp
                             if (p.Id != id) return;
 
                             // Backoff retry frequency to avoid congestion
-                            p.LastTimeout = (int)Math.Min(p.LastTimeout * 1.5f, this.disconnectTimeout / 2f);
+                            p.LastTimeout = (int)Math.Min(p.LastTimeout * 1.25f, 2000);
                             p.Timer.Change(p.LastTimeout, Timeout.Infinite);
                         }
 
@@ -240,7 +240,7 @@ namespace Hazel.Udp
 
                         Trace.WriteLine("Resend.");
                     },
-                    resendTimeout > 0 ? resendTimeout : (int)Math.Max(100, Math.Min(AveragePingMs * 4, 1500)),
+                    resendTimeout > 0 ? resendTimeout : (int)Math.Max(500, Math.Min(AveragePingMs * 4, 2000)),
                     ackCallback
                 );
             }
@@ -422,7 +422,7 @@ namespace Hazel.Udp
 
                 lock (PingLock)
                 {
-                    this.AveragePingMs = Math.Max(10, this.AveragePingMs * .7f + rt * .3f);
+                    this.AveragePingMs = Math.Max(50, this.AveragePingMs * .7f + rt * .3f);
                 }
             }
 
index bcefc867144ab8d580a24bfa802c8124f2e97b8a..6252e5889b3e46e723e087641209d3b1b0e54bc8 100644 (file)
@@ -23,7 +23,9 @@ namespace Hazel.Udp
         ///     The socket listening for connections.
         /// </summary>
         Socket listener;
-        
+
+        private Action<string> Logger;
+
         /// <summary>
         ///     The connections we currently hold
         /// </summary>
@@ -37,10 +39,10 @@ namespace Hazel.Udp
         /// <param name="port">The port to listen on.</param>
         /// <param name="mode">The <see cref="IPMode"/> to listen with.</param>
         [Obsolete("Temporary constructor in beta only, use NetworkEndPoint constructor instead.")]
-        public UdpConnectionListener(IPAddress IPAddress, int port, IPMode mode = IPMode.IPv4)
+        public UdpConnectionListener(IPAddress IPAddress, int port, Action<string> logger, IPMode mode = IPMode.IPv4)
             : this (new NetworkEndPoint(IPAddress, port, mode))
         {
-
+            this.Logger = logger;
         }
 
         /// <summary>
@@ -163,22 +165,23 @@ namespace Hazel.Udp
                 message.Recycle();
                 return;
             }
-            
+
             //Begin receiving again
             StartListeningForData();
 
-            bool aware;
-            bool isHello = message.Buffer[0] == (byte)UdpSendOption.Hello
-                && message.Length >= MinConnectionLength;
+            bool aware = true;
+            bool hasHelloByte = message.Buffer[0] == (byte)UdpSendOption.Hello;
+            bool isHello = hasHelloByte && message.Length >= MinConnectionLength;
 
             //If we're aware of this connection use the one already
             //If this is a new client then connect with them!
             UdpServerConnection connection;
-            if (!(aware = this.allConnections.TryGetValue(remoteEndPoint, out connection)))
+            if (!this.allConnections.TryGetValue(remoteEndPoint, out connection))
             {
                 lock (this.allConnections)
                 {
-                    if (!(aware = this.allConnections.TryGetValue(remoteEndPoint, out connection)))
+                    aware = this.allConnections.TryGetValue(remoteEndPoint, out connection);
+                    if (!aware)
                     {
                         //Check for malformed connection attempts
                         if (!isHello)
@@ -189,24 +192,39 @@ namespace Hazel.Udp
                         }
 
                         connection = new UdpServerConnection(this, remoteEndPoint, this.IPMode);
-                        this.allConnections.TryAdd(remoteEndPoint, connection);
+                        if (!this.allConnections.TryAdd(remoteEndPoint, connection))
+                        {
+                            throw new Exception();
+                        }
                     }
                 }
             }
 
-            //Inform the connection of the buffer (new connections need to send an ack back to client)
-            connection.HandleReceive(message, bytesReceived);
+            var stopwatch = System.Diagnostics.Stopwatch.StartNew();
+            try
+            {
+                //Inform the connection of the buffer (new connections need to send an ack back to client)
+                connection.HandleReceive(message, bytesReceived);
+            }
+            finally
+            {
+                var el = stopwatch.ElapsedMilliseconds;
+                if (el > 5)
+                {
+                    this.Logger?.Invoke($"Long Packet {el}ms = {string.Join(" ", message.Buffer.Take(bytesReceived))}");
+                }
+            }
 
             //If it's a new connection invoke the NewConnection event.
             if (!aware)
             {
                 // Skip header and hello byte;
-                message.Offset = 4; 
+                message.Offset = 4;
                 message.Length = bytesReceived - 4;
                 message.Position = 0;
                 InvokeNewConnection(message, connection);
             }
-            else if (isHello)
+            else if (isHello || (!isHello && hasHelloByte))
             {
                 message.Recycle();
             }
@@ -287,7 +305,10 @@ namespace Hazel.Udp
         /// <param name="endPoint">The endpoint of the virtual connection.</param>
         internal void RemoveConnectionTo(EndPoint endPoint)
         {
-            this.allConnections.TryRemove(endPoint, out var conn);
+            lock (this.allConnections)
+            {
+                this.allConnections.TryRemove(endPoint, out var conn);
+            }
         }
 
         /// <inheritdoc />