C #: Diagram: the fastest way to put data from a received UDP packet

I am trying to write a simple UDP-to-Chart application (for Windows forms) that will receive raw data from ethernet and put it in a certain way in a diagram. Here is what I have so far: a form with two diagrams, a stream for receiving UDP packets:

public void serverThread()
    {
        UdpClient udpClient = new UdpClient(Convert.ToInt16(tbEthPort.Text));
        while (_serverWork)
        {
            try
            {
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, Convert.ToInt16(tbEthPort.Text));
                Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
                if (receiveBytes.Length > 0)
                    for (i = 0; i < receiveBytes.Length; i++)
                    {
                        bigDataIn[i] = receiveBytes[i];
                    }

and draw a second graph (which shows the entire contents of the package in a certain way):

if (graphicOn)
{
    for (i = 0; i < 32; i++)
    {
        graphicData[i + (graphicStep * 32)] = bigDataIn[i * 32 + graphicChan];
    }
    graphicStep++;
    if (graphicStep == 32) graphicStep = 0; 
    try
    {
        Invoke(new Action(() => { chartGraphic.Series["DataGraphic"].Points.DataBindXY(graphicEnum, graphicData);
        }));
    }
    catch
    {
    }
}

and a main thread with a timer to draw the first chart.

private void tmrHisto_Tick(object sender, EventArgs e)
    {
        int[] slice = new int[32];
        for (int i = 0; i < 32; i++)
            slice[i] = bigDataIn[i + 32 * histogramArrayStep];

        histogramArrayStep++;
        if (histogramArrayStep == 32) histogramArrayStep = 0;

        chartHistogram.Series["DataHistogram"].Points.Clear();
        for (int i = 0; i < HISTO_XMAX; i++)
        {
            chartHistogram.Series["DataHistogram"].Points.AddXY(i, slice[i]);
        }
    }

, , , . Invoke chartGraphic. ( 20 ) WireShark - . , 50 150 , .

, - . ?

+4
1

Invoke , receivethread (DataBindXY). .

, , . . , udpClient: Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); , "" .

gui-thread/timer. , / . .


UPDATE:

:

:

private List<byte[]> datagrams = new List<byte[]>();

public void serverThread()
{
    UdpClient udpClient = new UdpClient(Convert.ToInt16(tbEthPort.Text));
    while (_serverWork)
    {
        try
        {
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, Convert.ToInt16(tbEthPort.Text));
            Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);

            // add to the queue
            lock (datagrams)
                datagrams.Add(receiveBytes);
        }
    }
}

GUI:

private Timer timer = new Timer();

public void timer_Tick(object sender, EventArgs e)
{
    byte[][] data;

    // lock the queue as short as possible. (create a copy with ToArray())
    // this way the receive thread can run again..
    // this is also know as bulk processing..
    lock (datagrams)
    {
        data = datagrams.ToArray();
        datagrams.Clear();
    }

    // if no packets received, don't update anything
    if(data.Length == 0)
        return;

    // process the received data (multiple datagrams)
    for(byte[] item in data)
    {
        ...
    }
    // Update chart
}

, . , . .

+2

Source: https://habr.com/ru/post/1654813/


All Articles