Moved Sending to Participant

This commit is contained in:
Pascal Serrarens 2025-05-02 12:18:46 +02:00
parent 71adf127d9
commit e7a6d92740
2 changed files with 63 additions and 28 deletions

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
namespace RoboidControl { namespace RoboidControl {
@ -20,6 +22,7 @@ namespace RoboidControl {
public Participant(string ipAddress, int port) { public Participant(string ipAddress, int port) {
this.ipAddress = ipAddress; this.ipAddress = ipAddress;
this.port = port; this.port = port;
this.udpClient = new UdpClient();
} }
/// <summary> /// <summary>
@ -31,6 +34,8 @@ namespace RoboidControl {
/// </summary> /// </summary>
public int port = 0; public int port = 0;
private UdpClient udpClient = null;
/// <summary> /// <summary>
/// he network Id to identify the participant /// he network Id to identify the participant
/// </summary> /// </summary>
@ -79,7 +84,7 @@ namespace RoboidControl {
this.things.Remove(thing); this.things.Remove(thing);
} }
#region Update #region Update
/// <summary> /// <summary>
/// Update all things for this participant /// Update all things for this participant
@ -101,7 +106,25 @@ namespace RoboidControl {
} }
public ConcurrentQueue<UpdateEvent> updateQueue = new(); public ConcurrentQueue<UpdateEvent> updateQueue = new();
#endregion Update #endregion Update
#region Send
// Would be nice if this could be shared between all participants....
public byte[] buffer = new byte[1024];
public virtual bool Send(IMessage msg) {
int bufferSize = msg.Serialize(ref this.buffer);
if (bufferSize <= 0)
return true;
IPEndPoint participantEndpoint = new IPEndPoint(IPAddress.Parse(this.ipAddress), this.port);
Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}");
this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint);
return true;
}
#endregion Send
/// <summary> /// <summary>
/// The collection of known participants. /// The collection of known participants.

View File

@ -116,7 +116,7 @@ namespace RoboidControl {
/// </summary> /// </summary>
public ulong publishInterval = 3000; // = 3 seconds public ulong publishInterval = 3000; // = 3 seconds
public byte[] buffer = new byte[1024]; //public byte[] buffer = new byte[1024];
public IPEndPoint endPoint = null; public IPEndPoint endPoint = null;
public UdpClient udpClient = null; public UdpClient udpClient = null;
@ -157,7 +157,8 @@ namespace RoboidControl {
if (this.remoteSite == null) if (this.remoteSite == null)
this.Publish(msg); this.Publish(msg);
else else
this.Send(this.remoteSite, msg); //this.Send(this.remoteSite, msg);
this.remoteSite.Send(msg);
this.nextPublishMe = currentTimeMS + this.publishInterval; this.nextPublishMe = currentTimeMS + this.publishInterval;
} }
@ -174,7 +175,8 @@ namespace RoboidControl {
if (thing.hierarchyChanged && !(this.isIsolated || this.networkId == 0)) { if (thing.hierarchyChanged && !(this.isIsolated || this.networkId == 0)) {
ThingMsg thingMsg = new(this.networkId, thing); ThingMsg thingMsg = new(this.networkId, thing);
this.Send(this.remoteSite, thingMsg); // this.Send(this.remoteSite, thingMsg);
this.remoteSite.Send(thingMsg);
} }
// Why don't we do recursive? // Why don't we do recursive?
@ -185,14 +187,17 @@ namespace RoboidControl {
if (!(this.isIsolated || this.networkId == 0)) { if (!(this.isIsolated || this.networkId == 0)) {
if (thing.terminate) { if (thing.terminate) {
DestroyMsg destroyMsg = new(this.networkId, thing); DestroyMsg destroyMsg = new(this.networkId, thing);
this.Send(this.remoteSite, destroyMsg); // this.Send(this.remoteSite, destroyMsg);
this.remoteSite.Send(destroyMsg);
} }
else { else {
// Send to remote site // Send to remote site
PoseMsg poseMsg = new(thing.owner.networkId, thing); // PoseMsg poseMsg = new(thing.owner.networkId, thing);
this.Send(this.remoteSite, poseMsg); // this.Send(this.remoteSite, poseMsg);
BinaryMsg binaryMsg = new(thing.owner.networkId, thing); // BinaryMsg binaryMsg = new(thing.owner.networkId, thing);
this.Send(this.remoteSite, binaryMsg); // this.Send(this.remoteSite, binaryMsg);
this.remoteSite.Send(new PoseMsg(thing.owner.networkId, thing));
this.remoteSite.Send(new BinaryMsg(thing.owner.networkId, thing));
} }
} }
if (thing.terminate) if (thing.terminate)
@ -215,10 +220,12 @@ namespace RoboidControl {
if (thing == null) if (thing == null)
continue; continue;
PoseMsg poseMsg = new(thing.owner.networkId, thing); // PoseMsg poseMsg = new(thing.owner.networkId, thing);
this.Send(participant, poseMsg); // this.Send(participant, poseMsg);
BinaryMsg binaryMsg = new(thing.owner.networkId, thing); // BinaryMsg binaryMsg = new(thing.owner.networkId, thing);
this.Send(participant, binaryMsg); // this.Send(participant, binaryMsg);
participant.Send(new PoseMsg(thing.owner.networkId, thing));
participant.Send(new BinaryMsg(thing.owner.networkId, thing));
} }
} }
@ -230,11 +237,16 @@ namespace RoboidControl {
public void SendThingInfo(Participant owner, Thing thing) { public void SendThingInfo(Participant owner, Thing thing) {
// Console.WriteLine("Send thing info"); // Console.WriteLine("Send thing info");
this.Send(owner, new ThingMsg(this.networkId, thing)); // this.Send(owner, new ThingMsg(this.networkId, thing));
this.Send(owner, new NameMsg(this.networkId, thing)); // this.Send(owner, new NameMsg(this.networkId, thing));
this.Send(owner, new ModelUrlMsg(this.networkId, thing)); // this.Send(owner, new ModelUrlMsg(this.networkId, thing));
this.Send(owner, new PoseMsg(this.networkId, thing)); // this.Send(owner, new PoseMsg(this.networkId, thing));
this.Send(owner, new BinaryMsg(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));
owner.Send(new PoseMsg(this.networkId, thing));
owner.Send(new BinaryMsg(this.networkId, thing));
} }
public void PublishThingInfo(Thing thing) { public void PublishThingInfo(Thing thing) {
@ -246,16 +258,16 @@ namespace RoboidControl {
this.Publish(new BinaryMsg(this.networkId, thing)); this.Publish(new BinaryMsg(this.networkId, thing));
} }
public bool Send(Participant owner, IMessage msg) { // public bool Send(Participant owner, 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;
IPEndPoint participantEndpoint = new IPEndPoint(IPAddress.Parse(owner.ipAddress), owner.port); // IPEndPoint participantEndpoint = new IPEndPoint(IPAddress.Parse(owner.ipAddress), owner.port);
// Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}"); // // Console.WriteLine($"msg to {participantEndpoint.Address.ToString()} {participantEndpoint.Port}");
this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint); // this.udpClient?.Send(this.buffer, bufferSize, participantEndpoint);
return true; // 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);