RoboidControl-csharp/SiteServer.cs
2025-02-04 17:25:51 +01:00

48 lines
1.7 KiB
C#

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.publishInterval = 0;
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.Client.Bind(new IPEndPoint(IPAddress.Any, port));
IPEndPoint receiveEndpoint = new(IPAddress.Any, port);
this.udpClient.BeginReceive(
new AsyncCallback(result => ReceiveUDP(result)),
new Tuple<UdpClient, IPEndPoint>(this.udpClient, receiveEndpoint));
}
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(ThingMsg msg) {
Thing thing = Thing.Get(msg.networkId, msg.thingId);
if (thing == null) {
if (thingMsgProcessors.ContainsKey(msg.thingType))
thingMsgProcessors[msg.thingType](msg.networkId, msg.thingId);
else
new Thing(this, msg.networkId, msg.thingId, msg.thingType);
}
}
}
}