Improved error handling

This commit is contained in:
Pascal Serrarens 2025-06-04 09:43:08 +02:00
parent 2d0acd2a82
commit 6f6bae267a
2 changed files with 28 additions and 47 deletions

View File

@ -132,8 +132,10 @@ namespace RoboidControl.Unity {
string extension = core.modelUrl[core.modelUrl.LastIndexOf(".")..]; string extension = core.modelUrl[core.modelUrl.LastIndexOf(".")..];
if (extension == ".jpg" || extension == ".png") if (extension == ".jpg" || extension == ".png")
StartCoroutine(LoadJPG()); StartCoroutine(LoadJPG());
#if GLTF
else if (extension == ".gltf" || extension == ".glb") else if (extension == ".gltf" || extension == ".glb")
ProcessGltfModel(core); ProcessGltfModel(core);
#endif
break; break;
case PoseMsg.Id: case PoseMsg.Id:
Debug.Log($"{this.core.id} Handle Pose"); Debug.Log($"{this.core.id} Handle Pose");
@ -177,9 +179,9 @@ namespace RoboidControl.Unity {
} }
} }
#if GLTF
bool loadingModel = false; bool loadingModel = false;
private async void ProcessGltfModel(RoboidControl.Thing coreThing) { private async void ProcessGltfModel(RoboidControl.Thing coreThing) {
#if GLTF
if (!loadingModel) { if (!loadingModel) {
loadingModel = true; loadingModel = true;
@ -200,21 +202,6 @@ namespace RoboidControl.Unity {
await gltfImport.InstantiateMainSceneAsync(parentTransform); await gltfImport.InstantiateMainSceneAsync(parentTransform);
SkinnedMeshRenderer[] meshRenderers = parentTransform.GetComponentsInChildren<SkinnedMeshRenderer>(); SkinnedMeshRenderer[] meshRenderers = parentTransform.GetComponentsInChildren<SkinnedMeshRenderer>();
#if pHUMANOID4
// if (parentThing.objectType == 7) {
// HumanoidControl hc = parentThing.gameObject.GetComponent<HumanoidControl>();
// if (hc == null)
// hc = parentThing.gameObject.AddComponent<HumanoidControl>();
// 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; parentTransform.localScale = Vector3.one;
if (meshRenderers.Length > 0) { if (meshRenderers.Length > 0) {
foreach (SkinnedMeshRenderer meshRenderer in meshRenderers) { foreach (SkinnedMeshRenderer meshRenderer in meshRenderers) {
@ -234,8 +221,8 @@ namespace RoboidControl.Unity {
} }
} }
loadingModel = true; loadingModel = true;
#endif
} }
#endif
private void ScanForThings(Transform rootTransform) { private void ScanForThings(Transform rootTransform) {
RoboidControl.Thing[] thingArray = this.core.owner.things.ToArray(); RoboidControl.Thing[] thingArray = this.core.owner.things.ToArray();

View File

@ -160,7 +160,6 @@ namespace RoboidControl {
if (this.remoteSite == null) if (this.remoteSite == null)
this.Publish(msg); this.Publish(msg);
else else
//this.Send(this.remoteSite, msg);
this.remoteSite.Send(msg); this.remoteSite.Send(msg);
this.nextPublishMe = currentTimeMS + this.publishIntervalMS; this.nextPublishMe = currentTimeMS + this.publishIntervalMS;
@ -242,12 +241,6 @@ namespace RoboidControl {
#region Send #region Send
public void SendThingInfo(Participant owner, Thing thing) { 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 ThingMsg(this.networkId, thing));
owner.Send(new NameMsg(this.networkId, thing)); owner.Send(new NameMsg(this.networkId, thing));
owner.Send(new ModelUrlMsg(this.networkId, thing)); owner.Send(new ModelUrlMsg(this.networkId, thing));
@ -264,23 +257,12 @@ namespace RoboidControl {
this.Publish(new BinaryMsg(this.networkId, thing)); 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) { public bool Publish(IMessage msg) {
int bufferSize = msg.Serialize(ref this.buffer); int bufferSize = msg.Serialize(ref this.buffer);
if (bufferSize <= 0) if (bufferSize <= 0)
return true; 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); this.udpClient?.Send(this.buffer, bufferSize, this.broadcastIpAddress, this.port);
return true; return true;
} }
@ -291,23 +273,35 @@ namespace RoboidControl {
protected void ReceiveUDP(IAsyncResult result) { protected void ReceiveUDP(IAsyncResult result) {
try { try {
// UnityEngine.Debug.Log("received"); if (this.udpClient == null)
if (this.udpClient == null) // || this.endPoint == null)
return; 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! // This does not yet take multi-packet messages into account!
if (endPoint == null) if (endPoint == null)
return; return;
// We can receive our own publish (broadcast) packages. How do we recognize them???? if (data.Length > 0) {
// It is hard to determine our source port // We can receive our own publish (broadcast) packages. How do we recognize them????
string ipAddress = endPoint.Address.ToString(); // It is hard to determine our source port
if (ipAddress != this.ipAddress) { string ipAddress = endPoint.Address.ToString();
Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port); if (ipAddress != this.ipAddress) {
remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port, this); 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); udpClient.BeginReceive(new AsyncCallback(callbackResult => ReceiveUDP(callbackResult)), null);
@ -437,7 +431,7 @@ namespace RoboidControl {
protected virtual void Process(Participant sender, BinaryMsg msg) { protected virtual void Process(Participant sender, BinaryMsg msg) {
#if DEBUG #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 #endif
Thing thing = sender.Get(msg.thingId); Thing thing = sender.Get(msg.thingId);
thing?.ProcessBinary(msg.data); thing?.ProcessBinary(msg.data);