Binary msg receiving fix

This commit is contained in:
Pascal Serrarens 2025-04-23 17:51:10 +02:00
parent 1225ee1097
commit fd8215cc7e
5 changed files with 19 additions and 13 deletions

View File

@ -24,7 +24,7 @@ namespace RoboidControl.Unity {
public void HandleNewThing(RoboidControl.Thing thing) { public void HandleNewThing(RoboidControl.Thing thing) {
//Debug.Log($"Handle New thing event for {thing}"); //Debug.Log($"Handle New thing event for {thing}");
site.Add(thing, false); //site.Add(thing, false);
thingQueue.Enqueue(thing); thingQueue.Enqueue(thing);
} }

View File

@ -108,13 +108,13 @@ namespace RoboidControl {
public void Add(Thing thing, bool checkId = true, bool invokeEvent = true) { public void Add(Thing thing, bool checkId = true, bool invokeEvent = true) {
if (checkId && thing.id == 0) { if (checkId && thing.id == 0) {
thing.id = (byte)(this.things.Count + 1); thing.id = (byte)(this.things.Count + 1);
things.Add(thing); this.things.Add(thing);
} }
// Console.WriteLine($"added thing [{thing.networkId}/{thing.id}]"); // Console.WriteLine($"added thing [{thing.networkId}/{thing.id}]");
Thing foundThing = Get(thing.networkId, thing.id); Thing foundThing = Get(thing.networkId, thing.id);
if (foundThing == null) { if (foundThing == null) {
things.Add(thing); this.things.Add(thing);
// if (invokeEvent) // if (invokeEvent)
// Thing.InvokeNewThing(thing); // Thing.InvokeNewThing(thing);

View File

@ -33,6 +33,7 @@ namespace RoboidControl {
public ParticipantUDP(int port = 0) : base("127.0.0.1", port) { public ParticipantUDP(int port = 0) : base("127.0.0.1", port) {
if (this.port == 0) if (this.port == 0)
this.isIsolated = true; this.isIsolated = true;
Participant.AddParticipant(this);
} }
/// <summary> /// <summary>
@ -45,6 +46,7 @@ namespace RoboidControl {
this.isIsolated = true; this.isIsolated = true;
else else
this.remoteSite = new Participant(ipAddress, port); this.remoteSite = new Participant(ipAddress, port);
Participant.AddParticipant(this);
this.endPoint = new IPEndPoint(IPAddress.Any, localPort); this.endPoint = new IPEndPoint(IPAddress.Any, localPort);
this.udpClient = new UdpClient(localPort); this.udpClient = new UdpClient(localPort);
@ -159,7 +161,9 @@ namespace RoboidControl {
if (this.isIsolated) if (this.isIsolated)
continue; continue;
foreach (Thing thing in participant.things) { //foreach (Thing thing in participant.things) {
for (int thingIx = 0; thingIx < participant.things.Count; thingIx++) {
Thing thing = participant.things[thingIx];
if (thing == null) if (thing == null)
continue; continue;
@ -301,10 +305,6 @@ namespace RoboidControl {
} }
} }
#endregion
#region Process
protected virtual void Process(Participant sender, ParticipantMsg msg) { protected virtual void Process(Participant sender, ParticipantMsg msg) {
#if DEBUG #if DEBUG
Console.WriteLine($"{this.name} Process participantMsg {msg.networkId}"); Console.WriteLine($"{this.name} Process participantMsg {msg.networkId}");
@ -377,9 +377,9 @@ namespace RoboidControl {
} }
protected virtual void Process(Participant sender, BinaryMsg msg) { protected virtual void Process(Participant sender, BinaryMsg msg) {
// #if DEBUG #if DEBUG
// Console.WriteLine($"Participant: Process BinaryMsg [{msg.networkId}/{msg.thingId}] {msg.dataLength}"); Console.WriteLine($"Participant: Process BinaryMsg [{msg.networkId}/{msg.thingId}] {msg.dataLength}");
// #endif #endif
Thing thing = sender.Get(msg.networkId, msg.thingId); Thing thing = sender.Get(msg.networkId, msg.thingId);
thing?.ProcessBinary(msg.data); thing?.ProcessBinary(msg.data);
} }

View File

@ -15,6 +15,7 @@ namespace RoboidControl {
/// <param name="port"></param> /// <param name="port"></param>
public SiteServer(int port = 7681) : base(port) { public SiteServer(int port = 7681) : base(port) {
this.name = "Site Server"; this.name = "Site Server";
Participant.AddParticipant(this);
Console.Write($"Prepare receive on port {port}"); Console.Write($"Prepare receive on port {port}");
this.endPoint = new IPEndPoint(IPAddress.Any, port); this.endPoint = new IPEndPoint(IPAddress.Any, port);
@ -33,7 +34,11 @@ namespace RoboidControl {
#region Update #region Update
protected override void UpdateMyThings(ulong currentTimeMS) { protected override void UpdateMyThings(ulong currentTimeMS) {
foreach (Thing thing in this.things) { // We don't use foreach to prevent the 'Collection was modified' error
int n = this.things.Count;
for (int ix = 0; ix < n; ix++) {
Thing thing = this.things[ix];
if (thing == null) if (thing == null)
continue; continue;
@ -75,7 +80,7 @@ namespace RoboidControl {
thing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); thing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);
if (msg.parentId != 0) { if (msg.parentId != 0) {
thing.parent = Get(msg.networkId, msg.parentId); thing.parent = sender.Get(msg.networkId, msg.parentId);
if (thing.parent == null) if (thing.parent == null)
Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]"); Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]");
} }

View File

@ -90,6 +90,7 @@ namespace RoboidControl {
this.id = thingId; this.id = thingId;
this.type = thingType; this.type = thingType;
this.networkId = networkId; this.networkId = networkId;
Console.Write($"New thing added to {owner}");
this.owner.Add(this); this.owner.Add(this);
InvokeNewThing(this); InvokeNewThing(this);
} }