diff --git a/Unity/SiteServer.cs b/Unity/SiteServer.cs index 4dde64b..3a686fe 100644 --- a/Unity/SiteServer.cs +++ b/Unity/SiteServer.cs @@ -24,7 +24,7 @@ namespace RoboidControl.Unity { public void HandleNewThing(RoboidControl.Thing thing) { //Debug.Log($"Handle New thing event for {thing}"); - site.Add(thing, false); + //site.Add(thing, false); thingQueue.Enqueue(thing); } diff --git a/src/Participant.cs b/src/Participant.cs index 460925d..4fc8489 100644 --- a/src/Participant.cs +++ b/src/Participant.cs @@ -108,13 +108,13 @@ namespace RoboidControl { public void Add(Thing thing, bool checkId = true, bool invokeEvent = true) { if (checkId && thing.id == 0) { thing.id = (byte)(this.things.Count + 1); - things.Add(thing); + this.things.Add(thing); } // Console.WriteLine($"added thing [{thing.networkId}/{thing.id}]"); Thing foundThing = Get(thing.networkId, thing.id); if (foundThing == null) { - things.Add(thing); + this.things.Add(thing); // if (invokeEvent) // Thing.InvokeNewThing(thing); diff --git a/src/Participants/ParticipantUDP.cs b/src/Participants/ParticipantUDP.cs index b2d763a..5df9e19 100644 --- a/src/Participants/ParticipantUDP.cs +++ b/src/Participants/ParticipantUDP.cs @@ -33,6 +33,7 @@ namespace RoboidControl { public ParticipantUDP(int port = 0) : base("127.0.0.1", port) { if (this.port == 0) this.isIsolated = true; + Participant.AddParticipant(this); } /// @@ -45,6 +46,7 @@ namespace RoboidControl { this.isIsolated = true; else this.remoteSite = new Participant(ipAddress, port); + Participant.AddParticipant(this); this.endPoint = new IPEndPoint(IPAddress.Any, localPort); this.udpClient = new UdpClient(localPort); @@ -159,7 +161,9 @@ namespace RoboidControl { if (this.isIsolated) 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) continue; @@ -301,10 +305,6 @@ namespace RoboidControl { } } - #endregion - - #region Process - protected virtual void Process(Participant sender, ParticipantMsg msg) { #if DEBUG Console.WriteLine($"{this.name} Process participantMsg {msg.networkId}"); @@ -377,9 +377,9 @@ namespace RoboidControl { } protected virtual void Process(Participant sender, BinaryMsg msg) { - // #if DEBUG - // Console.WriteLine($"Participant: Process BinaryMsg [{msg.networkId}/{msg.thingId}] {msg.dataLength}"); - // #endif +#if DEBUG + Console.WriteLine($"Participant: Process BinaryMsg [{msg.networkId}/{msg.thingId}] {msg.dataLength}"); +#endif Thing thing = sender.Get(msg.networkId, msg.thingId); thing?.ProcessBinary(msg.data); } diff --git a/src/Participants/SiteServer.cs b/src/Participants/SiteServer.cs index d86b21c..9e36f7c 100644 --- a/src/Participants/SiteServer.cs +++ b/src/Participants/SiteServer.cs @@ -15,6 +15,7 @@ namespace RoboidControl { /// public SiteServer(int port = 7681) : base(port) { this.name = "Site Server"; + Participant.AddParticipant(this); Console.Write($"Prepare receive on port {port}"); this.endPoint = new IPEndPoint(IPAddress.Any, port); @@ -33,7 +34,11 @@ namespace RoboidControl { #region Update 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) continue; @@ -75,7 +80,7 @@ namespace RoboidControl { thing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); if (msg.parentId != 0) { - thing.parent = Get(msg.networkId, msg.parentId); + thing.parent = sender.Get(msg.networkId, msg.parentId); if (thing.parent == null) Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]"); } diff --git a/src/Thing.cs b/src/Thing.cs index a00774a..7e4ae16 100644 --- a/src/Thing.cs +++ b/src/Thing.cs @@ -90,6 +90,7 @@ namespace RoboidControl { this.id = thingId; this.type = thingType; this.networkId = networkId; + Console.Write($"New thing added to {owner}"); this.owner.Add(this); InvokeNewThing(this); }