Esempio n. 1
0
        public static ToffeeInternalPacketReadResult Read(ToffeeParticipant receiver, byte[] packet)
        {
            try
            {
                // Iterate through the packet
                ToffeePacketIterator iterator = new ToffeePacketIterator(receiver, packet);

                // Get the header, data, and CRC
                InternalPacketHeader header = iterator.ReadStruct <InternalPacketHeader>();
                byte[] packetData           = iterator.ReadBytes(header.Length);
                uint   sentCrc = iterator.ReadUInt32();

                // Is the CRC correct?
                uint calculatedCrc = CRC.CalculateCRC32(packetData);
                if (sentCrc != calculatedCrc)
                {
                    return(new ToffeeInternalPacketReadResult(false, new InternalPacketHeader(), null));
                }

                // Is this packet compressed?
                if (header.Compressed)
                {
                    packetData = CompressionService.Decompress(packetData);
                }

                // Return the read result
                return(new ToffeeInternalPacketReadResult(true, header, packetData));
            }
            catch
            {
                return(new ToffeeInternalPacketReadResult(false, new InternalPacketHeader(), null));
            }
        }
Esempio n. 2
0
        public static ToffeeClientPacketReadResult Read(ToffeeParticipant receiver, byte[] packet)
        {
            try
            {
                // Is this packet encrypted?
                bool encrypted = packet[0] == 0x01;
                if (encrypted)
                {
                    // Is this client setup to use encryption?
                    if (receiver.UseEncryption)
                    {
                        packet = receiver.Encryption.Decrypt(packet.Skip(1).ToArray());
                    }
                }
                else
                {
                    packet = packet.Skip(1).ToArray();
                }

                // Iterate through the packet
                ToffeePacketIterator iterator = new ToffeePacketIterator(receiver, packet);

                // Get the header, data, and CRC
                ClientPacketHeader header     = iterator.ReadStruct <ClientPacketHeader>();
                byte[]             packetData = iterator.ReadBytes(header.Length);
                uint sentCrc = iterator.ReadUInt32();

                // Is the CRC correct?
                uint calculatedCrc = CRC.CalculateCRC32(packetData);
                if (sentCrc != calculatedCrc)
                {
                    return(new ToffeeClientPacketReadResult(false, encrypted, new ClientPacketHeader(), null));
                }

                // Is this packet compressed?
                if (header.Compressed)
                {
                    packetData = CompressionService.Decompress(packetData);
                }

                // Return the read result
                return(new ToffeeClientPacketReadResult(true, encrypted, header, packetData));
            }
            catch
            {
                return(new ToffeeClientPacketReadResult(false, false, new ClientPacketHeader(), null));
            }
        }