From eca5e698cdaccdbe85e34435bbc5484411b88abe Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 24 Feb 2025 12:54:25 +0100 Subject: [PATCH] First UDP communication is working --- Participant.cs | 16 ++++++++-------- RemoteParticipant.cs | 1 - SiteServer.cs | 23 ++++++++++++++--------- Thing.cs | 7 ++++++- Unity/DebugConsole.cs | 2 +- Unity/SiteServer.cs | 5 +++-- Unity/Thing.cs | 21 +++++++++++++++++++-- 7 files changed, 51 insertions(+), 24 deletions(-) diff --git a/Participant.cs b/Participant.cs index bebc1f3..cacb27a 100644 --- a/Participant.cs +++ b/Participant.cs @@ -34,9 +34,9 @@ namespace Passer.RoboidControl { /// Create a participant with the give UDP port /// /// The port number on which to communicate - public Participant(int port) : this() { - this.port = port; - } + // public Participant(int port) : this() { + // this.port = port; + // } /// /// Create a new participant for a site at the given address and port @@ -85,11 +85,11 @@ namespace Passer.RoboidControl { return participant; } - protected readonly Dictionary> thingMsgProcessors = new Dictionary>(); + protected readonly Dictionary> thingMsgProcessors = new Dictionary>(); - public delegate Thing ThingConstructor(byte networkId, byte thingId); + public delegate Thing ThingConstructor(RemoteParticipant sender, byte networkId, byte thingId); public void Register(byte thingType, ThingConstructor constr) { - thingMsgProcessors[thingType] = new Func(constr); + thingMsgProcessors[thingType] = new Func(constr); } public void Register(Thing.Type thingType) where ThingClass : Thing { @@ -97,8 +97,8 @@ namespace Passer.RoboidControl { } public void Register(byte thingType) where ThingClass : Thing { - thingMsgProcessors[thingType] = (byte networkId, byte thingId) => - Activator.CreateInstance(typeof(ThingClass), networkId, thingId) as ThingClass; + thingMsgProcessors[thingType] = (RemoteParticipant sender, byte networkId, byte thingId) => + Activator.CreateInstance(typeof(ThingClass), sender, networkId, thingId) as ThingClass; Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); } diff --git a/RemoteParticipant.cs b/RemoteParticipant.cs index 44c15f4..3f2bc02 100644 --- a/RemoteParticipant.cs +++ b/RemoteParticipant.cs @@ -1,4 +1,3 @@ -#nullable enable using System; using System.Collections.Generic; diff --git a/SiteServer.cs b/SiteServer.cs index 868aacf..23679c9 100644 --- a/SiteServer.cs +++ b/SiteServer.cs @@ -9,23 +9,25 @@ namespace Passer.RoboidControl { /// public class SiteServer : Participant { + public SiteServer(int port = 7681) : this("0.0.0.0", port) {} + /// /// Create a new site server /// /// - public SiteServer(int port = 7681) { + public SiteServer(string ipAddress = "0.0.0.0", int port = 7681) : base() { this.name = "Site Server"; - this.ipAddress = "0.0.0.0"; + this.ipAddress = ipAddress; this.port = port; - //this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending + this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending Console.Write($"Prepare receive on port {port}"); this.udpClient = new UdpClient(port); // for receiving this.udpClient.BeginReceive( new AsyncCallback(result => ReceiveUDP(result)), - new Tuple(this.udpClient, new IPEndPoint(IPAddress.Any, port))); + new Tuple(this.udpClient, new(IPAddress.Any, port))); } /// @@ -48,17 +50,20 @@ namespace Passer.RoboidControl { 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 = null; - if (thingMsgProcessors.TryGetValue(msg.thingType, out Func value)) { + if (thingMsgProcessors.TryGetValue(msg.thingType, out Func value)) { + Console.WriteLine("Found thing message processor"); if (value != null) - newThing = value(msg.networkId, msg.thingId); + newThing = value(sender, msg.networkId, msg.thingId); } - if (newThing == null) + if (newThing == null) { newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); - + Console.WriteLine("Created generic new core thing"); + } + sender.Add(newThing); } } diff --git a/Thing.cs b/Thing.cs index 6e40df1..8730e7e 100644 --- a/Thing.cs +++ b/Thing.cs @@ -228,7 +228,12 @@ namespace Passer.RoboidControl { /// Function which can be used to create components in external engines. /// /// Currently this is used to create GameObjects in Unity - public virtual void CreateComponent() { } + public virtual void CreateComponent() { +#if UNITY_5_3_OR_NEWER + this.component = Unity.Thing.Create(this); + this.component.core = this; +#endif + } #endregion Init diff --git a/Unity/DebugConsole.cs b/Unity/DebugConsole.cs index cb4de46..5b4e4f6 100644 --- a/Unity/DebugConsole.cs +++ b/Unity/DebugConsole.cs @@ -3,7 +3,7 @@ using System.IO; using System.Text; using UnityEngine; -namespace Passer.Control.Unity { +namespace Passer.RoboidControl.Unity { public class UnityLogWriter : TextWriter { public override void Write(char value) { diff --git a/Unity/SiteServer.cs b/Unity/SiteServer.cs index fcc32b1..ae16757 100644 --- a/Unity/SiteServer.cs +++ b/Unity/SiteServer.cs @@ -11,7 +11,7 @@ namespace Passer.RoboidControl.Unity { public Queue thingQueue = new(); protected virtual void Awake() { - //Console.SetOut(new UnityLogWriter()); + Console.SetOut(new UnityLogWriter()); site = new(7681); RoboidControl.Thing.OnNewThing += HandleNewThing; @@ -22,6 +22,7 @@ namespace Passer.RoboidControl.Unity { } public void HandleNewThing(RoboidControl.Thing thing) { + Debug.Log("Handle New thing event"); site.Add(thing, false); thingQueue.Enqueue(thing); } @@ -29,7 +30,7 @@ namespace Passer.RoboidControl.Unity { protected virtual void Update() { site.Update((ulong)(Time.time * 1000)); while (thingQueue.TryDequeue(out RoboidControl.Thing thing)) - thing.CreateComponent(); + thing.CreateComponent(); } } diff --git a/Unity/Thing.cs b/Unity/Thing.cs index 7ad3e12..2d81238 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -2,7 +2,7 @@ using UnityEngine; namespace Passer.RoboidControl.Unity { - + /// /// The representation of a Thing in Unity /// @@ -12,7 +12,7 @@ namespace Passer.RoboidControl.Unity { /// The core C# thing /// [field: SerializeField] - public RoboidControl.Thing core {get; set; } + public RoboidControl.Thing core { get; set; } /// /// Set the core C# thing @@ -29,6 +29,23 @@ namespace Passer.RoboidControl.Unity { siteServer.site.Add(thing); } + public static Thing Create(RoboidControl.Thing core) { + Debug.Log("Creating new Unity thing"); + GameObject gameObj = string.IsNullOrEmpty(core.name) ? + new("Thing") : + new(core.name); + Thing component = gameObj.AddComponent(); + + component.core = core; + if (core.parent != null && core.parent.component != null) + gameObj.transform.SetParent(core.parent.component.transform, false); + + if (core.position != null) + gameObj.transform.localPosition = core.position.ToVector3(); + + return component; + } + /// /// Update the Unity representation ///