RoboidControl-csharp/SiteServer.cs

59 lines
2.0 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace Passer.Control.Core {
public class SiteServer : Participant {
public SiteServer(int port = 7681) {
this.name = "Site Server";
this.ipAddress = "0.0.0.0";
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 IPEndPoint(IPAddress.Any, port)));
}
public void Close() {
this.udpClient?.Close();
}
public override void Publish() {
}
protected override void Process(RemoteParticipant sender, ClientMsg msg) {
if (msg.networkId == 0) {
Console.WriteLine($"{this.name} received New Client -> {sender.networkId}");
this.Send(sender, new NetworkIdMsg(sender.networkId));
}
}
protected override void Process(RemoteParticipant sender, NetworkIdMsg msg) { }
protected override void Process(RemoteParticipant sender, ThingMsg msg) {
//Console.WriteLine($"SiteServer: Process thing [{msg.networkId}/{msg.thingId}]");
Thing thing = sender.Get(msg.networkId, msg.thingId);
if (thing == null) {
Thing newThing = null;
if (thingMsgProcessors.TryGetValue(msg.thingType, out Func<byte, byte, Thing> value)) {
if (value != null)
newThing = value(msg.networkId, msg.thingId);
}
if (newThing == null)
newThing = new Thing(sender, msg.networkId, msg.thingId, msg.thingType);
sender.Add(newThing);
}
}
}
}