using System; using System.Net; using System.Net.Sockets; namespace Passer.Control.Core { public class SiteServer : Participant { public SiteServer(int port = 7681) { this.ipAddress = "0.0.0.0"; this.port = port; this.endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), 0); // for sending, but should not be used really... this.udpClient = new UdpClient(); // for receiving this.udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, port)); this.udpClient.BeginReceive(new AsyncCallback(result => ReceiveUDP(result)), null); this.name = "Site Server"; } public override void Update(long currentTimeMs) { Thing.UpdateAll(currentTimeMs); } protected override void Process(Participant sender, ClientMsg msg) { if (msg.networkId == 0) { Console.WriteLine($"{this.name} received New Client -> {sender.networkId}"); sender.Send(new NetworkIdMsg(sender.networkId)); } } protected override void Process(Participant sender, NetworkIdMsg msg) { } protected override void Process(ThingMsg msg) { Thing thing = Thing.Get(msg.networkId, msg.thingId); if (thing == null) { switch ((Thing.Type) msg.thingType) { case Thing.Type.TemperatureSensor: new TemperatureSensor(msg.networkId, msg.thingId); break; default: new Thing(this, msg.networkId, msg.thingId, msg.thingType); break; } } } } }