88 lines
3.1 KiB
C#
88 lines
3.1 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
|
|
namespace RoboidControl {
|
|
|
|
/// <summary>
|
|
/// A site server is a participant which provides a shared simulated environment
|
|
/// </summary>
|
|
public class SiteServer : ParticipantUDP {
|
|
|
|
/// <summary>
|
|
/// Create a new site server
|
|
/// </summary>
|
|
/// <param name="port"></param>
|
|
public SiteServer(int port = 7681) : base(port) {
|
|
this.name = "Site Server";
|
|
|
|
Console.Write($"Prepare receive on port {port}");
|
|
this.endPoint = new IPEndPoint(IPAddress.Any, port);
|
|
this.udpClient = new UdpClient(port);
|
|
this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Close the site
|
|
/// </summary>
|
|
public void Close() {
|
|
this.udpClient?.Close();
|
|
}
|
|
|
|
protected override void UpdateMyThings(ulong currentTimeMS) {
|
|
int n = this.things.Count;
|
|
for (int ix = 0; ix < n; ix++) {
|
|
Thing thing = this.things[ix];
|
|
if (thing == null || thing.parent != null)
|
|
continue;
|
|
|
|
thing.Update(currentTimeMS, true);
|
|
if (this.isIsolated || this.networkId == 0)
|
|
continue;
|
|
|
|
// Send to all other participants
|
|
foreach (Participant participant in Participant.participants) {
|
|
if (participant == null || participant == this)
|
|
continue;
|
|
|
|
PoseMsg poseMsg = new(thing.owner.networkId, thing);
|
|
this.Send(participant, poseMsg);
|
|
BinaryMsg binaryMsg = new(thing.owner.networkId, thing);
|
|
this.Send(participant, binaryMsg);
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void Publish() {
|
|
}
|
|
|
|
protected override void Process(Participant sender, ParticipantMsg msg) {
|
|
base.Process(sender, msg);
|
|
//if (msg.networkId == 0) {
|
|
//Console.WriteLine($"{this.name} received New Participant -> {sender.networkId}");
|
|
this.Send(sender, new NetworkIdMsg(sender.networkId));
|
|
//}
|
|
}
|
|
|
|
protected override void Process(Participant sender, NetworkIdMsg msg) { }
|
|
|
|
protected override void Process(Participant sender, ThingMsg msg) {
|
|
Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}] {msg.thingType} {msg.parentId} ");
|
|
Thing thing = sender.Get(msg.networkId, msg.thingId);
|
|
if (thing == null) {
|
|
thing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);
|
|
// Console.WriteLine("Created generic new core thing");
|
|
// }
|
|
}
|
|
|
|
if (msg.parentId != 0) {
|
|
Thing parentThing = Get(msg.networkId, msg.parentId);
|
|
if (parentThing == null)
|
|
Console.WriteLine($"Could not find parent [{msg.networkId}/{msg.parentId}]");
|
|
else
|
|
thing.parent = parentThing;
|
|
}
|
|
}
|
|
}
|
|
} |