async Task RunUdpListener()
        {
            while (true) //Continious restart logic...
            {
                try
                {
                    Console.WriteLine($"#{++retryCount} attempt to start UDP Listener...");

                    IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, PORT);
                    udpClient = new UdpClient(endPoint);

                    Console.WriteLine($"UDP Listener started on port {PORT}");
                    udpListenerRunning = true;

                    while (true)
                    {
                        UdpReceiveResult receivedResults = await udpClient.ReceiveAsync();

                        Console.WriteLine($"UDP message received ({receivedResults.Buffer.Length} bytes).");
                        messageProcessor = new MessageProcessor();
                        await messageProcessor.processMessage(receivedResults.Buffer, connectionString);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Failed to start UDP Listener on port {PORT}: {ex.Message}");
                }

                Task.Delay(5000).Wait();
            }
        }
Beispiel #2
0
        async Task RunUdpListener()
        {
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, PORT);

            udpClient = new UdpClient(endPoint);

            Console.WriteLine($"LoRaWAN server started on port {PORT}");


            while (true)
            {
                UdpReceiveResult receivedResults = await udpClient.ReceiveAsync();

                //Console.WriteLine($"UDP message received ({receivedResults.Buffer.Length} bytes) from port: {receivedResults.RemoteEndPoint.Port}");


                //Todo check that is an ack only, we could do a better check in a future verstion
                if (receivedResults.Buffer.Length == 12)
                {
                    remoteLoRaAggregatorIp   = receivedResults.RemoteEndPoint.Address;
                    remoteLoRaAggregatorPort = receivedResults.RemoteEndPoint.Port;
                }



                try
                {
                    MessageProcessor messageProcessor = new MessageProcessor();
                    _ = messageProcessor.processMessage(receivedResults.Buffer);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error processing the message {ex.Message}");
                }
            }
        }