Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-dmxc committed Jan 5, 2024
1 parent 7ae706b commit 66452c1
Show file tree
Hide file tree
Showing 39 changed files with 256 additions and 208 deletions.
12 changes: 4 additions & 8 deletions ArtNetSharp/ApplicationLogging.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using static ArtNetSharp.ApplicationLogging;
using System.Net.Sockets;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using static ArtNetSharp.ApplicationLogging;

namespace ArtNetSharp
{
Expand Down
14 changes: 7 additions & 7 deletions ArtNetSharp/ArtNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static ArtNet Instance
private List<AbstractInstance> instances = new List<AbstractInstance>();
public ReadOnlyCollection<AbstractInstance> Instances { get => instances.AsReadOnly(); }

private List<NetworkClientBag> networkClients= new List<NetworkClientBag>();
private List<NetworkClientBag> networkClients = new List<NetworkClientBag>();
public IReadOnlyCollection<NetworkClientBag> NetworkClients => networkClients.AsReadOnly();

private System.Timers.Timer _updateNetworkClientsTimer = null;
Expand Down Expand Up @@ -64,7 +64,7 @@ public bool Enabled
}
}

public event EventHandler<Tuple<IPv4Address,UdpReceiveResult>> ReceivedData;
public event EventHandler<Tuple<IPv4Address, UdpReceiveResult>> ReceivedData;

internal NetworkClientBag(IPAddress broadcastIpAddress)
{
Expand Down Expand Up @@ -126,7 +126,7 @@ private async Task StartListening()
ReceivedData?.Invoke(this, new Tuple<IPv4Address, UdpReceiveResult>(LocalIpAddress, received));
}
}
catch(Exception e) { Logger.LogError(e); _ = openClient(); }
catch (Exception e) { Logger.LogError(e); _ = openClient(); }
}

private List<IPAddress> notMatchingIpAdddresses = new List<IPAddress>();
Expand All @@ -144,7 +144,7 @@ public async Task<bool> MatchIP(IPAddress ip)
return true;

var _ip = await GetLocalIP(ip);
if (IPAddress.Equals(LocalIpAddress , _ip))
if (IPAddress.Equals(LocalIpAddress, _ip))
{
matchingIpAdddresses.Add(ip);
return true;
Expand Down Expand Up @@ -252,7 +252,7 @@ private ArtNet()

public static void SetLoggerFectory(ILoggerFactory loggerFactory)
{
Tools.LoggerFactory= loggerFactory;
Tools.LoggerFactory = loggerFactory;
ApplicationLogging.LoggerFactory = loggerFactory;
}

Expand Down Expand Up @@ -337,7 +337,7 @@ public static bool IsNetworkAvailable(long minimumSpeed)
return true;
}
}
catch(Exception e)
catch (Exception e)
{
Logger.LogError(e);
}
Expand Down Expand Up @@ -415,7 +415,7 @@ internal async Task TrySendPacket(AbstractArtPacketCore packet, IPv4Address dest
internal async Task TrySendBroadcastPacket(AbstractArtPacketCore packet)
{
List<Task> tasks = new List<Task>();
foreach(var ncb in networkClients)
foreach (var ncb in networkClients)
tasks.Add(Task.Run(async () => await ncb.TrySendBroadcastPacket(packet)));
await Task.WhenAll(tasks);
}
Expand Down
107 changes: 55 additions & 52 deletions ArtNetSharp/Communication/AbstractInstance.cs

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions ArtNetSharp/Communication/ControllerInstance.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using RDMSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -15,7 +14,7 @@ public class ControllerInstance : AbstractInstance

protected override async void OnPacketReceived(AbstractArtPacketCore packet, IPv4Address localIp, IPv4Address sourceIp)
{
switch(packet)
switch (packet)
{

case ArtDataReply artDataReply:
Expand All @@ -35,7 +34,7 @@ private async Task processArtDataReply(ArtDataReply artDataReply, IPv4Address so

public async Task PerformRDMDiscovery(PortAddress? portAddress = null, bool flush = false)
{
await base.PerformRDMDiscovery(portAddress,flush);
await base.PerformRDMDiscovery(portAddress, flush);
}
}
}
6 changes: 3 additions & 3 deletions ArtNetSharp/Communication/IInstance.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using RDMSharp;
using System;
using ArtNetSharp.Messages.Interfaces;
using RDMSharp;

namespace ArtNetSharp.Communication
{
internal interface IInstance: IDisposable
internal interface IInstance : IDisposableExtended
{
string Name { get; set; }
string ShortName { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion ArtNetSharp/Communication/NodeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected async Task processArtData(ArtData artData, IPv4Address source)
packet = buildArtDataReply(artData);

if (packet != null)
await TrySendPacket(packet,source);
await TrySendPacket(packet, source);
}
catch (Exception ex) { Logger.LogError(ex); }
}
Expand Down
2 changes: 1 addition & 1 deletion ArtNetSharp/Communication/PortConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public PortConfig(PortAddress portAddress, bool output, bool input)
KnownRDMUIDs = knownRDMUIDs.Values.ToList().AsReadOnly();
}

public void AddAdditionalIPEndpoints(params IPv4Address[] addresses)
public void AddAdditionalIPEndpoints(params IPv4Address[] addresses)
{
foreach (IPv4Address address in addresses)
if (!additionalIPEndpoints.Contains(address))
Expand Down
15 changes: 10 additions & 5 deletions ArtNetSharp/Communication/RemoteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Mail;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

Expand Down Expand Up @@ -196,7 +195,7 @@ public ArtPollReply Root
this.IsSACNCapable = root.Status.HasFlag(ENodeStatus.NodeSupportArtNet_sACN_Switching);
}
}
private ConcurrentDictionary<int, RemoteClientPort> ports= new ConcurrentDictionary<int, RemoteClientPort>();
private ConcurrentDictionary<int, RemoteClientPort> ports = new ConcurrentDictionary<int, RemoteClientPort>();
public IReadOnlyCollection<RemoteClientPort> Ports { get; private set; }
public event EventHandler<RemoteClientPort> PortDiscovered;
public event EventHandler<RemoteClientPort> PortTimedOut;
Expand All @@ -217,7 +216,7 @@ private void onPropertyChanged(PropertyChangedEventArgs eventArgs)
{
PropertyChanged?.Invoke(this, eventArgs);
}
catch(Exception e)
catch (Exception e)
{
Logger.LogError(e);
}
Expand Down Expand Up @@ -332,7 +331,10 @@ public async Task processArtDataReply(ArtDataReply artDataReply)
private async Task PollArtData()
{
if (Instance is ControllerInstance)
await ArtNet.Instance.TrySendPacket(new ArtData(instance.OEMProductCode, instance.ESTAManufacturerCode), IpAddress);
{
using ArtData artData = new ArtData(instance.OEMProductCode, instance.ESTAManufacturerCode);
await ArtNet.Instance.TrySendPacket(artData, IpAddress);
}
}
private async Task QueryArtData()
{
Expand All @@ -341,7 +343,10 @@ private async Task QueryArtData()

EDataRequest[] todo = new[] { EDataRequest.UrlProduct, EDataRequest.UrlSupport, EDataRequest.UrlUserGuide, EDataRequest.UrlPersUdr, EDataRequest.UrlPersGdtf };
foreach (EDataRequest req in todo)
await ArtNet.Instance.TrySendPacket(new ArtData(instance.OEMProductCode, instance.ESTAManufacturerCode, req), IpAddress);
{
using ArtData artData = new ArtData(instance.OEMProductCode, instance.ESTAManufacturerCode, req);
await ArtNet.Instance.TrySendPacket(artData, IpAddress);
}
}

private void Port_RDMUIDReceived(object sender, RDMUID_ReceivedBag bag)
Expand Down
4 changes: 2 additions & 2 deletions ArtNetSharp/Communication/RemoteClientPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private set

private void onPropertyChanged([CallerMemberName] string membername = "")
{
onPropertyChanged( new PropertyChangedEventArgs(membername));
onPropertyChanged(new PropertyChangedEventArgs(membername));
}
private void onPropertyChanged(PropertyChangedEventArgs eventArgs)
{
Expand Down Expand Up @@ -226,7 +226,7 @@ internal void AddRdmUIDs(params RDMUID[] rdmuids)
bag.Seen();
else
{
bag= new RDMUID_ReceivedBag(rdmuid);
bag = new RDMUID_ReceivedBag(rdmuid);
if (knownRDMUIDs.TryAdd(rdmuid, bag))
{
RDMUIDReceived?.Invoke(this, bag);
Expand Down
47 changes: 44 additions & 3 deletions ArtNetSharp/Messages/Abstract/AbstractArtPacketCore.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
using System;
using ArtNetSharp.Messages.Interfaces;
using System;

namespace ArtNetSharp
{
public abstract class AbstractArtPacketCore
public abstract class AbstractArtPacketCore : IDisposableExtended
{
public abstract EOpCodes OpCode { get; }
protected abstract ushort PacketMinLength { get; }
protected virtual ushort PacketMaxLength { get { return PacketMinLength; } }
protected virtual ushort PacketBuildLength { get { return PacketMaxLength; } }

protected bool PacketLengthIsMinimum { get { return PacketMinLength != PacketMaxLength; } }

protected bool IsDisposing { get; private set; }
bool IDisposableExtended.IsDisposing { get => IsDisposing; }
protected bool IsDisposed { get; private set; }
bool IDisposableExtended.IsDisposed { get => IsDisposed; }

protected int? HashCode { get; private set; }

protected AbstractArtPacketCore()
{
}
Expand All @@ -27,9 +36,16 @@ public AbstractArtPacketCore(in byte[] packet) : this()
else if (packet.Length != PacketMinLength)
throw new ArgumentOutOfRangeException($"This Packet({packet.Length}) should be {PacketMinLength} bytes");
}
~AbstractArtPacketCore()
{
((IDisposable)this).Dispose();
}

public byte[] GetPacket()
{
if (this.IsDisposing || this.IsDisposed)
throw new ObjectDisposedException(this.GetType().FullName);

byte[] p = new byte[(int)PacketBuildLength];

Tools.FillDefaultPacket(OpCode, ref p);
Expand All @@ -52,7 +68,32 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
return 483245663 + OpCode.GetHashCode();
if (HashCode.HasValue)
return HashCode.Value;

HashCode = 483245663 + OpCode.GetHashCode();
return HashCode.Value;
}

void IDisposable.Dispose()
{
if (this.IsDisposing || this.IsDisposed)
return;

IsDisposing = true;
try
{
this.Dispose();
}
catch { }
IsDisposed = true;
IsDisposing = false;
GC.SuppressFinalize(this);
}

protected virtual void Dispose()
{

}
}
}
4 changes: 2 additions & 2 deletions ArtNetSharp/Messages/Abstract/AbstractArtPacketNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public abstract class AbstractArtPacketNet : AbstractArtPacket
/// The top 7 bits of the 15 bit Port-Address to which this packet is destined.
/// </summary>
public readonly Net Net;

public AbstractArtPacketNet(in Net net,
in ushort protocolVersion = Constants.PROTOCOL_VERSION) : base(protocolVersion)
{
Expand All @@ -29,7 +29,7 @@ public static implicit operator byte[](AbstractArtPacketNet abstractArtPacketNet

public override bool Equals(object obj)
{
return base.Equals(obj) &&
return base.Equals(obj) &&
obj is AbstractArtPacketNet other &&
Net == other.Net;
}
Expand Down
4 changes: 2 additions & 2 deletions ArtNetSharp/Messages/ArtAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class ArtAddress : AbstractArtPacket
protected override sealed ushort PacketMinLength => 107;

public readonly Net? Net;
public readonly Subnet? Subnet;
public readonly Subnet? Subnet;
public readonly ArtAddressCommand Command;
public readonly byte BindIndex;
public readonly string ShortName;
Expand Down Expand Up @@ -83,7 +83,7 @@ public ArtAddress(in byte[] packet) : base(packet)
for (int i = 0; i < 4; i++)
if ((packet[100 + i] & 0x80) == 0x80)
swOut.Add((Universe?)(byte)(packet[100 + i] & 0x0f));
OutputUniverses= swOut.ToArray();
OutputUniverses = swOut.ToArray();

if ((packet[104] & 0x80) == 0x80)
Subnet = (byte)(packet[104] & 0x0f);
Expand Down
2 changes: 1 addition & 1 deletion ArtNetSharp/Messages/ArtDMX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ArtNetSharp
{
public sealed class ArtDMX: AbstractArtPacketNetAddress
public sealed class ArtDMX : AbstractArtPacketNetAddress
{
/// <summary>
/// The sequence number is used to ensure that ArtDmx packets are used in the correct order.
Expand Down
10 changes: 5 additions & 5 deletions ArtNetSharp/Messages/ArtDataReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public sealed class ArtDataReply : AbstractArtPacket
{
public override sealed EOpCodes OpCode => EOpCodes.OpDataReply;
protected override sealed ushort PacketMinLength => 42;
protected override sealed ushort PacketMaxLength => (ushort)(PacketMinLength+512);
protected override sealed ushort PacketMaxLength => (ushort)(PacketMinLength + 512);
protected override sealed ushort PacketBuildLength => (ushort)(PacketMinLength + (Data?.Length ?? 0));

public readonly EDataRequest Request;
Expand All @@ -33,7 +33,7 @@ public ArtDataReply(in ushort oemCode = Constants.DEFAULT_OEM_CODE,
in ushort manufacturerCode = Constants.DEFAULT_ESTA_MANUFACTURER_CODE,
in EDataRequest request = EDataRequest.Poll,
in string payload = null,
in ushort protocolVersion = Constants.PROTOCOL_VERSION) : this(oemCode,manufacturerCode,request, !string.IsNullOrWhiteSpace(payload)? Encoding.ASCII.GetBytes(payload):null,protocolVersion)
in ushort protocolVersion = Constants.PROTOCOL_VERSION) : this(oemCode, manufacturerCode, request, !string.IsNullOrWhiteSpace(payload) ? Encoding.ASCII.GetBytes(payload) : null, protocolVersion)
{
PayloadObject = payload;
}
Expand All @@ -53,13 +53,13 @@ public ArtDataReply(in byte[] packet) : base(packet)
{
if (packet.Length >= 13)

ManufacturerCode = (ushort)(packet[12] << 8 | packet[13]);
ManufacturerCode = (ushort)(packet[12] << 8 | packet[13]);
OemCode = (ushort)(packet[14] << 8 | packet[15]);
Request = (EDataRequest)(ushort)(packet[16] << 8 | packet[17]);
ushort payloadLength= (ushort)(packet[18] << 8 | packet[19]);
ushort payloadLength = (ushort)(packet[18] << 8 | packet[19]);
Data = new byte[payloadLength];
Array.Copy(packet, 20, Data, 0, Data.Length);
if((ushort)Request <=8) // Data is String/URL
if ((ushort)Request <= 8) // Data is String/URL
PayloadObject = Encoding.ASCII.GetString(Data, 0, Data.Length).TrimEnd('\0');
}

Expand Down
2 changes: 1 addition & 1 deletion ArtNetSharp/Messages/ArtNzs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ArtNetSharp
{
public sealed class ArtNzs: AbstractArtPacketNetAddress
public sealed class ArtNzs : AbstractArtPacketNetAddress
{
/// <summary>
/// The sequence number is used to ensure that ArtDmx packets are used in the correct order.
Expand Down
2 changes: 1 addition & 1 deletion ArtNetSharp/Messages/ArtPoll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ArtPoll(in ushort oemCode = Constants.DEFAULT_OEM_CODE,
Flags = flags;
Priority = priority;
}
public ArtPoll(in byte[] packet): base(packet)
public ArtPoll(in byte[] packet) : base(packet)
{
if (packet.Length >= 13)
Flags = (EArtPollFlags)packet[12];
Expand Down
Loading

0 comments on commit 66452c1

Please sign in to comment.