Пример #1
0
        internal static LinkUpPacket ParseFromRaw(byte[] data, int startIndex, int size)
        {
            LinkUpPacket result = new LinkUpPacket();
            int          length = 0;

            try
            {
                int escaped = 0;

                length = BitConverter.ToInt32(RemoveEscaping(data, startIndex + 1, 4, ref escaped), 0);

                result.Data = RemoveEscaping(data, startIndex + 5 + escaped, length, ref escaped);

                ushort crc = BitConverter.ToUInt16(RemoveEscaping(data, startIndex + 5 + length + escaped, 2, ref escaped), 0);

                if (!(crc == 0 || crc == result.Crc) || data[startIndex] != Constant.Preamble || data[startIndex + size - 1] != Constant.EndOfPacket || length != size - (4 + 2 + 1 + 1 + escaped))
                {
                    result._IsValid = false;
                }
                else
                {
                    result._IsValid = true;
                }
            }
            catch (Exception ex)
            {
                result._IsValid = false;
            }

            return(result);
        }
Пример #2
0
 public void SendPacket(LinkUpPacket packet)
 {
     byte[] data = _Converter.ConvertToSend(packet);
     SendData(data);
     SentPacket?.Invoke(this, packet);
     _TotalSentPackets++;
     _TotalSentBytes += data.Length;
     _SentCounter.AddBytes(data.Length);
 }
Пример #3
0
 private void OnDataReceivedWorker()
 {
     while (_IsRunning)
     {
         try
         {
             LinkUpPacket packet = _BlockingCollection.Take(_CancellationTokenSource.Token);
             ReveivedPacket?.Invoke(this, packet);
         }
         catch (Exception) { }
     }
 }
Пример #4
0
        private List <LinkUpPacket> ParseBuffer()
        {
            List <LinkUpPacket> result = new List <LinkUpPacket>();
            int indexOfPreamble        = IndexOfInBuffer(0, Constant.Preamble);
            int indexOfEndOfPacket     = IndexOfInBuffer(0, Constant.EndOfPacket);

            if (indexOfPreamble != -1 && indexOfEndOfPacket != -1 && indexOfPreamble < indexOfEndOfPacket)
            {
                int indexOfPreambleNext = IndexOfInBuffer(indexOfPreamble + 1, Constant.Preamble);

                while (indexOfPreambleNext < indexOfEndOfPacket && indexOfPreambleNext != -1)
                {
                    indexOfPreamble    += _Buffer.Skip(indexOfPreamble + 1).Take(indexOfEndOfPacket - indexOfPreamble).ToList().IndexOf(Constant.Preamble) + 1;
                    indexOfPreambleNext = IndexOfInBuffer(indexOfPreamble + 1, Constant.Preamble);
                }

                LinkUpPacket packet = LinkUpPacket.ParseFromRaw(_Buffer, indexOfPreamble, indexOfEndOfPacket - indexOfPreamble + 1);
                if (packet.IsValid)
                {
                    result.Add(packet);
                    _TotalReceivedPackets++;
                }
                else
                {
                    _TotalFailedPackets++;
                }
            }

            if (indexOfEndOfPacket != -1)
            {
                //if (_Buffer.Length > indexOfEndOfPacket + 1)
                //{
                _BufferSize -= (indexOfEndOfPacket + 1);

                Array.Copy(_Buffer, indexOfEndOfPacket + 1, _Buffer, 0, _BufferSize);
                AddBufferEnd();
                //}
                //else
                //{
                //    _Buffer = new byte[0];
                //    _BufferSize = 0;
                //}
            }

            if (indexOfPreamble != -1 && indexOfEndOfPacket != -1)
            {
                result.AddRange(ParseBuffer());
            }

            return(result);
        }
Пример #5
0
 public byte[] ConvertToSend(LinkUpPacket packet)
 {
     return(packet.ToRaw());
 }