using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
-using System.Threading;
namespace Hazel
{
{
private int numberCreated;
public int NumberCreated { get { return numberCreated; } }
+
public int NumberInUse { get { return this.inuse.Count; } }
+ public int NumberNotInUse { get { return this.pool.Count; } }
- public int Size { get { return this.pool.Count; } }
+ // Available objects
+ private readonly ConcurrentBag<T> pool = new ConcurrentBag<T>();
- /// <summary>
- /// Our pool of objects
- /// </summary>
- ConcurrentBag<T> pool = new ConcurrentBag<T>();
-
- private ConcurrentDictionary<T, bool> inuse = new ConcurrentDictionary<T, bool>();
+ // Unavailable objects
+ private readonly ConcurrentDictionary<T, bool> inuse = new ConcurrentDictionary<T, bool>();
/// <summary>
/// The generator for creating new objects.
/// </summary>
/// <returns></returns>
- Func<T> objectFactory;
-
+ private readonly Func<T> objectFactory;
+
/// <summary>
/// Internal constructor for our ObjectPool.
/// </summary>
using System;
using System.Collections.Concurrent;
-using System.Collections.Generic;
using System.Diagnostics;
-using System.Linq;
-using System.Text;
using System.Threading;
// Pings are special, quasi-reliable packets.
// We send them to trigger responses that validate our connection is alive
- // They should never be the *cause* of a disconnect.
- // Rather, the responses will reset our
+ // An unacked ping should never be the sole cause of a disconnect.
+ // Rather, the responses will reset our pingsSinceAck, enough unacked
+ // pings should cause a disconnect.
void SendPing()
{
ushort id = (ushort)Interlocked.Increment(ref lastIDAllocated);
this.keepAliveTimer = null;
timer.Dispose();
}
+
+ foreach (var kvp in activePingPackets)
+ {
+ if (this.activePingPackets.TryRemove(kvp.Key, out var pkt))
+ {
+ pkt.Recycle();
+ }
+ }
}
}
}
\ No newline at end of file
/// <param name="ackCallback">The callback to make once the packet has been acknowledged.</param>
void AttachReliableID(byte[] buffer, int offset, int sendLength, Action ackCallback = null)
{
- //Find an ID not used yet.
ushort id = (ushort)Interlocked.Increment(ref lastIDAllocated);
buffer[offset] = (byte)(id >> 8);
buffer[offset + 1] = (byte)id;
- //Create packet object
Packet packet = Packet.GetObject();
packet.Set(
id,
//Get ID
ushort id = (ushort)((bytes[1] << 8) + bytes[2]);
- //Dispose of timer and remove from dictionary
- Packet packet;
- if (reliableDataPacketsSent.TryRemove(id, out packet))
+ // Dispose of timer and remove from dictionary
+ if (reliableDataPacketsSent.TryRemove(id, out Packet packet))
{
float rt = packet.Stopwatch.ElapsedMilliseconds;
else if (this.activePingPackets.TryRemove(id, out PingPacket pingPkt))
{
float rt = pingPkt.Stopwatch.ElapsedMilliseconds;
+
+ pingPkt.Recycle();
+
lock (PingLock)
{
this.AveragePingMs = Math.Max(50, this.AveragePingMs * .7f + rt * .3f);
}
-
- pingPkt.Recycle();
}
Statistics.LogReliableReceive(0, bytes.Length);
{
foreach (var kvp in reliableDataPacketsSent)
{
- Packet pkt;
- if (this.reliableDataPacketsSent.TryRemove(kvp.Key, out pkt))
+ if (this.reliableDataPacketsSent.TryRemove(kvp.Key, out var pkt))
{
pkt.Recycle();
}
/// <inheritdoc/>
public override void Send(MessageWriter msg)
{
- //Early check
if (this._state != ConnectionState.Connected)
throw new InvalidOperationException("Could not send data as this Connection is not connected. Did you disconnect?");
}
catch (SocketException)
{
- //Client no longer reachable, pretend it didn't happen
- //TODO possibly able to disconnect client, see other TODO
message?.Recycle();
StartListeningForData();
return;
catch (Exception ex)
{
//If the socket's been disposed then we can just end there.
+ message.Recycle();
this.Logger?.Invoke("Stopped due to: " + ex.Message);
return;
}
catch (Exception ex)
{
//If the socket's been disposed then we can just end there.
+ message.Recycle();
this.Logger?.Invoke("Stopped due to: " + ex.Message);
return;
}
if (this._state == ConnectionState.Connected
|| this._state == ConnectionState.Disconnecting)
{
- SendDisconnect();
this._state = ConnectionState.NotConnected;
+ SendDisconnect();
}
}