public static void ServerThread() { //TODO: Provide datapacketbuffer access as a service (signal?) to provide realtime counts without hitting the DB var dataPacketBuffer = new List <DetectionDataPacket>(); UdpClient udpClient = new UdpClient(5000); while (true) { IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); Models.IncomingRawUdpPacket potentialPacket = new Models.IncomingRawUdpPacket(udpClient.Receive(ref RemoteIpEndPoint)); potentialPacket.PopulateFromIncomingPacket(RemoteIpEndPoint); Console.WriteLine($"{potentialPacket.GetPacketType().ToString()}"); if (potentialPacket.GetPacketType() != Models.PacketType.Unknown) { var newPacket = potentialPacket.Generate(); Console.WriteLine($"New packet incoming on thread {Thread.CurrentThread.ManagedThreadId} : {potentialPacket.IPAddress}:{potentialPacket.IPPort}"); // Throw in buffer if (newPacket is DetectionDataPacket dPacket) { dataPacketBuffer.Add(dPacket); //TODO: If coincedent, keep it, else wait till it ages var data = FindStrike(dataPacketBuffer); data.ForEach(x => dataPacketBuffer.Remove(x)); if (data != null) { Task.Run(() => { using (var context = new LightningContext()) { foreach (var d in data) { d.StoreInDB(context); } } }); } dataPacketBuffer.RemoveAll(x => x.GetPacket().Persisteddate < DateTime.Now.AddMilliseconds(-500).Ticks); } else { Task.Run(() => { using (var context = new LightningContext()) { newPacket.StoreInDB(context); } }); } } else { Task.Run(() => Console.WriteLine($"Unknown Packet incoming on thread {Thread.CurrentThread.ManagedThreadId}")); } } }
public DetectionStatusPacket(IncomingRawUdpPacket incomingRawUdpPacket) { this.incomingRawUdpPacket = incomingRawUdpPacket; packet = new Statuspackets(this.incomingRawUdpPacket.RawBytes) { Received = DateTimeOffset.Now.ToUnixTimeSeconds(), Persisteddate = DateTime.Now.Ticks }; }
public DetectionDataPacket(IncomingRawUdpPacket packetWrapper) { this.incomingRawUdpPacket = packetWrapper; packet = new Datapackets(packetWrapper.RawBytes) { Address = incomingRawUdpPacket.IPAddress, Persisteddate = DateTime.Now.Ticks }; }