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.Collections.Generic;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Sockets;
namespace RoboidControl {
@ -20,6 +22,7 @@ namespace RoboidControl {
public Participant(string ipAddress, int port) {
this.ipAddress = ipAddress;
this.port = port;
this.udpClient = new UdpClient();
}
/// <summary>
@ -31,6 +34,8 @@ namespace RoboidControl {
/// </summary>
public int port = 0;
private UdpClient udpClient = null;
/// <summary>
/// he network Id to identify the participant
/// </summary>
@ -79,7 +84,7 @@ namespace RoboidControl {
this.things.Remove(thing);
}
#region Update
#region Update
/// <summary>
/// Update all things for this participant
@ -101,7 +106,25 @@ namespace RoboidControl {
}
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>
/// The collection of known participants.

View File

@ -116,7 +116,7 @@ namespace RoboidControl {
/// </summary>
public ulong publishInterval = 3000; // = 3 seconds
public byte[] buffer = new byte[1024];
//public byte[] buffer = new byte[1024];
public IPEndPoint endPoint = null;
public UdpClient udpClient = null;
@ -157,7 +157,8 @@ namespace RoboidControl {
if (this.remoteSite == null)
this.Publish(msg);
else
this.Send(this.remoteSite, msg);
//this.Send(this.remoteSite, msg);
this.remoteSite.Send(msg);
this.nextPublishMe = currentTimeMS + this.publishInterval;
}
@ -174,7 +175,8 @@ namespace RoboidControl {
if (thing.hierarchyChanged && !(this.isIsolated || this.networkId == 0)) {
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?
@ -185,14 +187,17 @@ namespace RoboidControl {
if (!(this.isIsolated || this.networkId == 0)) {
if (thing.terminate) {
DestroyMsg destroyMsg = new(this.networkId, thing);
this.Send(this.remoteSite, destroyMsg);
// this.Send(this.remoteSite, destroyMsg);
this.remoteSite.Send(destroyMsg);
}
else {
// Send to remote site
PoseMsg poseMsg = new(thing.owner.networkId, thing);
this.Send(this.remoteSite, poseMsg);
BinaryMsg binaryMsg = new(thing.owner.networkId, thing);
this.Send(this.remoteSite, binaryMsg);
// PoseMsg poseMsg = new(thing.owner.networkId, thing);
// this.Send(this.remoteSite, poseMsg);
// BinaryMsg binaryMsg = new(thing.owner.networkId, thing);
// 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)
@ -215,10 +220,12 @@ namespace RoboidControl {
if (thing == null)
continue;
PoseMsg poseMsg = new(thing.owner.networkId, thing);
this.Send(participant, poseMsg);
BinaryMsg binaryMsg = new(thing.owner.networkId, thing);
this.Send(participant, binaryMsg);
// PoseMsg poseMsg = new(thing.owner.networkId, thing);
// this.Send(participant, poseMsg);
// BinaryMsg binaryMsg = new(thing.owner.networkId, thing);
// 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) {
// 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));
// 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));
owner.Send(new PoseMsg(this.networkId, thing));
owner.Send(new BinaryMsg(this.networkId, thing));
}
public void PublishThingInfo(Thing thing) {
@ -246,16 +258,16 @@ 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;
// 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;
}
// 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);