diff --git a/Messages/CustomMsg.cs b/Messages/CustomMsg.cs index 5b491b1..75e4311 100644 --- a/Messages/CustomMsg.cs +++ b/Messages/CustomMsg.cs @@ -20,8 +20,14 @@ namespace Passer.Control.Core { this.thingId = thingId; this.bytes = bytes; } + public CustomMsg(byte networkId, Thing thing) : base() { + this.networkId = networkId; + this.thingId = thing.id; + } public override byte Serialize(ref byte[] buffer) { + if (bytes == null) + return 0; byte ix = 0; buffer[ix++] = CustomMsg.Id; buffer[ix++] = this.networkId; diff --git a/Participant.cs b/Participant.cs index c9cbaad..04d2900 100644 --- a/Participant.cs +++ b/Participant.cs @@ -109,6 +109,8 @@ namespace Passer.Control.Core { byte[] data = udpClient.EndReceive(result, ref this.endPoint); // This does not yet take multi-packet messages into account! + // We can receive our own publish (broadcast) packages. How do we recognize them???? + // It is hard to determine our source port RemoteParticipant remoteParticipant = this.GetParticipant(endPoint.Address.ToString(), endPoint.Port); if (remoteParticipant == null) remoteParticipant = this.AddParticipant(endPoint.Address.ToString(), endPoint.Port); @@ -118,7 +120,7 @@ namespace Passer.Control.Core { udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); } - private ulong nextPublishMe = 0; + protected ulong nextPublishMe = 0; public virtual void Update(ulong currentTimeMS = 0) { if (currentTimeMS == 0) { @@ -128,7 +130,7 @@ namespace Passer.Control.Core { } if (this.publishInterval > 0 && currentTimeMS > this.nextPublishMe) { - this.Publish(new ClientMsg(this.networkId)); + Publish(); // Console.WriteLine($"{this.name} Publish ClientMsg {this.networkId}"); this.nextPublishMe = currentTimeMS + this.publishInterval; } @@ -137,7 +139,10 @@ namespace Passer.Control.Core { if (thing != null && thing.parent == null) // update only root things thing.Update(currentTimeMS); } + } + public virtual void Publish() { + this.Publish(new ClientMsg(this.networkId)); } #endregion Update @@ -149,6 +154,7 @@ namespace Passer.Control.Core { this.Send(remoteParticipant, new ThingMsg(this.networkId, thing)); this.Send(remoteParticipant, new NameMsg(this.networkId, thing)); this.Send(remoteParticipant, new ModelUrlMsg(this.networkId, thing)); + this.Send(remoteParticipant, new CustomMsg(this.networkId, thing)); } public bool Send(IMessage msg) { @@ -172,6 +178,14 @@ namespace Passer.Control.Core { return true; } + public void PublishThingInfo(Thing thing) { + //Console.WriteLine("Publish thing info"); + this.Publish(new ThingMsg(this.networkId, thing)); + this.Publish(new NameMsg(this.networkId, thing)); + this.Publish(new ModelUrlMsg(this.networkId, thing)); + this.Publish(new CustomMsg(this.networkId, thing)); + } + public bool Publish(IMessage msg) { int bufferSize = msg.Serialize(ref this.buffer); if (bufferSize <= 0) @@ -269,7 +283,7 @@ namespace Passer.Control.Core { } protected virtual void Process(RemoteParticipant sender, NameMsg msg) { - Console.WriteLine($"Participant: Process name [{msg.networkId}/{msg.thingId}] {msg.name}"); + // Console.WriteLine($"Participant: Process name [{msg.networkId}/{msg.thingId}] {msg.name}"); Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing != null) thing.name = msg.name; @@ -282,7 +296,7 @@ namespace Passer.Control.Core { protected virtual void Process(PoseMsg msg) { } protected virtual void Process(RemoteParticipant sender, CustomMsg msg) { - Console.WriteLine($"Participant: Process binary [{msg.networkId}/{msg.thingId}]"); + // Console.WriteLine($"Participant: Process binary [{msg.networkId}/{msg.thingId}]"); Thing thing = sender.Get(msg.networkId, msg.thingId); thing?.ProcessBinary(msg.bytes); } diff --git a/RemoteParticipant.cs b/RemoteParticipant.cs index 9e1e51e..989ec36 100644 --- a/RemoteParticipant.cs +++ b/RemoteParticipant.cs @@ -21,8 +21,8 @@ namespace Passer.Control.Core { public Thing Get(byte networkId, byte thingId) { Thing thing = things.Find(aThing => Thing.IsThing(aThing, networkId, thingId)); - if (thing == null) - Console.WriteLine($"Could not find thing {ipAddress}:{port}[{networkId}/{thingId}]"); + // if (thing == null) + // Console.WriteLine($"Could not find thing {ipAddress}:{port}[{networkId}/{thingId}]"); return thing; } @@ -37,7 +37,7 @@ namespace Passer.Control.Core { if (invokeEvent) Thing.InvokeNewThing(thing); - Console.Write($"Add thing [{thing.networkId}/{thing.id}] {thing.name}"); + Console.Write($"Add thing {ipAddress}:{port}[{networkId}/{thing.id}]"); } } diff --git a/SiteServer.cs b/SiteServer.cs index 1d29844..4172a8e 100644 --- a/SiteServer.cs +++ b/SiteServer.cs @@ -9,7 +9,6 @@ namespace Passer.Control.Core { public SiteServer(int port = 7681) { this.name = "Site Server"; - this.publishInterval = 0; this.ipAddress = "0.0.0.0"; this.port = port; @@ -23,6 +22,9 @@ namespace Passer.Control.Core { new Tuple(this.udpClient, new(IPAddress.Any, port))); } + public override void Publish() { + } + protected override void Process(RemoteParticipant sender, ClientMsg msg) { if (msg.networkId == 0) { Console.WriteLine($"{this.name} received New Client -> {sender.networkId}"); @@ -33,14 +35,14 @@ namespace Passer.Control.Core { protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { } protected override void Process(RemoteParticipant sender, ThingMsg msg) { - Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}]"); + //Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}]"); Thing thing = sender.Get(msg.networkId, msg.thingId); if (thing == null) { Thing newThing; if (thingMsgProcessors.ContainsKey(msg.thingType)) newThing = thingMsgProcessors[msg.thingType](msg.networkId, msg.thingId); else - newThing = new Thing(this, msg.networkId, msg.thingId, msg.thingType); + newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); sender.Add(newThing); } } diff --git a/Thing.cs b/Thing.cs index 53fc8bc..93bc59a 100644 --- a/Thing.cs +++ b/Thing.cs @@ -32,7 +32,7 @@ namespace Passer.Control.Core { #region Properties - public Participant participant; + public RemoteParticipant participant; public delegate void ChangeHandler(); public delegate void SphericalHandler(Spherical v); @@ -138,13 +138,15 @@ namespace Passer.Control.Core { //this.Init(); //Thing.Add(this); } + //OnNewThing?.Invoke(this); } - public Thing(Participant client, byte networkId, byte thingId, byte thingType = 0) { - this.participant = client; + public Thing(RemoteParticipant sender, byte networkId, byte thingId, byte thingType = 0) { + this.participant = sender; this.id = thingId; this.type = thingType; this.networkId = networkId; this.Init(); + //OnNewThing?.Invoke(this); //Thing.Add(this); }