From: Forest Date: Thu, 20 Dec 2018 22:28:18 +0000 (-0800) Subject: Change some more stuff, let's use strings instead of exceptions because I don't like... X-Git-Tag: 1.0.0~67 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=eac8183ad36ed69f1f09db3ca78334d9a150a0c5;p=rhonda%2Fimpostor.hazel.git Change some more stuff, let's use strings instead of exceptions because I don't like stack unwind for disconnects --- diff --git a/Hazel/Connection.cs b/Hazel/Connection.cs index 2a96dda..50014ba 100644 --- a/Hazel/Connection.cs +++ b/Hazel/Connection.cs @@ -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. /// - protected void InvokeDisconnected(Exception e = null) + protected void InvokeDisconnected(string e) { //Make a copy to avoid race condition between null check and invocation EventHandler handler = Disconnected; diff --git a/Hazel/DisconnectedEventArgs.cs b/Hazel/DisconnectedEventArgs.cs index 4e950ad..58969c4 100644 --- a/Hazel/DisconnectedEventArgs.cs +++ b/Hazel/DisconnectedEventArgs.cs @@ -36,7 +36,7 @@ namespace Hazel /// that caused it or a with the details of the exception, if the disconnection /// wasn't caused by an error then this will contain null. /// - public Exception Exception { get; private set; } + public string Reason { get; private set; } /// /// Private constructor for object pool. @@ -50,9 +50,9 @@ namespace Hazel /// Sets the given exception for the arguments. /// /// The exception if the cause. - internal void Set(Exception e) + internal void Set(string reason) { - this.Exception = e; + this.Reason = reason; } } } diff --git a/Hazel/Udp/UdpClientConnection.cs b/Hazel/Udp/UdpClientConnection.cs index 21abdb2..257e782 100644 --- a/Hazel/Udp/UdpClientConnection.cs +++ b/Hazel/Udp/UdpClientConnection.cs @@ -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 } /// - protected override void HandleDisconnect(HazelException e = null) + protected override void HandleDisconnect(string e) { if (State == ConnectionState.Connected) { diff --git a/Hazel/Udp/UdpConnection.KeepAlive.cs b/Hazel/Udp/UdpConnection.KeepAlive.cs index b3353fa..a9a5f11 100644 --- a/Hazel/Udp/UdpConnection.KeepAlive.cs +++ b/Hazel/Udp/UdpConnection.KeepAlive.cs @@ -40,6 +40,8 @@ namespace Hazel.Udp } int keepAliveInterval = 10000; + public int KeepAlivesSent; + /// /// The timer creating keepalive pulses. /// @@ -63,7 +65,7 @@ namespace Hazel.Udp try { ReliableSend((byte)UdpSendOption.Ping); - Trace.WriteLine("Keepalive packet sent."); + Interlocked.Increment(ref KeepAlivesSent); } catch { diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index fcc5497..6c6597e 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -47,7 +47,7 @@ namespace Hazel.Udp /// The packet id that was received last. /// volatile ushort reliableReceiveLast = 0; - + /// /// Has the connection received anything yet /// @@ -102,7 +102,7 @@ namespace Hazel.Udp public int LastTimeout; public volatile bool Acknowledged; - private Action ResendAction; + private Func ResendAction; public Action AckCallback; public volatile int Retransmissions; @@ -112,7 +112,7 @@ namespace Hazel.Udp { } - internal void Set(ushort id, byte[] data, Action resendAction, int timeout, Action ackCallback) + internal void Set(ushort id, byte[] data, Func 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; } /// @@ -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; } /// @@ -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 diff --git a/Hazel/Udp/UdpConnection.cs b/Hazel/Udp/UdpConnection.cs index ee05df1..3d6c720 100644 --- a/Hazel/Udp/UdpConnection.cs +++ b/Hazel/Udp/UdpConnection.cs @@ -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. /// /// The exception if one was the cause. - protected abstract void HandleDisconnect(HazelException e = null); + protected abstract void HandleDisconnect(string reason); /// /// Sends a disconnect message to the end point. diff --git a/Hazel/Udp/UdpConnectionListener.cs b/Hazel/Udp/UdpConnectionListener.cs index a951f2a..3459a06 100644 --- a/Hazel/Udp/UdpConnectionListener.cs +++ b/Hazel/Udp/UdpConnectionListener.cs @@ -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; diff --git a/Hazel/Udp/UdpServerConnection.cs b/Hazel/Udp/UdpServerConnection.cs index 99b9bc8..27f593c 100644 --- a/Hazel/Udp/UdpServerConnection.cs +++ b/Hazel/Udp/UdpServerConnection.cs @@ -77,7 +77,7 @@ namespace Hazel.Udp } /// - 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 { }