Improved compatibility with C++
This commit is contained in:
parent
83159b487d
commit
62cc00b694
@ -15,6 +15,9 @@ namespace RoboidControl {
|
|||||||
|
|
||||||
public string name = "Participant";
|
public string name = "Participant";
|
||||||
|
|
||||||
|
public bool isIsolated = false;
|
||||||
|
public Participant remoteSite;
|
||||||
|
|
||||||
public IPEndPoint endPoint = null;
|
public IPEndPoint endPoint = null;
|
||||||
public UdpClient udpClient = null;
|
public UdpClient udpClient = null;
|
||||||
public string broadcastIpAddress = "255.255.255.255";
|
public string broadcastIpAddress = "255.255.255.255";
|
||||||
@ -23,19 +26,13 @@ namespace RoboidControl {
|
|||||||
|
|
||||||
#region Init
|
#region Init
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create a porticiapnt
|
|
||||||
/// </summary>
|
|
||||||
public ParticipantUDP() {
|
|
||||||
//senders.Add(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a participant with the give UDP port
|
/// Create a participant with the give UDP port
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="port">The port number on which to communicate</param>
|
/// <param name="port">The port number on which to communicate</param>
|
||||||
public ParticipantUDP(int port) : this() {
|
public ParticipantUDP(int port = 0) : base("127.0.0.1", port) {
|
||||||
this.port = port;
|
if (this.port == 0)
|
||||||
|
this.isIsolated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -43,17 +40,16 @@ namespace RoboidControl {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ipAddress">The ip address of the site server</param>
|
/// <param name="ipAddress">The ip address of the site server</param>
|
||||||
/// <param name="port">The port number 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() {
|
public ParticipantUDP(string ipAddress, int port = 7681, int localPort = 7681) : base("127.0.0.1", localPort) {
|
||||||
this.ipAddress = ipAddress;
|
if (this.port == 0)
|
||||||
this.port = port;
|
this.isIsolated = true;
|
||||||
|
else
|
||||||
|
this.remoteSite = new Participant(ipAddress, port);
|
||||||
|
|
||||||
this.udpClient = new UdpClient();
|
this.endPoint = new IPEndPoint(IPAddress.Any, localPort);
|
||||||
this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); // local port
|
this.udpClient = new UdpClient(localPort);
|
||||||
// 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.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null);
|
this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -92,41 +88,27 @@ namespace RoboidControl {
|
|||||||
return participant;
|
return participant;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected readonly Dictionary<byte, Func<Participant, byte, byte, Thing>> thingMsgProcessors = new();
|
// 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}");
|
|
||||||
// }
|
|
||||||
|
|
||||||
#endregion Init
|
#endregion Init
|
||||||
|
|
||||||
#region Update
|
#region Update
|
||||||
|
|
||||||
protected void ReceiveUDP(IAsyncResult result) {
|
protected void ReceiveUDP(IAsyncResult result) {
|
||||||
if (this.udpClient == null || this.endPoint == null)
|
UnityEngine.Debug.Log("received");
|
||||||
|
if (this.udpClient == null) // || this.endPoint == null)
|
||||||
return;
|
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!
|
// This does not yet take multi-packet messages into account!
|
||||||
if (this.endPoint == null)
|
if (endPoint == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// We can receive our own publish (broadcast) packages. How do we recognize them????
|
// We can receive our own publish (broadcast) packages. How do we recognize them????
|
||||||
// It is hard to determine our source port
|
// It is hard to determine our source port
|
||||||
string ipAddress = this.endPoint.Address.ToString();
|
string ipAddress = endPoint.Address.ToString();
|
||||||
Participant remoteParticipant = GetParticipant(ipAddress, this.endPoint.Port);
|
Participant remoteParticipant = GetParticipant(ipAddress, endPoint.Port);
|
||||||
remoteParticipant ??= AddParticipant(ipAddress, this.endPoint.Port);
|
remoteParticipant ??= AddParticipant(ipAddress, endPoint.Port);
|
||||||
|
|
||||||
ReceiveData(data, remoteParticipant);
|
ReceiveData(data, remoteParticipant);
|
||||||
|
|
||||||
|
@ -9,27 +9,18 @@ namespace RoboidControl {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SiteServer : ParticipantUDP {
|
public class SiteServer : ParticipantUDP {
|
||||||
|
|
||||||
public SiteServer(int port = 7681) : this("0.0.0.0", port) { }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a new site server
|
/// Create a new site server
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="port"></param>
|
/// <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.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}");
|
Console.Write($"Prepare receive on port {port}");
|
||||||
this.udpClient = new UdpClient(port); // for receiving
|
this.endPoint = new IPEndPoint(IPAddress.Any, port);
|
||||||
this.udpClient.BeginReceive(
|
this.udpClient = new UdpClient(port);
|
||||||
new AsyncCallback(result => ReceiveUDP(result)),
|
this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null);
|
||||||
new Tuple<UdpClient, IPEndPoint>(this.udpClient, new(IPAddress.Any, port)));
|
|
||||||
|
|
||||||
// Register<TouchSensor>(Thing.Type.TouchSensor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -45,8 +36,8 @@ namespace RoboidControl {
|
|||||||
protected override void Process(Participant sender, ParticipantMsg msg) {
|
protected override void Process(Participant sender, ParticipantMsg msg) {
|
||||||
base.Process(sender, msg);
|
base.Process(sender, msg);
|
||||||
//if (msg.networkId == 0) {
|
//if (msg.networkId == 0) {
|
||||||
//Console.WriteLine($"{this.name} received New Participant -> {sender.networkId}");
|
//Console.WriteLine($"{this.name} received New Participant -> {sender.networkId}");
|
||||||
this.Send(sender, new NetworkIdMsg(sender.networkId));
|
this.Send(sender, new NetworkIdMsg(sender.networkId));
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,8 +54,8 @@ namespace RoboidControl {
|
|||||||
// newThing = msgProcessor(sender, msg.networkId, msg.thingId);
|
// newThing = msgProcessor(sender, msg.networkId, msg.thingId);
|
||||||
// }
|
// }
|
||||||
// if (newThing == null) {
|
// if (newThing == null) {
|
||||||
newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);
|
newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);
|
||||||
// Console.WriteLine("Created generic new core thing");
|
// Console.WriteLine("Created generic new core thing");
|
||||||
// }
|
// }
|
||||||
if (msg.parentId != 0) {
|
if (msg.parentId != 0) {
|
||||||
Thing parentThing = Get(msg.networkId, msg.parentId);
|
Thing parentThing = Get(msg.networkId, msg.parentId);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user