Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Hazel/Tools/PingBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;

namespace Hazel.Tools
{
public class PingBuffer
{
private const ushort InvalidatingFactor = ushort.MaxValue / 2;

private struct PingInfo
{
public ushort Id;
public DateTime SentAt;
}

private PingInfo[] activePings;
private int head; // The location of the next usable activePing

public PingBuffer(int maxPings)
{
this.activePings = new PingInfo[maxPings];

// We don't want the first few packets to match id before we set anything.
for (int i = 0; i < this.activePings.Length; ++i)
{
this.activePings[i].Id = InvalidatingFactor;
}
}

public void AddPing(ushort id)
{
lock (this.activePings)
{
this.activePings[this.head].Id = id;
this.activePings[this.head].SentAt = DateTime.UtcNow;
this.head++;
if (this.head >= this.activePings.Length)
{
this.head = 0;
}
}
}

public bool TryFindPing(ushort id, out DateTime sentAt)
{
lock (this.activePings)
{
for (int i = 0; i < this.activePings.Length; ++i)
{
if (this.activePings[i].Id == id)
{
sentAt = this.activePings[i].SentAt;
this.activePings[i].Id += InvalidatingFactor;
return true;
}
}
}

sentAt = default;
return false;
}
}
}
60 changes: 10 additions & 50 deletions Hazel/Udp/UdpConnection.KeepAlive.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
using System;
using Hazel.Tools;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading;


namespace Hazel.Udp
{
partial class UdpConnection
{

/// <summary>
/// Class to hold packet data
/// </summary>
public class PingPacket : IRecyclable
{
private static readonly ObjectPool<PingPacket> PacketPool = new ObjectPool<PingPacket>(() => new PingPacket());

public readonly Stopwatch Stopwatch = new Stopwatch();

internal static PingPacket GetObject()
{
return PacketPool.GetObject();
}

public void Recycle()
{
Stopwatch.Stop();
PacketPool.PutObject(this);
}
}

internal ConcurrentDictionary<ushort, PingPacket> activePingPackets = new ConcurrentDictionary<ushort, PingPacket>();

/// <summary>
/// The interval from data being received or transmitted to a keepalive packet being sent in milliseconds.
/// </summary>
Expand Down Expand Up @@ -63,6 +39,10 @@ public int KeepAliveInterval
public int MissingPingsUntilDisconnect { get; set; } = 6;
private volatile int pingsSinceAck = 0;

// TODO: Technically, Min(MissingPingsUntilDisconnect + 1, 16) would be better, but I don't want to mess with it.
// The real point is that we're bounding the number of active pings.
private PingBuffer activePings = new PingBuffer(16);

/// <summary>
/// The timer creating keepalive pulses.
/// </summary>
Expand Down Expand Up @@ -116,18 +96,9 @@ private void SendPing()
bytes[1] = (byte)(id >> 8);
bytes[2] = (byte)id;

PingPacket pkt;
if (!this.activePingPackets.TryGetValue(id, out pkt))
{
pkt = PingPacket.GetObject();
if (!this.activePingPackets.TryAdd(id, pkt))
{
throw new Exception("This shouldn't be possible");
}
}

pkt.Stopwatch.Restart();

// TODO: This could overwrite a date, perhaps we should track pings that are simply never ack'd?
this.activePings.AddPing(id);

WriteBytesToConnection(bytes, bytes.Length);

Statistics.LogReliableSend(0);
Expand All @@ -150,18 +121,7 @@ protected void ResetKeepAliveTimer()
/// </summary>
private void DisposeKeepAliveTimer()
{
if (this.keepAliveTimer != null)
{
this.keepAliveTimer.Dispose();
}

foreach (var kvp in activePingPackets)
{
if (this.activePingPackets.TryRemove(kvp.Key, out var pkt))
{
pkt.Recycle();
}
}
this.keepAliveTimer?.Dispose();
}
}
}
6 changes: 2 additions & 4 deletions Hazel/Udp/UdpConnection.Reliable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,9 @@ private void AcknowledgeMessageId(ushort id)
this._pingMs = this._pingMs * .7f + rt * .3f;
}
}
else if (this.activePingPackets.TryRemove(id, out PingPacket pingPkt))
else if (this.activePings.TryFindPing(id, out DateTime pingPkt))
{
float rt = pingPkt.Stopwatch.ElapsedMilliseconds;

pingPkt.Recycle();
float rt = (float)(DateTime.UtcNow - pingPkt).TotalMilliseconds;

lock (PingLock)
{
Expand Down