adventuresncode
adventuresncode

Reputation: 113

Receive UDP packet to Class. Design/Implementation assistance appreciated

I receive UDP data from localhost. This data needs to be used and kept track of. There could be >250k packets sent over the course of about 5 minutes. Currently, I create the IPEndPoint, and Receive the data. I then get the byte[] data into a string. Then I split the string into an array because the data is comma separated. From this array, I instantiate an object with the correct values from the array.

The incoming data has 4 properties and looks like this: ProductId=1284,Quantity=48623,Time=08:45:12.034,Date=2019-09-09

Currently I am able to Console.WriteLine the incoming packets, and cast each packet to an object (Product). Currently I do not store the data anywhere.

I need to monitor Quantity levels, and send alerts if the Quantity goes above or below certain thresholds. Secondly, I need to send another alert if the change in Quantity exceeds thresholds. This I will implement with if statements later and do not require assistance with at the moment. This alerts do need to be sent as UDP packets are received.

My questions are:

1) Should I implement this Asynchronously, or on a different thread?

2) Should I be sending the UDP Data directly to a Queue of type byte[], and then Dequeue each byte[] into an object? or keep instantiating new objects (or updating the quantity for each product) on the fly as the UDP data is received? Does Queue reduce the risk of dropped packets?

3) What is the best way to store each packet? To a List (This would keep everything in memory), a database (A bit beyond my current needs), or append each object to a text file (easy to write to, and can eventually build a database from)?

4) Is 250k packets over 5 minutes, and creating objects for all those packets considered a lot for a computer to handle?

Please advise if I can clarify anything. I apologize if any of my terminology is incorrect, I will learn and fix it. The coding I should be able to figure out on my own, it is the design and proper implementation that I am asking for guidance on. Thank you.

Below is code for setting up the UDP connection, and the class. I will be able to figure out the monitoring/messaging Quantity information after the UDP has been setup properly. I do have a Product Class not shown, it has several properties including ProductId, Quantity, Initial Quantity, Change in Quantity, Time and Date.

class Program
    {
        static void Main(string[] args)
        {
            int port = 22486;
            var client = new UdpClient();
            IPEndPoint localEp = new IPEndPoint(IPAddress.Any, port);
            client.Client.Bind(localEp);

            while (true)
            {
                try
                {
                    byte[] data = client.Receive(ref localEp);
                    string text = Encoding.UTF8.GetString(data);
                    string[] message = text.Split(',', '=');
                    Product product = new Product();
                    product.ProductId = message[1];
                    product.Quantity = message[3];
                }
                catch (Exception err)
                {
                    Console.WriteLine(err.ToString());
                }
            }
        }
    }

Upvotes: 0

Views: 210

Answers (1)

terrencep
terrencep

Reputation: 673

  1. If you need to do other things while listening on that port, make it a thread.

  2. queue it up in a byte array... a buffer of sorts.

  3. Are azure blobs an option for you? you can use the storage emulator and if you change your mind about storing a file in blob, you can switch to table storage (this is like a nosql approach where you provide a partition name.)

  4. No

Upvotes: 0

Related Questions