From 62cc00b694cc3e511c57a1d0763858f5f392aed7 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 18 Apr 2025 17:25:07 +0200 Subject: [PATCH] Improved compatibility with C++ --- src/ParticipantUDP.cs | 62 +++++++++++++++---------------------------- src/SiteServer.cs | 25 ++++++----------- 2 files changed, 30 insertions(+), 57 deletions(-) diff --git a/src/ParticipantUDP.cs b/src/ParticipantUDP.cs index 01071b5..7d8b27b 100644 --- a/src/ParticipantUDP.cs +++ b/src/ParticipantUDP.cs @@ -15,6 +15,9 @@ namespace RoboidControl { public string name = "Participant"; + public bool isIsolated = false; + public Participant remoteSite; + public IPEndPoint endPoint = null; public UdpClient udpClient = null; public string broadcastIpAddress = "255.255.255.255"; @@ -23,19 +26,13 @@ namespace RoboidControl { #region Init - /// - /// Create a porticiapnt - /// - public ParticipantUDP() { - //senders.Add(this); - } - /// /// Create a participant with the give UDP port /// /// The port number on which to communicate - public ParticipantUDP(int port) : this() { - this.port = port; + public ParticipantUDP(int port = 0) : base("127.0.0.1", port) { + if (this.port == 0) + this.isIsolated = true; } /// @@ -43,17 +40,16 @@ namespace RoboidControl { /// /// The ip address of the site server /// The port number of the site server - public ParticipantUDP(string ipAddress = "0.0.0.0", int port = 7681) : this() { - this.ipAddress = ipAddress; - this.port = port; + public ParticipantUDP(string ipAddress, int port = 7681, int localPort = 7681) : base("127.0.0.1", localPort) { + if (this.port == 0) + this.isIsolated = true; + else + this.remoteSite = new Participant(ipAddress, port); - this.udpClient = new UdpClient(); - this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); // local port - // this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port); // for sending - - // this.udpClient = new UdpClient(port); // for receiving - // this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); + this.endPoint = new IPEndPoint(IPAddress.Any, localPort); + this.udpClient = new UdpClient(localPort); this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); + } /// @@ -92,41 +88,27 @@ namespace RoboidControl { return participant; } - protected readonly Dictionary> thingMsgProcessors = new(); - - // public delegate Thing ThingConstructor(Participant sender, byte networkId, byte thingId); - // public void Register(byte thingType, ThingConstructor constr) { - // thingMsgProcessors[thingType] = new Func(constr); - // } - - // public void Register(Thing.Type thingType) where ThingClass : Thing { - // Register((byte)thingType); - // } - - // public void Register(byte thingType) where ThingClass : Thing { - // thingMsgProcessors[thingType] = (participant, networkId, thingId) => - // Activator.CreateInstance(typeof(ThingClass), participant, networkId, thingId) as ThingClass; - // Console.WriteLine($"Registering {typeof(ThingClass)} for thing type {thingType}"); - // } + // protected readonly Dictionary> thingMsgProcessors = new(); #endregion Init #region Update protected void ReceiveUDP(IAsyncResult result) { - if (this.udpClient == null || this.endPoint == null) + UnityEngine.Debug.Log("received"); + if (this.udpClient == null) // || this.endPoint == null) return; - byte[] data = this.udpClient.EndReceive(result, ref this.endPoint); + byte[] data = this.udpClient.EndReceive(result, ref endPoint); // This does not yet take multi-packet messages into account! - if (this.endPoint == null) + if (endPoint == null) return; // We can receive our own publish (broadcast) packages. How do we recognize them???? // It is hard to determine our source port - string ipAddress = this.endPoint.Address.ToString(); - Participant remoteParticipant = GetParticipant(ipAddress, this.endPoint.Port); - remoteParticipant ??= AddParticipant(ipAddress, this.endPoint.Port); + string ipAddress = endPoint.Address.ToString(); + Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port); + remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port); ReceiveData(data, remoteParticipant); diff --git a/src/SiteServer.cs b/src/SiteServer.cs index 1032c7f..e6a9141 100644 --- a/src/SiteServer.cs +++ b/src/SiteServer.cs @@ -9,27 +9,18 @@ namespace RoboidControl { /// public class SiteServer : ParticipantUDP { - public SiteServer(int port = 7681) : this("0.0.0.0", port) { } - /// /// Create a new site server /// /// - public SiteServer(string ipAddress = "0.0.0.0", int port = 7681) : base() { + public SiteServer(int port = 7681) : base(port) { this.name = "Site Server"; - this.ipAddress = ipAddress; - this.port = port; - - 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(IPAddress.Any, port))); + this.endPoint = new IPEndPoint(IPAddress.Any, port); + this.udpClient = new UdpClient(port); + this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); - // Register(Thing.Type.TouchSensor); } /// @@ -45,8 +36,8 @@ namespace RoboidControl { protected override void Process(Participant sender, ParticipantMsg msg) { base.Process(sender, msg); //if (msg.networkId == 0) { - //Console.WriteLine($"{this.name} received New Participant -> {sender.networkId}"); - this.Send(sender, new NetworkIdMsg(sender.networkId)); + //Console.WriteLine($"{this.name} received New Participant -> {sender.networkId}"); + this.Send(sender, new NetworkIdMsg(sender.networkId)); //} } @@ -63,8 +54,8 @@ namespace RoboidControl { // newThing = msgProcessor(sender, msg.networkId, msg.thingId); // } // if (newThing == null) { - newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); - // Console.WriteLine("Created generic new core thing"); + newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType); + // Console.WriteLine("Created generic new core thing"); // } if (msg.parentId != 0) { Thing parentThing = Get(msg.networkId, msg.parentId);