From 8600cb92499a69996d7190913fd496147af0bbbd Mon Sep 17 00:00:00 2001 From: Patrick Grote Date: Tue, 4 Jun 2024 14:14:59 +0200 Subject: [PATCH] Grrrg --- ArtNetSharp/Communication/AbstractInstance.cs | 11 +++++------ ArtNetSharp/Communication/RemoteClient.cs | 19 ++++++++++++++----- ArtNetSharp/Communication/RemoteClientPort.cs | 14 ++++++++++---- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/ArtNetSharp/Communication/AbstractInstance.cs b/ArtNetSharp/Communication/AbstractInstance.cs index 097bcaa..ae6922f 100644 --- a/ArtNetSharp/Communication/AbstractInstance.cs +++ b/ArtNetSharp/Communication/AbstractInstance.cs @@ -819,7 +819,7 @@ void add(RemoteClient rc) if (remoteClients.TryAdd(rc.ID, rc)) { Logger.LogInformation($"Discovered: {rc.ID}"); - RemoteClientDiscovered?.InvokeFailSafe(this, rc); + Task.Run(() => RemoteClientDiscovered?.InvokeFailSafe(this, rc)); return; } Logger.LogWarning($"Cant add {rc.ID} to Dictionary"); @@ -827,20 +827,19 @@ void add(RemoteClient rc) } catch (Exception ex) { Logger.LogError(ex); } - var deadline = 9500; // Spec 1.4dd page 12, doubled to allow one lost reply (6s is allowad, for some delay i add 2500 ms) - var timoutedClients = remoteClients.Where(p => (DateTime.UtcNow - p.Value.LastSeen).TotalMilliseconds > deadline); + var timoutedClients = RemoteClients.Where(p => p.Timouted()); if (timoutedClients.Count() != 0) { timoutedClients = timoutedClients.ToList(); foreach (var rc in timoutedClients) { - if (remoteClients.TryRemove(rc.Key, out RemoteClient removed)) + if (remoteClients.TryRemove(rc.ID, out RemoteClient removed)) remoteClientsTimeouted.TryAdd(removed.ID, removed); if (removed != null) { - Logger.LogInformation($"Timeout: {removed.ID} ({(DateTime.UtcNow - rc.Value.LastSeen).TotalMilliseconds}ms)"); - RemoteClientTimedOut?.InvokeFailSafe(this, removed); + Logger.LogInformation($"Timeout: {removed.ID} ({(DateTime.UtcNow - rc.LastSeen).TotalMilliseconds}ms)"); + Task.Run(() => RemoteClientTimedOut?.InvokeFailSafe(this, removed)); } } } diff --git a/ArtNetSharp/Communication/RemoteClient.cs b/ArtNetSharp/Communication/RemoteClient.cs index 803d2ae..c714b35 100644 --- a/ArtNetSharp/Communication/RemoteClient.cs +++ b/ArtNetSharp/Communication/RemoteClient.cs @@ -80,6 +80,12 @@ private set onPropertyChanged(); } } + internal bool Timouted() // Spec 1.4dd page 12, doubled to allow one lost reply (6s is allowad, for some delay i add 2500 ms) + { + var now = DateTime.UtcNow.AddSeconds(-9.5); + return LastSeen <= now; + } + private bool isRDMCapable; public bool IsRDMCapable { @@ -252,11 +258,13 @@ internal AbstractInstance Instance public RemoteClient(in ArtPollReply artPollReply) { + seen(); ID = getIDOf(artPollReply); MacAddress = artPollReply.MAC; IpAddress = artPollReply.OwnIp; seen(); processArtPollReply(artPollReply); + seen(); } private void seen() { @@ -292,7 +300,7 @@ public void processArtPollReply(ArtPollReply artPollReply) port = new RemoteClientPort(artPollReply, portIndex); if (ports.TryAdd(physicalPort, port)) { - PortDiscovered?.InvokeFailSafe(this, port); + Task.Run(() => PortDiscovered?.InvokeFailSafe(this, port)); port.RDMUIDReceived += Port_RDMUIDReceived; } } @@ -302,23 +310,24 @@ public void processArtPollReply(ArtPollReply artPollReply) { Logger.LogError(ex); } + seen(); - var deadline = 7500; // Spec 1.4dd page 12, doubled to allow one lost reply (6s is allowad, for some delay i add 1500 ms) - var timoutedPorts = ports.Where(p => (DateTime.UtcNow - p.Value.LastSeen).TotalMilliseconds > deadline); + var timoutedPorts = ports.Where(p => p.Value.Timouted()); if (timoutedPorts.Count() != 0) { timoutedPorts = timoutedPorts.ToList(); foreach (var port in timoutedPorts) { ports.TryRemove(port.Key, out _); - PortTimedOut?.InvokeFailSafe(this, port.Value); + Task.Run(()=>PortTimedOut?.InvokeFailSafe(this, port)); } } + seen(); Ports = ports.Select(p => p.Value).ToList().AsReadOnly(); } public async Task processArtDataReply(ArtDataReply artDataReply) { - LastSeen = DateTime.UtcNow; + seen(); if (artDataReply.Request == EDataRequest.Poll) { await QueryArtData(); diff --git a/ArtNetSharp/Communication/RemoteClientPort.cs b/ArtNetSharp/Communication/RemoteClientPort.cs index 793a844..973d030 100644 --- a/ArtNetSharp/Communication/RemoteClientPort.cs +++ b/ArtNetSharp/Communication/RemoteClientPort.cs @@ -33,9 +33,13 @@ private set onPropertyChanged(); } } - internal bool Timouted() + private void seen() { - var now = DateTime.UtcNow.AddSeconds(-30); + LastSeen = DateTime.UtcNow; + } + internal bool Timouted()// Spec 1.4dd page 12, doubled to allow one lost reply (6s is allowad, for some delay i add 1500 ms) + { + var now = DateTime.UtcNow.AddSeconds(-7.5); return LastSeen <= now; } public ArtPollReply ArtPollReply { get; private set; } @@ -157,6 +161,7 @@ private void onPropertyChanged(PropertyChangedEventArgs eventArgs) public RemoteClientPort(in ArtPollReply artPollReply, byte portIndex = 0) { + seen(); ID = getIDOf(artPollReply, portIndex); IpAddress = artPollReply.OwnIp; BindIndex = artPollReply.BindIndex; @@ -182,7 +187,7 @@ public void processArtPollReply(ArtPollReply artPollReply) return; ArtPollReply = artPollReply; - LastSeen = DateTime.UtcNow; + seen(); PortType = artPollReply.PortTypes[PortIndex]; GoodOutput = artPollReply.GoodOutput[PortIndex]; @@ -210,6 +215,7 @@ public void processArtPollReply(ArtPollReply artPollReply) } else InputPortAddress = null; + seen(); } private void addControllerRdmUID(UID rdmuid) { @@ -270,7 +276,7 @@ internal void ProcessArtRDM(ArtRDM artRDM) if (!KnownResponderRDMUIDs.Any(k => k.Uid.Equals(artRDM.Source))) return; - LastSeen = DateTime.UtcNow; + seen(); } public override string ToString()