From 6f6bae267a09a8732d2911f53c08519f8362bdd1 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 4 Jun 2025 09:43:08 +0200 Subject: [PATCH] Improved error handling --- Unity/Thing.cs | 21 +++--------- src/Participants/ParticipantUDP.cs | 54 +++++++++++++----------------- 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/Unity/Thing.cs b/Unity/Thing.cs index 035b3ae..b461f13 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -132,8 +132,10 @@ namespace RoboidControl.Unity { string extension = core.modelUrl[core.modelUrl.LastIndexOf(".")..]; if (extension == ".jpg" || extension == ".png") StartCoroutine(LoadJPG()); +#if GLTF else if (extension == ".gltf" || extension == ".glb") ProcessGltfModel(core); +#endif break; case PoseMsg.Id: Debug.Log($"{this.core.id} Handle Pose"); @@ -177,9 +179,9 @@ namespace RoboidControl.Unity { } } +#if GLTF bool loadingModel = false; private async void ProcessGltfModel(RoboidControl.Thing coreThing) { -#if GLTF if (!loadingModel) { loadingModel = true; @@ -200,21 +202,6 @@ namespace RoboidControl.Unity { await gltfImport.InstantiateMainSceneAsync(parentTransform); SkinnedMeshRenderer[] meshRenderers = parentTransform.GetComponentsInChildren(); -#if pHUMANOID4 - // if (parentThing.objectType == 7) { - // HumanoidControl hc = parentThing.gameObject.GetComponent(); - // if (hc == null) - // hc = parentThing.gameObject.AddComponent(); - - // foreach (SkinnedMeshRenderer meshRenderer in meshRenderers) { - // if (meshRenderer.rootBone != null) { - // Debug.Log("Found a skinned mesh with bones"); - // hc.RetrieveBonesFrom(meshRenderer.rootBone); - // break; - // } - // } - // } -#endif parentTransform.localScale = Vector3.one; if (meshRenderers.Length > 0) { foreach (SkinnedMeshRenderer meshRenderer in meshRenderers) { @@ -234,8 +221,8 @@ namespace RoboidControl.Unity { } } loadingModel = true; -#endif } +#endif private void ScanForThings(Transform rootTransform) { RoboidControl.Thing[] thingArray = this.core.owner.things.ToArray(); diff --git a/src/Participants/ParticipantUDP.cs b/src/Participants/ParticipantUDP.cs index 941d117..d54a607 100644 --- a/src/Participants/ParticipantUDP.cs +++ b/src/Participants/ParticipantUDP.cs @@ -160,7 +160,6 @@ namespace RoboidControl { if (this.remoteSite == null) this.Publish(msg); else - //this.Send(this.remoteSite, msg); this.remoteSite.Send(msg); this.nextPublishMe = currentTimeMS + this.publishIntervalMS; @@ -242,12 +241,6 @@ namespace RoboidControl { #region Send public void SendThingInfo(Participant owner, Thing thing) { - // Console.WriteLine("Send thing info"); - // this.Send(owner, new ThingMsg(this.networkId, thing)); - // this.Send(owner, new NameMsg(this.networkId, thing)); - // this.Send(owner, new ModelUrlMsg(this.networkId, thing)); - // this.Send(owner, new PoseMsg(this.networkId, thing)); - // this.Send(owner, new BinaryMsg(this.networkId, thing)); owner.Send(new ThingMsg(this.networkId, thing)); owner.Send(new NameMsg(this.networkId, thing)); owner.Send(new ModelUrlMsg(this.networkId, thing)); @@ -264,23 +257,12 @@ namespace RoboidControl { this.Publish(new BinaryMsg(this.networkId, thing)); } - // public bool Send(Participant owner, IMessage msg) { - // int bufferSize = msg.Serialize(ref this.buffer); - // if (bufferSize <= 0) - // return true; - - // IPEndPoint participantEndpoint = new IPEndPoint(IPAddress.Parse(owner.ipAddress), owner.port); - // // Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); - // this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint); - // return true; - // } - public bool Publish(IMessage msg) { int bufferSize = msg.Serialize(ref this.buffer); if (bufferSize <= 0) return true; - // Console.WriteLine($"publish to {broadcastIpAddress.ToString()} {this.port}"); + Console.WriteLine($"publish to {broadcastIpAddress.ToString()} {this.port}"); this.udpClient?.Send(this.buffer, bufferSize, this.broadcastIpAddress, this.port); return true; } @@ -291,23 +273,35 @@ namespace RoboidControl { protected void ReceiveUDP(IAsyncResult result) { try { - // UnityEngine.Debug.Log("received"); - if (this.udpClient == null) // || this.endPoint == null) + if (this.udpClient == null) return; - byte[] data = this.udpClient.EndReceive(result, ref endPoint); + byte[] data = { }; + try { + data = this.udpClient.EndReceive(result, ref endPoint); + } + catch (SocketException e) { + if (e.ErrorCode != 10054) + throw; +#if DEBUG + else + Console.WriteLine($"No receiver found for the previously sent UDP packet"); +#endif + } // This does not yet take multi-packet messages into account! 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 = endPoint.Address.ToString(); - if (ipAddress != this.ipAddress) { - Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port); - remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port, this); + if (data.Length > 0) { + // We can receive our own publish (broadcast) packages. How do we recognize them???? + // It is hard to determine our source port + string ipAddress = endPoint.Address.ToString(); + if (ipAddress != this.ipAddress) { + Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port); + remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port, this); - ReceiveData(data, remoteParticipant); + ReceiveData(data, remoteParticipant); + } } udpClient.BeginReceive(new AsyncCallback(callbackResult => ReceiveUDP(callbackResult)), null); @@ -437,7 +431,7 @@ namespace RoboidControl { protected virtual void Process(Participant sender, BinaryMsg msg) { #if DEBUG -// Console.WriteLine($"{this.name}: Process BinaryMsg [{msg.networkId}/{msg.thingId}] {msg.dataLength}"); + // Console.WriteLine($"{this.name}: Process BinaryMsg [{msg.networkId}/{msg.thingId}] {msg.dataLength}"); #endif Thing thing = sender.Get(msg.thingId); thing?.ProcessBinary(msg.data);