Ejemplo n.º 1
0
        public Packet(byte[] data, UInt16 command, Sender sender)
        {
            Data    = data;
            Command = command;
            Sender  = sender;
            CrCHandler crc = new CrCHandler();

            Crc       = crc.ComputeChecksumBytes(data);
            Size      = unchecked ((short)(data.Length + 14));
            TotalData = new byte[0];
            IsEmpty   = false;
        }
Ejemplo n.º 2
0
 public static Packet ParsePacket(byte[] data, int len)
 {
     try
     {
         var a = new Packet()
         {
             TotalData = data
         };
         var command    = (UInt16)HelpMethods.GetNumber(data[2], data[3]);
         var sender     = new Sender(SenderType.UnNamedByteMask);
         var sendertype = (SenderType)data[4];
         sender.Type = sendertype;
         Buffer.BlockCopy(data, 5, sender.Mask, 0, 7);
         if (sendertype == SenderType.SevenByteName)
         {
             sender.Name = Encoding.Default.GetString(sender.Mask);
         }
         a.Sender  = sender;
         a.Size    = (short)len;
         a.Command = command;
         a.Data    = new byte[len - 14];
         Buffer.BlockCopy(data, 12, a.Data, 0, len - 14);
         CrCHandler crc = new CrCHandler();
         a.Crc = new byte[2] {
             data[data.Length - 2], data[data.Length - 1]
         };
         var newCrc = crc.ComputeChecksumBytes(a.Data);
         if (a.Crc[0] != newCrc[0] || a.Crc[1] != newCrc[1])
         {
             throw new WrongPacketInputException(a, new Tuple <byte, byte>(newCrc[0], newCrc[1]), new Tuple <byte, byte>(a.Crc[0], a.Crc[1]));
         }
         return(a);
     }
     catch (Exception e)
     {
         throw new WrongPacketInputException(e);
     }
 }