jchristn / SuperSimpleTcp

Simple wrapper for TCP client and server in C# with SSL support
MIT License
459 stars 95 forks source link

DataReceived is missing "single transmit" events #214

Closed jboss10 closed 2 months ago

jboss10 commented 3 months ago

To start, thanks Joel for your efforts on this repository.

This is more of design question/discussion. I'm porting some code from an old WinForms application to a more modern architecture using Avalonia.

The code being ported was using the TCPListener/TCPClient in it's raw form which proved to just work but wanted a bit more robustness and simplicity with the library.

My current setup is: GUI frontend that talks with a 'middleware' piece of 'C' software that acts as a mediator between the GUI and a final end point.

  1. GUI front-end that:
    • Establishes a server service using loopback on a given port. The server is responsible for receiving data from this middleware and displays data as needed (which is transmitted from the end point).
    • Establishes a client service using loopback on a given port. The client is responsible for taking UI data and sending to middleware which then sends to final end point.
  2. The middleware piece is to remained untouched

The issue I'm running into from legacy software to new implementation is "single" transmits from 'final endpoint -> middleware -> GUI'. I noticed when data is 'set' on the end point and progresses through this line of transmission, my DataReceived function doesn't reliably get the data that is sent from the middleware. I do see it being sent as I have the means to debug it but it always seems like the data gets lost or dropped.

Scenario would be:

  1. Final endpoint pushes a button
  2. End point transmits the data associated w/ button press
  3. Middleware processes
  4. Middleware then relays the data to GUI

However, when data from the middleware sends the data in a periodic fashion, I'm not seeing any issue with receiving it. I'm assuming the periodic nature of this is what's helping.

Any ideas on implementation? I put my DataReceived function below:

    private void DataReceived(object? sender, SuperSimpleTcp.DataReceivedEventArgs e)
    {

        // TODO: This 'message' doesn't always come in.
        if (e.Data.Array[4] == 0x08 && e.Data.Array[5] == 0x01 && (e.Data.Array[6] == 0x00 || e.Data.Array[6] == 0x01))
        {
            Debug.WriteLine("Received My Non-Periodic Message");
        }

        try
        {
            byte[]? data = e.Data.Array;

            if (data is not null && data.Length > 0)
            {
                // Convert the first 4 bytes (length of data)
                int dataLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 0));

                // Extract the rest of the data
                byte[] dataPayload = new byte[dataLength];
                Array.Copy(data, 4, dataPayload, 0, dataLength);

                ProcessStatusData(dataPayload);
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.ToString());
        }
    }