Skip to content

Commit

Permalink
Grrrg
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-dmxc committed Jun 4, 2024
1 parent c0878eb commit 8600cb9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
11 changes: 5 additions & 6 deletions ArtNetSharp/Communication/AbstractInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -819,28 +819,27 @@ 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");
}
}
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));
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions ArtNetSharp/Communication/RemoteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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();
Expand Down
14 changes: 10 additions & 4 deletions ArtNetSharp/Communication/RemoteClientPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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;
Expand All @@ -182,7 +187,7 @@ public void processArtPollReply(ArtPollReply artPollReply)
return;

ArtPollReply = artPollReply;
LastSeen = DateTime.UtcNow;
seen();

PortType = artPollReply.PortTypes[PortIndex];
GoodOutput = artPollReply.GoodOutput[PortIndex];
Expand Down Expand Up @@ -210,6 +215,7 @@ public void processArtPollReply(ArtPollReply artPollReply)
}
else
InputPortAddress = null;
seen();
}
private void addControllerRdmUID(UID rdmuid)
{
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 8600cb9

Please sign in to comment.