Example #1
0
        public void AddBlock(byte[] Buffer)
        {
            //Add data to Block

            //Minimul lenth required is CAT (1 bit) + Length of Data (2 bit)
            if (Buffer.Count() <= 3)
            {
                return;
            }

            //Start with data that received CAT62;
            int CatSpecification = Buffer.First();

            if (CatSpecification != 62)
            {
                return;
            }

            byte[] DataBlockBytes = Buffer.Skip(3).ToArray();
            //AppLog.Add($"Received {DataBlockBytes.Count()}...");
            MultiDataBlock   multiBlock = new MultiDataBlock();
            List <DataBlock> blocks     = multiBlock.Parse(DataBlockBytes);

            //Raise the event once block is received
            OnBlockReceive?.Invoke(this, blocks);
        }
Example #2
0
        private void DataBlockThread()
        {
            byte[] DataBlockBytes;

            //Minimul lenth required is CAT (1 bit) + Length of Data (2 bit)
            if (_ReceivedBuffer.Count() <= 3)
            {
                return;
            }

            //Start with data that received CAT62;
            int CatSpecification = _ReceivedBuffer.First();

            if (CatSpecification != 62)
            {
                lock (_lockBuffer)
                    _ReceivedBuffer.RemoveAt(0);
                return;
            }


            //Get the length of Data
            byte[] _DataLengthBytes = new byte[] { _ReceivedBuffer.ElementAt(2), _ReceivedBuffer.ElementAt(1) };
            int    DataLength       = BitConverter.ToInt16(_DataLengthBytes, 0);

            if (DataLength < 16)
            {
                lock (_lockBuffer)
                    _ReceivedBuffer.RemoveAt(0);
                return;
            }

            //If the data is not fully received, wait for next add
            if (_ReceivedBuffer.Count() < DataLength)
            {
                return;
            }


            //if the total length is received, make it as a block
            lock (_lockBuffer) {
                DataBlockBytes = _ReceivedBuffer.Skip(3).Take(DataLength - 3).ToArray();
                _ReceivedBuffer.RemoveRange(0, DataLength);
            }

            //Add data to Block
            MultiDataBlock   multiBlock = new MultiDataBlock();
            List <DataBlock> blocks     = multiBlock.Parse(DataBlockBytes);

            //Raise the event once block is received
            OnBlockReceive?.Invoke(this, blocks);
        }