Exemple #1
0
        void ProcessRecievedDataInDataMode()
        {
            for (int i = 0; i < _listBuffer.Count; i++)
            {
                if (_listBuffer[i] == 0)
                {
                    if (i > 2 &&
                        OnPacketReceived != null)
                    {
                        var dataLength = i - 1;
                        var data       = new byte[dataLength];
                        for (int j = 0; j < dataLength; j++)
                        {
                            data[j] = _listBuffer[j];
                        }

                        var decodedDataLength = COBS.GetDecodedArraySize(data);
                        if (decodedDataLength != 0)
                        {
                            var decodedData = new byte[decodedDataLength];
                            COBS.Decode(data, dataLength, decodedData);

                            var hash         = _listBuffer[dataLength];
                            var computedHash = HashCheck.Hash(decodedData);

                            if (hash == computedHash)
                            {
                                OnPacketReceived(decodedData);
                            }
                        }
                    }

                    _listBuffer.RemoveRange(0, i + 1);
                    i = -1;
                }
            }
        }
Exemple #2
0
        public void SendPacket(byte[] data)
        {
            if (_mode != EMode.Data)
            {
                SetMode(EMode.Data);
            }

            // TODO big length buffering

            // Packet structure
            // Data in cobs ..... n bytes
            // Hash of NON ENCODED data 1 bytes
            // Zero-byte

            var encodedSize = COBS.GetEncodedArraySize(data);
            var encoded     = new byte[encodedSize + 2];

            COBS.Encode(data, data.Length, encoded);
            encoded[encodedSize]     = (byte)HashCheck.Hash(data);
            encoded[encodedSize + 1] = 0;

            // TODO split length
            SendRawData(encoded);
        }