Skip to content

Commit

Permalink
Grrrrrrr
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-dmxc committed Apr 11, 2024
1 parent d172376 commit b6222a0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions ArtNetSharp/ArtNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using RDMSharp;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
Expand Down Expand Up @@ -30,8 +31,8 @@ public static ArtNet Instance

private Dictionary<IPv4Address, MACAddress> ipTomacAddressCache = new Dictionary<IPv4Address, MACAddress>();

private List<AbstractInstance> instances = new List<AbstractInstance>();
public ReadOnlyCollection<AbstractInstance> Instances { get => instances.AsReadOnly(); }
private ConcurrentDictionary<uint, AbstractInstance> instances = new ConcurrentDictionary<uint, AbstractInstance>();
public ReadOnlyCollection<AbstractInstance> Instances { get => instances.Values.ToList().AsReadOnly(); }

private List<NetworkClientBag> networkClients = new List<NetworkClientBag>();
public IReadOnlyCollection<NetworkClientBag> NetworkClients => networkClients.AsReadOnly();
Expand Down Expand Up @@ -332,7 +333,7 @@ private void processPacket(AbstractArtPacketCore packet, IPv4Address localIp, IP

Logger.LogTrace($"Received Packet from {sourceIp} -> {packet}");
//#endif
instances.ForEach(_instance => { ((IInstance)_instance).PacketReceived(packet, localIp, sourceIp); });
instances.Values.ToList().ForEach(_instance => { ((IInstance)_instance).PacketReceived(packet, localIp, sourceIp); });
}

public static bool IsNetworkAvailable(long? minimumSpeed=null)
Expand Down Expand Up @@ -434,12 +435,17 @@ private void updateNetworkClients()

public void AddInstance(AbstractInstance instance)
{
this.instances.Add(instance);
if (this.instances.Any(i => i.Value == instance))
return;
this.instances.TryAdd((uint)new Random().Next(), instance);
Logger?.LogDebug($"Added instance {instance.GetType().Name}: {instance.Name} ({instance.ShortName})");
}
public void RemoveInstance(AbstractInstance instance)
{
this.instances.Remove(instance);
var toRemove = this.instances.FirstOrDefault(i => i.Value == instance);
if (toRemove.Value == null)
return;
this.instances.TryRemove(toRemove.Key, out _);
Logger?.LogDebug($"Removed instance {instance.ShortName}");
}

Expand Down

0 comments on commit b6222a0

Please sign in to comment.