]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Slight optimizations to disconnect
authorForest <chocozilla@gmail.com>
Mon, 31 Dec 2018 05:02:44 +0000 (21:02 -0800)
committerForest <chocozilla@gmail.com>
Mon, 31 Dec 2018 05:02:44 +0000 (21:02 -0800)
Hazel/Connection.cs
Hazel/Udp/UdpClientConnection.cs
Hazel/Udp/UdpConnection.cs
Hazel/Udp/UdpConnectionListener.cs
Hazel/Udp/UdpServerConnection.cs

index 5a8e99195c85a97e79bff713c42fe5d96e7fe013..f136ffc133da399fb1f327a7bcea292e5e34b831 100644 (file)
@@ -52,18 +52,6 @@ namespace Hazel
 
         public int TestLagMs = -1;
         
-        public event Action<byte[], int> DataSentRaw;
-        protected void InvokeDataSentRaw(byte[] data, int length)
-        {
-            this.DataSentRaw?.Invoke(data, length);
-        }
-
-        public event Action<byte[]> DataReceivedRaw;
-        protected void InvokeDataReceivedRaw(byte[] data)
-        {
-            this.DataReceivedRaw?.Invoke(data);
-        }
-
         /// <summary>
         ///     Called when the end point disconnects or an error occurs.
         /// </summary>
@@ -319,8 +307,6 @@ namespace Hazel
             if (disposing)
             {
                 this.DataReceived = null;
-                this.DataReceivedRaw = null;
-                this.DataSentRaw = null;
                 this.Disconnected = null;
             }
         }
index f4ccc1a240196753ecd7ba068f5d250a0dfebd06..048ff60cf18f01a02f814bcbb9aeaa290468a121 100644 (file)
@@ -70,9 +70,13 @@ namespace Hazel.Udp
             }
         }
 
+
+        public event Action<byte[], int> DataSentRaw;
+        public event Action<byte[]> DataReceivedRaw;
+
         private void WriteBytesToConnectionReal(byte[] bytes, int length)
         {
-            InvokeDataSentRaw(bytes, length);
+            DataSentRaw?.Invoke(bytes, length);
 
             if (State != ConnectionState.Connected && State != ConnectionState.Connecting)
                 throw new InvalidOperationException("Could not send data as this Connection is not connected and is not connecting. Did you disconnect?");
@@ -117,7 +121,7 @@ namespace Hazel.Udp
 
         protected override void WriteBytesToConnectionSync(byte[] bytes, int length)
         {
-            InvokeDataSentRaw(bytes, length);
+            DataSentRaw?.Invoke(bytes, length);
 
             if (State != ConnectionState.Connected && State != ConnectionState.Connecting)
                 throw new InvalidOperationException("Could not send data as this Connection is not connected and is not connecting. Did you disconnect?");
@@ -266,10 +270,23 @@ namespace Hazel.Udp
                 Thread.Sleep(this.TestLagMs);
             }
 
+            DataReceivedRaw?.Invoke(bytes);
             MessageReader msg = MessageReader.GetRaw(bytes, 0, bytesReceived);
             HandleReceive(msg, bytesReceived);
         }
 
+        /// <summary>
+        ///     Sends a disconnect message to the end point.
+        /// </summary>
+        protected override void SendDisconnect()
+        {
+            try
+            {
+                WriteBytesToConnectionSync(DisconnectBytes, 1);
+            }
+            catch { }
+        }
+
         /// <inheritdoc />
         protected override void Dispose(bool disposing)
         {
index 89e80e5a7b484aae855cf3ba20654e1d8675ce4a..a488cdeda3feb6f61cfa62e99d7de61b22c34097 100644 (file)
@@ -15,6 +15,8 @@ namespace Hazel.Udp
     /// <inheritdoc />
     public abstract partial class UdpConnection : NetworkConnection
     {
+        protected static readonly byte[] DisconnectBytes = new byte[] { (byte)UdpSendOption.Disconnect };
+
         /// <summary>
         ///     Creates a new UdpConnection and initializes the keep alive timer.
         /// </summary>
@@ -146,8 +148,6 @@ namespace Hazel.Udp
         /// <param name="message">The buffer containing the bytes received.</param>
         protected internal void HandleReceive(MessageReader message, int bytesReceived)
         {
-            InvokeDataReceivedRaw(message.Buffer);
-
             ushort id;
             switch (message.Buffer[0])
             {
@@ -174,7 +174,7 @@ namespace Hazel.Udp
                     break;
 
                 case (byte)UdpSendOption.Disconnect:
-                    Disconnect("The remote sent a disconnect request");
+                    Disconnect("The remote sent a disconnect request", true);
                     message.Recycle();
                     break;
                     
@@ -260,13 +260,18 @@ namespace Hazel.Udp
         /// </summary>
         /// <param name="e">The exception if one was the cause.</param>
         public override void Disconnect(string reason)
+        {
+            this.Disconnect(reason, false);
+        }
+
+        protected void Disconnect(string reason, bool skipSendDisconnect)
         {
             bool invoke = false;
             lock (this)
             {
                 if (this.state == ConnectionState.Connected)
                 {
-                    this.state = ConnectionState.Disconnecting;
+                    this.state = skipSendDisconnect ? ConnectionState.NotConnected : ConnectionState.Disconnecting;
                     invoke = true;
                 }
             }
@@ -282,19 +287,7 @@ namespace Hazel.Udp
 
             this.Dispose();
         }
-
-        /// <summary>
-        ///     Sends a disconnect message to the end point.
-        /// </summary>
-        protected override void SendDisconnect()
-        {
-            try
-            {
-                WriteBytesToConnectionSync(new byte[] { (byte)UdpSendOption.Disconnect }, 1);
-            }
-            catch { }
-        }
-
+        
         /// <inheritdoc/>
         protected override void Dispose(bool disposing)
         {
index 75ea137935a510d8ce246ebe2925c36cf4a303e4..e8f4defbc387e41b5df52b6c08664b23d1d11064 100644 (file)
@@ -134,10 +134,12 @@ namespace Hazel.Udp
         /// <param name="result">The asyncronous operation's result.</param>
         
         public int ActiveListeners;
+        public int PacketsReceived;
 
         void ReadCallback(IAsyncResult result)
         {
             Interlocked.Decrement(ref ActiveListeners);
+            Interlocked.Increment(ref PacketsReceived);
 
             var message = (MessageReader)result.AsyncState;
             int bytesReceived;
@@ -170,11 +172,12 @@ namespace Hazel.Udp
                 return;
             }
 
-            // Exit if no bytes read, we've closed.
+            // I'm a little concerned about a infinite loop here, but it seems like it's possible 
+            // to get 0 bytes read on UDP without the socket being shut down.
             if (bytesReceived == 0)
             {
                 message.Recycle();
-                this.Logger?.Invoke("Stopped due to receiving 0 bytes");
+                StartListeningForData();
                 return;
             }
 
index a9f56b3787ca184948ca02d1e6cf8f91d93d391b..b0f5058477a52648da3236d0217b677b47d8ae9f 100644 (file)
@@ -47,8 +47,6 @@ namespace Hazel.Udp
         /// <inheritdoc />
         protected override void WriteBytesToConnection(byte[] bytes, int length)
         {
-            InvokeDataSentRaw(bytes, length);
-
             if (State != ConnectionState.Connected)
                 throw new InvalidOperationException("Could not send data: Not connected.");
 
@@ -58,8 +56,6 @@ namespace Hazel.Udp
         /// <inheritdoc />
         protected override void WriteBytesToConnectionSync(byte[] bytes, int length)
         {
-            InvokeDataSentRaw(bytes, length);
-
             // No throw: As an internal interface, I want to try sending bytes whenever the I feel like it.
 
             Listener.SendDataSync(bytes, length, RemoteEndPoint);
@@ -82,7 +78,20 @@ namespace Hazel.Udp
         {
             throw new HazelException("Cannot manually connect a UdpServerConnection, did you mean to use UdpClientConnection?");
         }
-        
+
+
+        /// <summary>
+        ///     Sends a disconnect message to the end point.
+        /// </summary>
+        protected override void SendDisconnect()
+        {
+            try
+            {
+                WriteBytesToConnection(DisconnectBytes, 1);
+            }
+            catch { }
+        }
+
         protected override void Dispose(bool disposing)
         {
             Listener.RemoveConnectionTo(RemoteEndPoint);
@@ -97,6 +106,7 @@ namespace Hazel.Udp
                 }
             }
 
+            
             base.Dispose(disposing);
         }
     }