]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
This is what I'm using now, 1100 users on my single core, but after moving to multico...
authorForest <chocozilla@gmail.com>
Sat, 22 Dec 2018 22:03:36 +0000 (14:03 -0800)
committerForest <chocozilla@gmail.com>
Sat, 22 Dec 2018 22:03:36 +0000 (14:03 -0800)
Hazel/Udp/UdpClientConnection.cs
Hazel/Udp/UdpConnection.KeepAlive.cs
Hazel/Udp/UdpConnection.Reliable.cs
Hazel/Udp/UdpConnectionListener.cs

index 257e78255e54230f33a702a76acb757501095c43..0752d2c7b7b209014404d1a0136aedd38382c888 100644 (file)
@@ -25,6 +25,8 @@ namespace Hazel.Udp
         /// </summary>
         byte[] dataBuffer = new byte[ushort.MaxValue];
 
+        Timer reliablePacketTimer;
+
         /// <summary>
         ///     Creates a new UdpClientConnection.
         /// </summary>
@@ -46,6 +48,8 @@ namespace Hazel.Udp
                 socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
                 socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false);    //TODO these lines shouldn't be needed anymore
             }
+
+            reliablePacketTimer = new Timer((s) => ManageReliablePackets(s), null, 50, Timeout.Infinite);
         }
 
         ~UdpClientConnection()
@@ -60,12 +64,14 @@ namespace Hazel.Udp
             {
                 ThreadPool.QueueUserWorkItem(a => { Thread.Sleep(this.TestLagMs); WriteBytesToConnectionReal(bytes, length); });
             }
-
-            WriteBytesToConnectionReal(bytes, length);
+            else
+            {
+                WriteBytesToConnectionReal(bytes, length);
+            }
         }
 
         private void WriteBytesToConnectionReal(byte[] bytes, int length)
-        { 
+        {
             InvokeDataSentRaw(bytes, length);
 
             if (State != ConnectionState.Connected && State != ConnectionState.Connecting)
@@ -83,14 +89,13 @@ namespace Hazel.Udp
                     {
                         try
                         {
-                            lock (socket)
-                                socket.EndSendTo(result);
+                            socket.EndSendTo(result);
                         }
-                        catch (ObjectDisposedException e)
+                        catch (ObjectDisposedException)
                         {
                             HandleDisconnect("Could not send as the socket was disposed of.");
                         }
-                        catch (SocketException e)
+                        catch (SocketException)
                         {
                             HandleDisconnect("Could not send data as a SocketException occured.");
                         }
@@ -103,13 +108,13 @@ namespace Hazel.Udp
                 //User probably called Disconnect in between this method starting and here so report the issue
                 throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?");
             }
-            catch (SocketException e)
+            catch (SocketException)
             {
                 HandleDisconnect("Could not send data as a SocketException occured.");
-                throw e;
+                throw;
             }
         }
-        
+
         /// <inheritdoc />
         public override void Connect(byte[] bytes = null, int timeout = 5000)
         {
@@ -129,8 +134,8 @@ namespace Hazel.Udp
         /// <inheritdoc />
         public override void ConnectAsync(byte[] bytes = null, int timeout = 5000)
         {
-                if (State != ConnectionState.NotConnected)
-                    throw new InvalidOperationException("Cannot connect as the Connection is already connected.");
+            if (State != ConnectionState.NotConnected)
+                throw new InvalidOperationException("Cannot connect as the Connection is already connected.");
 
             State = ConnectionState.Connecting;
 
@@ -278,7 +283,9 @@ namespace Hazel.Udp
                 socket = null;
             }
 
+            this.reliablePacketTimer.Dispose();
+
             base.Dispose(disposing);
         }
     }
-}
+}
\ No newline at end of file
index a9a5f11e49fa7430b71214d9e388dd689bbb6466..8227a6fd7d6890185a5560baec5b3ab3f8fd445b 100644 (file)
@@ -38,7 +38,7 @@ namespace Hazel.Udp
                 ResetKeepAliveTimer();
             }
         }
-        int keepAliveInterval = 10000;
+        int keepAliveInterval = 3000;
 
         public int KeepAlivesSent;
 
index 6c6597e353a79328c88a0dca8a2713e719879dec..23be344af54b69f474c4c6b807ab3b144f39b52d 100644 (file)
@@ -47,7 +47,9 @@ namespace Hazel.Udp
         ///     The packet id that was received last.
         /// </summary>
         volatile ushort reliableReceiveLast = 0;
-        
+
+        public int DuplicateRecieves;
+
         /// <summary>
         ///     Has the connection received anything yet
         /// </summary>
@@ -215,6 +217,7 @@ namespace Hazel.Udp
                     if (p.Acknowledged) return 0;
 
                     p.LastSend = DateTime.Now;
+                    p.LastTimeout = (int)Math.Min(p.LastTimeout * 1.5f, 3000);
 
                     Packet self;
                     if (p.Stopwatch.ElapsedMilliseconds > this.disconnectTimeout)
@@ -229,8 +232,6 @@ namespace Hazel.Udp
                         return 0;
                     }
 
-                    // Backoff retry frequency to avoid congestion
-                    p.LastTimeout = (int)Math.Min(p.LastTimeout * 1.25f, 1000);
 
                     try
                     {
@@ -319,6 +320,7 @@ namespace Hazel.Udp
             }
             else
             {
+                Interlocked.Increment(ref this.DuplicateRecieves);
                 message.Recycle();
             }
 
index 3459a0684f87bd2a6d11a34b26618b5d3ea3b13d..0c2239aa87cafdf5fc4e39f0133b7e2b10e3b111 100644 (file)
@@ -27,9 +27,7 @@ namespace Hazel.Udp
         ///     The socket listening for connections.
         /// </summary>
         Socket listener;
-
-        private Action<string> Logger;
-
+        
         Timer reliablePacketTimer;
 
         /// <summary>
@@ -43,9 +41,8 @@ namespace Hazel.Udp
         ///     Creates a new UdpConnectionListener for the given <see cref="IPAddress"/>, port and <see cref="IPMode"/>.
         /// </summary>
         /// <param name="endPoint">The endpoint to listen on.</param>
-        public UdpConnectionListener(NetworkEndPoint endPoint, Action<string> logger = null)
+        public UdpConnectionListener(NetworkEndPoint endPoint)
         {
-            this.Logger = logger;
             this.EndPoint = endPoint.EndPoint;
             this.IPMode = endPoint.IPMode;
 
@@ -70,6 +67,8 @@ namespace Hazel.Udp
 
         public float AveragePacketsTime = 1;
         public int PacketsResent = 0;
+        public int KeepAlives = 0;
+        public int DuplicateRecieves = 0;
 
         Stopwatch stopwatch = new Stopwatch();
         private void ManageReliablePackets(object state)
@@ -77,7 +76,10 @@ namespace Hazel.Udp
             stopwatch.Restart();
             foreach (var kvp in this.allConnections)
             {
-                PacketsResent += kvp.Value.ManageReliablePackets(state);
+                var sock = kvp.Value;
+                PacketsResent += sock.ManageReliablePackets(state);
+                KeepAlives += Interlocked.Exchange(ref sock.KeepAlivesSent, 0);
+                DuplicateRecieves += Interlocked.Exchange(ref sock.DuplicateRecieves, 0);
             }
 
             this.AveragePacketsTime = this.AveragePacketsTime * .7f + stopwatch.ElapsedMilliseconds * .3f;
@@ -128,7 +130,7 @@ namespace Hazel.Udp
                 return;
             }
         }
-
+        
         /// <summary>
         ///     Called when data has been received by the listener.
         /// </summary>
@@ -223,20 +225,8 @@ namespace Hazel.Udp
                 }
             }
 
-            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))}");
-                }
-            }
+            //Inform the connection of the buffer (new connections need to send an ack back to client)
+            connection.HandleReceive(message, bytesReceived);
 
             //If it's a new connection invoke the NewConnection event.
             if (!aware)
@@ -329,10 +319,7 @@ namespace Hazel.Udp
         /// <param name="endPoint">The endpoint of the virtual connection.</param>
         internal void RemoveConnectionTo(EndPoint endPoint)
         {
-            lock (this.allConnections)
-            {
-                this.allConnections.TryRemove(endPoint, out var conn);
-            }
+            this.allConnections.TryRemove(endPoint, out var conn);
         }
 
         /// <inheritdoc />