From 9a726504770f703548d9de1ef2b3197a9a43e1fb Mon Sep 17 00:00:00 2001 From: JamJar00 Date: Tue, 17 May 2016 14:13:30 +0100 Subject: [PATCH] Fixes and help project --- Hazel/Hazel.csproj | 3 ++ Hazel/Hazel.shfbproj | 77 +++++++++++++++++++++++++++++++++ Hazel/UdpConnection.Reliable.cs | 43 +++++++++++++++--- 3 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 Hazel/Hazel.shfbproj diff --git a/Hazel/Hazel.csproj b/Hazel/Hazel.csproj index a4cc7dc..9547f64 100644 --- a/Hazel/Hazel.csproj +++ b/Hazel/Hazel.csproj @@ -20,6 +20,8 @@ DEBUG;TRACE prompt 4 + + pdbonly @@ -28,6 +30,7 @@ TRACE prompt 4 + bin\Release\Hazel.XML true diff --git a/Hazel/Hazel.shfbproj b/Hazel/Hazel.shfbproj new file mode 100644 index 0000000..0086936 --- /dev/null +++ b/Hazel/Hazel.shfbproj @@ -0,0 +1,77 @@ + + + + + Debug + AnyCPU + 2.0 + {359995e0-bef3-42dd-800f-20368ff5fabb} + 2015.6.5.0 + + Documentation + Documentation + Documentation + + .NET Framework 4.5 + .\Help\ + Documentation + en-US + 100 + OnlyWarningsAndErrors + Website + False + True + False + True + + + + 1.0.0.0 + 2 + False + Standard + Blank + False + VS2013 + False + Guid + Hazel Networking + AboveNamespaces + + + + + + + + + + + + + + + + + + + + + + + + + + + OnBuildSuccess + + + + Hazel + {02CFBD30-D77D-400F-94B2-700F60EFDD7F} + + + \ No newline at end of file diff --git a/Hazel/UdpConnection.Reliable.cs b/Hazel/UdpConnection.Reliable.cs index de46771..80038c6 100644 --- a/Hazel/UdpConnection.Reliable.cs +++ b/Hazel/UdpConnection.Reliable.cs @@ -19,7 +19,7 @@ namespace Hazel /// On each resend this is doubled for that packet. /// public int ResendTimeout { get { return resendTimeout; } set { resendTimeout = value; } } - private int resendTimeout = 200; //TODO this based of average ping? + private volatile int resendTimeout = 200; //TODO this based of average ping? /// /// Holds the last ID allocated. @@ -46,10 +46,16 @@ namespace Hazel /// volatile bool hasReceivedSomething = false; + /// + /// The maximum times a message should be retransmitted before marking the endpoint as disconnected. + /// + public int RetransmissionsBeforeDisconnect { get { return retransmissionsBeforeDisconnect; } set { retransmissionsBeforeDisconnect = value; } } + private volatile int retransmissionsBeforeDisconnect = 3; + /// /// Class to hold packet data /// - class Packet : IRecyclable + class Packet : IRecyclable, IDisposable { /// /// Object pool for this event. @@ -70,6 +76,7 @@ namespace Hazel public volatile int LastTimeout; public Action AckCallback; public volatile bool Acknowledged; + public volatile int Retransmissions; Packet() { @@ -90,6 +97,7 @@ namespace Hazel LastTimeout = timeout; AckCallback = ackCallback; Acknowledged = false; + Retransmissions = 0; } /// @@ -97,8 +105,29 @@ namespace Hazel /// public void Recycle() { + lock (Timer) + Timer.Dispose(); + objectPool.PutObject(this); } + + /// + /// Disposes of this object. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected void Dispose(bool disposing) + { + if (disposing) + { + lock (Timer) + Timer.Dispose(); + } + } } /// @@ -132,7 +161,14 @@ namespace Hazel lock (p.Timer) { if (!p.Acknowledged) + { p.Timer.Change(p.LastTimeout *= 2, Timeout.Infinite); //TODO disconnect after x tries + if (++p.Retransmissions >= RetransmissionsBeforeDisconnect) + { + HandleDisconnect(); + p.Recycle(); + } + } } Trace.WriteLine("Resend."); @@ -211,9 +247,6 @@ namespace Hazel packet.Acknowledged = true; - lock (packet.Timer) - packet.Timer.Dispose(); - if (packet.AckCallback != null) packet.AckCallback.Invoke(); -- 2.39.5