]> git.deb.at Git - rhonda/impostor.hazel.git/commitdiff
Change some more stuff, let's use strings instead of exceptions because I don't like...
authorForest <chocozilla@gmail.com>
Thu, 20 Dec 2018 22:28:18 +0000 (14:28 -0800)
committerForest <chocozilla@gmail.com>
Thu, 20 Dec 2018 22:28:18 +0000 (14:28 -0800)
Hazel/Connection.cs
Hazel/DisconnectedEventArgs.cs
Hazel/Udp/UdpClientConnection.cs
Hazel/Udp/UdpConnection.KeepAlive.cs
Hazel/Udp/UdpConnection.Reliable.cs
Hazel/Udp/UdpConnection.cs
Hazel/Udp/UdpConnectionListener.cs
Hazel/Udp/UdpServerConnection.cs

index 2a96ddaf5d66fcddefb1cfb998f0c783540de5e0..50014bafea5ee6bb75b28eb48567a89c32ad98eb 100644 (file)
@@ -268,7 +268,7 @@ namespace Hazel
         ///     by the end point or because an error occured. If an error occured the error should be passed in in order to 
         ///     pass to the subscribers, otherwise null can be passed in.
         /// </remarks>
-        protected void InvokeDisconnected(Exception e = null)
+        protected void InvokeDisconnected(string e)
         {
             //Make a copy to avoid race condition between null check and invocation
             EventHandler<DisconnectedEventArgs> handler = Disconnected;
index 4e950ade5067c0da0cce876f005e491e7060cc2a..58969c4291019c40d1b9a944059c7f7fbf1347b8 100644 (file)
@@ -36,7 +36,7 @@ namespace Hazel
         ///     that caused it or a <see cref="HazelException"/> with the details of the exception, if the disconnection 
         ///     wasn't caused by an error then this will contain null.
         /// </remarks>
-        public Exception Exception { get; private set; }
+        public string Reason { get; private set; }
 
         /// <summary>
         ///     Private constructor for object pool.
@@ -50,9 +50,9 @@ namespace Hazel
         ///     Sets the given exception for the arguments.
         /// </summary>
         /// <param name="e">The exception if the cause.</param>
-        internal void Set(Exception e)
+        internal void Set(string reason)
         {
-            this.Exception = e;
+            this.Reason = reason;
         }
     }
 }
index 21abdb29085c439ed1c4984e1036511ae27d1630..257e78255e54230f33a702a76acb757501095c43 100644 (file)
@@ -88,11 +88,11 @@ namespace Hazel.Udp
                         }
                         catch (ObjectDisposedException e)
                         {
-                            HandleDisconnect(new HazelException("Could not send as the socket was disposed of.", e));
+                            HandleDisconnect("Could not send as the socket was disposed of.");
                         }
                         catch (SocketException e)
                         {
-                            HandleDisconnect(new HazelException("Could not send data as a SocketException occured.", e));
+                            HandleDisconnect("Could not send data as a SocketException occured.");
                         }
                     },
                     null
@@ -105,14 +105,8 @@ namespace Hazel.Udp
             }
             catch (SocketException e)
             {
-                HazelException he = new HazelException("Could not send data as a SocketException occured.", e);
-                HandleDisconnect(he);
-                throw he;
-            }
-            catch (ArgumentOutOfRangeException e)
-            {
-                HazelException he = new HazelException("Something wonk with the buffer: " + bytes.Length, e);
-                HandleDisconnect(he);
+                HandleDisconnect("Could not send data as a SocketException occured.");
+                throw e;
             }
         }
         
@@ -204,14 +198,14 @@ namespace Hazel.Udp
             }
             catch (SocketException e)
             {
-                HandleDisconnect(new HazelException("A socket exception occured while reading data.", e));
+                HandleDisconnect("A socket exception occured while reading data.");
                 return;
             }
 
             //Exit if no bytes read, we've failed.
             if (bytesReceived == 0)
             {
-                HandleDisconnect(new HazelException("Recieved 0 bytes"));
+                HandleDisconnect("Recieved 0 bytes");
                 return;
             }
 
@@ -226,7 +220,7 @@ namespace Hazel.Udp
             }
             catch (SocketException e)
             {
-                HandleDisconnect(new HazelException("A Socket exception occured while initiating a receive operation.", e));
+                HandleDisconnect("A Socket exception occured while initiating a receive operation.");
             }
             catch (ObjectDisposedException)
             {
@@ -244,7 +238,7 @@ namespace Hazel.Udp
         }
 
         /// <inheritdoc />
-        protected override void HandleDisconnect(HazelException e = null)
+        protected override void HandleDisconnect(string e)
         {
             if (State == ConnectionState.Connected)
             {
index b3353fac0eb88f32a753c07972f0c4660b966088..a9a5f11e49fa7430b71214d9e388dd689bbb6466 100644 (file)
@@ -40,6 +40,8 @@ namespace Hazel.Udp
         }
         int keepAliveInterval = 10000;
 
+        public int KeepAlivesSent;
+
         /// <summary>
         ///     The timer creating keepalive pulses.
         /// </summary>
@@ -63,7 +65,7 @@ namespace Hazel.Udp
                         try
                         {
                             ReliableSend((byte)UdpSendOption.Ping);
-                            Trace.WriteLine("Keepalive packet sent.");
+                            Interlocked.Increment(ref KeepAlivesSent);
                         }
                         catch
                         {
index fcc5497ae505a004f2f699a9735179164e10ea3c..6c6597e353a79328c88a0dca8a2713e719879dec 100644 (file)
@@ -47,7 +47,7 @@ namespace Hazel.Udp
         ///     The packet id that was received last.
         /// </summary>
         volatile ushort reliableReceiveLast = 0;
-
+        
         /// <summary>
         ///     Has the connection received anything yet
         /// </summary>
@@ -102,7 +102,7 @@ namespace Hazel.Udp
             public int LastTimeout;
             public volatile bool Acknowledged;
 
-            private Action<Packet> ResendAction;
+            private Func<Packet, int> ResendAction;
             public Action AckCallback;
 
             public volatile int Retransmissions;
@@ -112,7 +112,7 @@ namespace Hazel.Udp
             {
             }
             
-            internal void Set(ushort id, byte[] data, Action<Packet> resendAction, int timeout, Action ackCallback)
+            internal void Set(ushort id, byte[] data, Func<Packet, int> resendAction, int timeout, Action ackCallback)
             {
                 this.Id = id;
                 this.Data = data;
@@ -127,16 +127,19 @@ namespace Hazel.Udp
                 Stopwatch.Restart();
             }
 
-            public void Resend()
+            // Packets resent
+            public int Resend()
             {
                 var evt = this.ResendAction;
                 if (!this.Acknowledged)
                 {
                     if (evt != null)
                     {
-                        evt(this);
+                        return evt(this);
                     }
                 }
+
+                return 0;
             }
 
             /// <summary>
@@ -150,8 +153,9 @@ namespace Hazel.Udp
             }
         }
                 
-        internal void ManageReliablePackets(object state)
+        internal int ManageReliablePackets(object state)
         {
+            int output = 0;
             if (this.reliableDataPacketsSent.Count > 0)
             {
                 double minTimeout = int.MaxValue;
@@ -163,7 +167,7 @@ namespace Hazel.Udp
                     {
                         try
                         {
-                            pkt.Resend();
+                            output += pkt.Resend();
                         }
                         catch { }
                     }
@@ -171,6 +175,8 @@ namespace Hazel.Udp
                     minTimeout = Math.Min(pkt.LastTimeout, minTimeout);
                 }
             }
+
+            return output;
         }
 
         /// <summary>
@@ -206,7 +212,7 @@ namespace Hazel.Udp
                 (Packet p) =>
                 {
                     // Callback for a previous packet
-                    if (p.Acknowledged) return;
+                    if (p.Acknowledged) return 0;
 
                     p.LastSend = DateTime.Now;
 
@@ -215,12 +221,12 @@ namespace Hazel.Udp
                     {
                         if (reliableDataPacketsSent.TryRemove(p.Id, out self))
                         {
-                            HandleDisconnect(new HazelException($"Reliable packet {self.Id} was not ack'd after {self.Retransmissions} resends"));
+                            HandleDisconnect($"Reliable packet {self.Id} was not ack'd after {self.Retransmissions} resends");
 
                             self.Recycle();
                         }
 
-                        return;
+                        return 0;
                     }
 
                     // Backoff retry frequency to avoid congestion
@@ -230,14 +236,15 @@ namespace Hazel.Udp
                     {
                         WriteBytesToConnection(p.Data, sendLength);
                         p.Retransmissions++;
+                        return 1;
                     }
-                    catch (InvalidOperationException e)
+                    catch (InvalidOperationException)
                     {
                         //No longer connected
-                        HandleDisconnect(new HazelException("Could not resend data as connection is no longer connected", e));
+                        HandleDisconnect("Could not resend data as connection is no longer connected");
                     }
 
-                    Trace.WriteLine("Resend.");
+                    return 0;
                 },
                 timeout,
                 ackCallback
index ee05df15ea192570f8ebb73e97d9fe356188e722..3d6c720a5f1d3b46dd0debf05bd9f97275d45569 100644 (file)
@@ -168,7 +168,7 @@ namespace Hazel.Udp
                     break;
 
                 case (byte)UdpSendOption.Disconnect:
-                    HandleDisconnect(new HazelException("The remote sent a disconnect request"));
+                    HandleDisconnect("The remote sent a disconnect request");
                     message.Recycle();
                     break;
                     
@@ -253,7 +253,7 @@ namespace Hazel.Udp
         ///     Called when the socket has been disconnected at the remote host.
         /// </summary>
         /// <param name="e">The exception if one was the cause.</param>
-        protected abstract void HandleDisconnect(HazelException e = null);
+        protected abstract void HandleDisconnect(string reason);
 
         /// <summary>
         ///     Sends a disconnect message to the end point.
index a951f2a9dba55e54da8b068dc7b91d4a0a05dcfa..3459a0684f87bd2a6d11a34b26618b5d3ea3b13d 100644 (file)
@@ -69,13 +69,15 @@ namespace Hazel.Udp
         }
 
         public float AveragePacketsTime = 1;
+        public int PacketsResent = 0;
+
         Stopwatch stopwatch = new Stopwatch();
         private void ManageReliablePackets(object state)
         {
             stopwatch.Restart();
             foreach (var kvp in this.allConnections)
             {
-                kvp.Value.ManageReliablePackets(state);
+                PacketsResent += kvp.Value.ManageReliablePackets(state);
             }
 
             this.AveragePacketsTime = this.AveragePacketsTime * .7f + stopwatch.ElapsedMilliseconds * .3f;
index 99b9bc8380141d980292b1d3b2525f0d9609db1b..27f593caaa7c7fa8fb6f1bc14bf38ea20f2a4188 100644 (file)
@@ -77,7 +77,7 @@ namespace Hazel.Udp
         }
 
         /// <inheritdoc />
-        protected override void HandleDisconnect(HazelException e = null)
+        protected override void HandleDisconnect(string reason)
         {
             bool invoke = false;
 
@@ -96,7 +96,7 @@ namespace Hazel.Udp
             {
                 try
                 {
-                    InvokeDisconnected(e);
+                    InvokeDisconnected(reason);
                 }
                 catch { }