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
{
/// <threadsafety static="true" instance="true"/>
public sealed class ObjectPool<T> where T : IRecyclable
{
+ private int numberCreated;
+ public int NumberCreated { get { return numberCreated; } }
+
public int Size { get { return this.pool.Count; } }
/// <summary>
/// Our pool of objects
/// </summary>
-#if NET_45
ConcurrentBag<T> pool = new ConcurrentBag<T>();
-#else
- Queue<T> pool = new Queue<T>();
-#endif
/// <summary>
/// The generator for creating new objects.
/// <returns>An instance of T.</returns>
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();
}
/// <param name="item">The item to return.</param>
internal void PutObject(T item)
{
-#if NET_45
pool.Add(item);
-#else
- lock (pool)
- pool.Enqueue(item);
-#endif
}
}
}
public volatile bool Acknowledged;
public volatile int Retransmissions;
public Stopwatch Stopwatch = new Stopwatch();
-
+
Packet()
{
{
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"));
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);
}
}
{
lock (this.reliableDataPacketsSent)
{
- var packets = this.reliableDataPacketsSent.Keys.ToArray();
foreach (var kvp in this.reliableDataPacketsSent)
{
Packet pkt = kvp.Value;
+ pkt.Acknowledged = true;
pkt.Recycle();
}