Exemple #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));
            }
        }
Exemple #2
0
        public static ToffeeInternalPacket Create <T>(ToffeeParticipant sender, ushort opCode, T o)
        {
            ToffeeInternalPacket packet = new ToffeeInternalPacket(sender, opCode);

            packet.WriteStruct(o);
            return(packet);
        }
Exemple #3
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));
            }
        }
Exemple #4
0
        public static ToffeeInternalPacketReadResult Read <T>(ToffeeParticipant receiver, byte[] packet, out T output) where T : new()
        {
            // Default the output
            output = new T();

            // Read the packet
            ToffeeInternalPacketReadResult result = Read(receiver, packet);

            if (result.Success)
            {
                // Create a new iterator for the decompressed/decrypted packet data
                ToffeePacketIterator iterator = new ToffeePacketIterator(receiver, result.Data);
                output = (T)iterator.Read(typeof(T));
            }

            // Return the read result
            return(result);
        }
Exemple #5
0
 public ToffeeInternalPacket(ToffeeParticipant client, ToffeeOpCode opCode) : base(client)
 {
     OpCode = (ushort)opCode;
 }
Exemple #6
0
 public ToffeeInternalPacket(ToffeeParticipant client, ushort opCode) : base(client)
 {
     OpCode = opCode;
 }