/// <summary>
 /// Constructor
 /// </summary>
 /// <param name="webSocket">WebSocket with open connection that will be used by the object</param>
 /// <param name="receivedMessageDelegate">Will be called everytime the websocket receives a message of text type</param>
 public MessageHandler(WebSocket webSocket, ReceivedMessageDelegate receivedMessageDelegate, ReceivedImageDelegate receivedImageDelegate)
 {
     _webSocket      = webSocket;
     ReceivedMessage = receivedMessageDelegate;
     ReceivedImage   = receivedImageDelegate;
     ReceiveMessage();
 }
Ejemplo n.º 2
0
        public void NewRXPacket(byte[] data, int dataLen, ReceivedMessageDelegate ReceivedMessage)
        {
            // RX Parser
            for (int i = 0; i != dataLen; i++)
            {
                byte b = data[i];
                switch (RXPhase)
                {
                // HEADER
                case ERXPhase.HDR_FF:
                    if (b == 0xFF)
                    {
                        RXPhase = ERXPhase.HDR_FE;     // wait for start
                    }
                    break;

                case ERXPhase.HDR_FE:
                    if (b == 0xFE)
                    {
                        RXPhase = ERXPhase.HDR_A5;     // wait for start
                    }
                    else
                    {
                        RXPhase = ERXPhase.HDR_FF;     // reset
                        HeaderFails++;
                    }
                    break;

                case ERXPhase.HDR_A5:
                    if (b == 0xA5)
                    {
                        RXPhase = ERXPhase.HDR_5A;     // wait for start
                    }
                    else
                    {
                        RXPhase = ERXPhase.HDR_FF;     // reset
                        HeaderFails++;
                    }
                    break;

                case ERXPhase.HDR_5A:
                    if (b == 0x5A)
                    {
                        RXPhase = ERXPhase.TYPE;     // wait for start
                    }
                    else
                    {
                        RXPhase = ERXPhase.HDR_FF;     // reset
                        HeaderFails++;
                    }
                    break;

                case ERXPhase.TYPE:
                    Type    = b;
                    RXPhase = ERXPhase.LEN;
                    break;

                case ERXPhase.LEN:
                    Len       = b;
                    DataIndex = 0;     // reset data counter
                    RXPhase   = ERXPhase.DATA;
                    break;


                case ERXPhase.DATA:
                    Data[DataIndex] = b;
                    DataIndex++;
                    if (DataIndex >= Len)
                    {
                        RXPhase = ERXPhase.CRC_A;
                    }
                    break;

                case ERXPhase.CRC_A:
                    CRC     = (uint)(b << 24);
                    RXPhase = ERXPhase.CRC_B;
                    break;

                case ERXPhase.CRC_B:
                    CRC    += (uint)(b << 16);
                    RXPhase = ERXPhase.CRC_C;
                    break;

                case ERXPhase.CRC_C:
                    CRC    += (uint)(b << 8);
                    RXPhase = ERXPhase.CRC_D;
                    break;

                case ERXPhase.CRC_D:
                    CRC += b;
                    // check CRC, process data!!
                    uint calculatedCRC = Crc32.CalculateCrc32(Data, Len);
                    if (calculatedCRC == CRC)
                    {
                        // message OK, process!
                        MsgReceivedOK++;

                        ReceivedMessage(Type, Data, Len);
                    }
                    else
                    {
                        // CRC Failed
                        CrcErrors++;
                    }

                    RXPhase = ERXPhase.HDR_FF;
                    break;
                }
            }
        }
 public void UnsubscribeReceiveEvent(ReceivedMessageDelegate subscriber)
 {
     OnReceivedMessage -= subscriber;
 }