From 95dcb2a52e65779b037499a0379125e081e8438c Mon Sep 17 00:00:00 2001 From: Forest Date: Sun, 16 Dec 2018 12:01:06 -0800 Subject: [PATCH] Stabilize everything --- Hazel/ObjectPool.cs | 27 ++++++--------------------- Hazel/Udp/UdpConnection.Reliable.cs | 12 ++++++------ 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/Hazel/ObjectPool.cs b/Hazel/ObjectPool.cs index c497b3b..fdc1058 100644 --- a/Hazel/ObjectPool.cs +++ b/Hazel/ObjectPool.cs @@ -1,11 +1,8 @@ using System; -#if NET_45 using System.Collections.Concurrent; -#else -using System.Collections.Generic; -#endif using System.Linq; using System.Text; +using System.Threading; namespace Hazel { @@ -16,16 +13,15 @@ namespace Hazel /// public sealed class ObjectPool where T : IRecyclable { + private int numberCreated; + public int NumberCreated { get { return numberCreated; } } + public int Size { get { return this.pool.Count; } } /// /// Our pool of objects /// -#if NET_45 ConcurrentBag pool = new ConcurrentBag(); -#else - Queue pool = new Queue(); -#endif /// /// The generator for creating new objects. @@ -47,17 +43,11 @@ namespace Hazel /// An instance of T. internal T GetObject() { -#if NET_45 T item; if (pool.TryTake(out item)) return item; -#else - lock (pool) - { - if (pool.Count > 0) - return pool.Dequeue(); - } -#endif + + Interlocked.Increment(ref numberCreated); return objectFactory.Invoke(); } @@ -67,12 +57,7 @@ namespace Hazel /// The item to return. internal void PutObject(T item) { -#if NET_45 pool.Add(item); -#else - lock (pool) - pool.Enqueue(item); -#endif } } } diff --git a/Hazel/Udp/UdpConnection.Reliable.cs b/Hazel/Udp/UdpConnection.Reliable.cs index 7f802ec..6314f5f 100644 --- a/Hazel/Udp/UdpConnection.Reliable.cs +++ b/Hazel/Udp/UdpConnection.Reliable.cs @@ -101,7 +101,7 @@ namespace Hazel.Udp public volatile bool Acknowledged; public volatile int Retransmissions; public Stopwatch Stopwatch = new Stopwatch(); - + Packet() { @@ -189,10 +189,6 @@ namespace Hazel.Udp { if (!p.Acknowledged) { - // Backoff retry frequency to avoid congestion - p.LastTimeout = (int)Math.Min(p.LastTimeout * 1.5f, this.disconnectTimeout / 2f); - p.Timer.Change(p.LastTimeout, Timeout.Infinite); - if (p.Stopwatch.ElapsedMilliseconds > this.disconnectTimeout) { HandleDisconnect(new HazelException($"Reliable packet {id} was not ack'd after {p.Retransmissions} resends")); @@ -203,6 +199,10 @@ namespace Hazel.Udp p.Recycle(); return; } + + // Backoff retry frequency to avoid congestion + p.LastTimeout = (int)Math.Min(p.LastTimeout * 1.5f, this.disconnectTimeout / 2f); + p.Timer.Change(p.LastTimeout, Timeout.Infinite); } } @@ -438,10 +438,10 @@ namespace Hazel.Udp { lock (this.reliableDataPacketsSent) { - var packets = this.reliableDataPacketsSent.Keys.ToArray(); foreach (var kvp in this.reliableDataPacketsSent) { Packet pkt = kvp.Value; + pkt.Acknowledged = true; pkt.Recycle(); } -- 2.39.5