Improved compatibility with C++

This commit is contained in:
Pascal Serrarens 2025-04-18 17:25:07 +02:00
parent 83159b487d
commit 62cc00b694
2 changed files with 30 additions and 57 deletions

View File

@ -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
/// <summary>
/// Create a porticiapnt
/// </summary>
public ParticipantUDP() {
//senders.Add(this);
}
/// <summary>
/// Create a participant with the give UDP port
/// </summary>
/// <param name="port">The port number on which to communicate</param>
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;
}
/// <summary>
@ -43,17 +40,16 @@ namespace RoboidControl {
/// </summary>
/// <param name="ipAddress">The ip address of the site server</param>
/// <param name="port">The port number of the site server</param>
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);
}
/// <summary>
@ -92,41 +88,27 @@ namespace RoboidControl {
return participant;
}
protected readonly Dictionary<byte, Func<Participant, byte, byte, Thing>> thingMsgProcessors = new();
// public delegate Thing ThingConstructor(Participant sender, byte networkId, byte thingId);
// public void Register(byte thingType, ThingConstructor constr) {
// thingMsgProcessors[thingType] = new Func<Participant, byte, byte, Thing>(constr);
// }
// public void Register<ThingClass>(Thing.Type thingType) where ThingClass : Thing {
// Register<ThingClass>((byte)thingType);
// }
// public void Register<ThingClass>(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<byte, Func<Participant, byte, byte, Thing>> 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);

View File

@ -9,27 +9,18 @@ namespace RoboidControl {
/// </summary>
public class SiteServer : ParticipantUDP {
public SiteServer(int port = 7681) : this("0.0.0.0", port) { }
/// <summary>
/// Create a new site server
/// </summary>
/// <param name="port"></param>
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<UdpClient, IPEndPoint>(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<TouchSensor>(Thing.Type.TouchSensor);
}
/// <summary>