Esempio n. 1
0
 internal void ProcessPacket(SerialHeader sh, byte[] buffer, int offset)
 {
     if (sh is SafeSerialHeader)
     {
         if (sh.length > 0)
         {
             headerACK.CRC      = 0;
             headerACK.sequence = ((SafeSerialHeader)sh).sequence;
             headerACK.CRC      = SerialCRC.Calculate(headerACK.GetBytes(), headerACK.size);
             WriteToPort(headerACK.GetBytes(), 0, headerACK.size, true);
         }
         else
         {
             byte seq = ((SafeSerialHeader)sh).sequence;
             lock (SendBuffer)
             {
                 if (SendBuffer.ContainsKey(seq))
                 {
                     SendBuffer.Remove(seq);
                 }
             }
         }
     }
     if (sh.length > 0)
     {
         coder.DataReceived(buffer, offset, sh.length, sta);
     }
 }
Esempio n. 2
0
        public virtual void Send(TransportAddress t, byte[] data)
        {
            byte[]       tmp;
            SerialHeader header;

            if (data.Length > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException("there are too many bytes to transmit");
            }

            // Check if this message is going to use Acknowledgements
            if (((SerialTransportAddress)t).forceACK)
            {
                header = new SafeSerialHeader();
            }
            else
            {
                header = new SerialHeader();
            }

            header.length = (ushort)data.Length;
            if (header is SafeSerialHeader)
            {
                ((SafeSerialHeader)header).sequence = GetSequence();
            }
            tmp = new byte[header.size + data.Length];
            header.GetBytes().CopyTo(tmp, 0);
            data.CopyTo(tmp, header.size);

            header.CRC = SerialCRC.Calculate(tmp, (uint)tmp.Length);
            header.GetBytes().CopyTo(tmp, 0);

            if (header is SafeSerialHeader)
            {
                lock (SendBuffer)
                {
                    SendBuffer.Add(sequence, new SafeData(tmp));
                }
            }
            try
            {
                WriteToPort(tmp, 0, tmp.Length, header is SafeSerialHeader);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 3
0
        public virtual void Receive()
        {
            int n;

            byte[]       buffer = new byte[ushort.MaxValue]; // Max UDP Datagram
            SerialHeader sh;

            while (!end)
            {
                try
                {
                    // Receive magic byte
                    n = 0;
                    do
                    {
                        n += port.Read(buffer, 0, 1);
                    } while (n < 1);
                    if (buffer[0] == (byte)MagicByte.SAFE)
                    {
                        sh = new SafeSerialHeader();
                    }
                    else if (buffer[0] == (byte)MagicByte.FAST)
                    {
                        sh = new SerialHeader();
                    }
                    else
                    {
                        continue;
                    }
                    // Receive the header
                    do
                    {
                        n += port.Read(buffer, n, sh.size - n);
                    } while (n < sh.size);
                    if (!sh.Populate(buffer, 1))
                    {
                        continue;
                    }
                    // Receive and process the data
                    n = 0;
                    do
                    {
                        n += port.Read(buffer, sh.size + n, sh.length - n);
                    } while (n < sh.length);

                    if (sh.CRC == SerialCRC.Calculate(buffer, (uint)(sh.length + sh.size)))
                    {
                        ProcessPacket(sh, buffer, sh.size);
                    }
                    else
                    {
                    }
                }
                catch (ThreadAbortException)
                {
                    //It's safe to ignore this :)
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }