Skip to content

Commit

Permalink
Revert back to not have the optimize send code in here
Browse files Browse the repository at this point in the history
  • Loading branch information
HakanL committed Mar 3, 2024
1 parent 8480899 commit 5b6c76c
Showing 1 changed file with 0 additions and 81 deletions.
81 changes: 0 additions & 81 deletions src/Haukcode.sACN/SACNClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,6 @@ public SendData()
}
}

public class OutputData : IDisposable
{
public Stopwatch LastSent = Stopwatch.StartNew();

public double AgeMS => LastSent.Elapsed.TotalMilliseconds;

public IMemoryOwner<byte> SendData;

public void Dispose()
{
SendData.Dispose();
}
}

private const int ReceiveBufferSize = 20480;
private const int SendBufferSize = 1024;
private static readonly IPEndPoint _blankEndpoint = new(IPAddress.Any, 0);
Expand All @@ -83,7 +69,6 @@ public void Dispose()
private int droppedPackets;
private int slowSends;
private readonly HashSet<(IPAddress Destination, ushort UniverseId)> usedDestinations = new();
private readonly Dictionary<(IPAddress Destination, ushort UniverseId), OutputData> outputDataPerDestination = new();

public SACNClient(Guid senderId, string senderName, IPAddress localAddress, int port = 5568)
{
Expand Down Expand Up @@ -407,18 +392,11 @@ public void DropDMXUniverse(ushort universeId)
/// <param name="startCode">Start code (default 0)</param>
public void SendMulticast(ushort universeId, ReadOnlyMemory<byte> data, byte priority = 100, ushort syncAddress = 0, byte startCode = 0)
{
// See if we should send
if (SkipSending(null, universeId, data))
return;

byte sequenceId = GetNewSequenceId(universeId);

var packet = new SACNDataPacket(universeId, SenderName, SenderId, sequenceId, data, priority, syncAddress, startCode);

SendPacket(universeId, packet);

var outputData = UpdateOutputData(null, universeId);
data.CopyTo(outputData.SendData.Memory);
}

/// <summary>
Expand All @@ -431,18 +409,11 @@ public void SendMulticast(ushort universeId, ReadOnlyMemory<byte> data, byte pri
/// <param name="startCode">Start code (default 0)</param>
public void SendUnicast(IPAddress address, ushort universeId, ReadOnlyMemory<byte> data, byte priority = 100, ushort syncAddress = 0, byte startCode = 0)
{
// See if we should send
if (SkipSending(address, universeId, data))
return;

byte sequenceId = GetNewSequenceId(universeId);

var packet = new SACNDataPacket(universeId, SenderName, SenderId, sequenceId, data, priority, syncAddress, startCode);

SendPacket(universeId, address, packet);

var outputData = UpdateOutputData(address, universeId);
data.CopyTo(outputData.SendData.Memory);
}

/// <summary>
Expand Down Expand Up @@ -566,53 +537,6 @@ public void SendPacket(ushort universeId, SACNPacket packet)
//}
}

private bool SkipSending(IPAddress destination, ushort universeId, ReadOnlyMemory<byte> data)
{
if (!OptimizeSend)
return false;

OutputData outputData;
lock (this.lockObject)
{
if (!this.outputDataPerDestination.TryGetValue((destination, universeId), out outputData))
return false;
}

if (outputData.AgeMS > 1_000)
// Send at least once every second
return false;

// Check if the data has changed
if (data.Length != outputData.SendData.Memory.Length)
return false;

if (data.Span.SequenceEqual(outputData.SendData.Memory.Span))
// Identical
return true;

return false;
}

private OutputData UpdateOutputData(IPAddress destination, ushort universeId)
{
lock (this.lockObject)
{
if (!this.outputDataPerDestination.TryGetValue((destination, universeId), out var outputData))
{
outputData = new OutputData
{
SendData = this.memoryPool.Rent(512)
};

this.outputDataPerDestination.Add((destination, universeId), outputData);
}

outputData.LastSent.Restart();

return outputData;
}
}

public void WarmUpSockets(IEnumerable<ushort> universeIds)
{
foreach (ushort universeId in universeIds)
Expand Down Expand Up @@ -684,11 +608,6 @@ public void Dispose()

this.listenSocket.Close();
this.listenSocket.Dispose();

foreach (var kvp in this.outputDataPerDestination)
{
kvp.Value.Dispose();
}
}
}
}

0 comments on commit 5b6c76c

Please sign in to comment.