Exemple #1
0
        public bool TryGetPacket(out Message p)
        {
            p = null;
            bool result = false;

            if (DataStream.Count >= 5)
            {
                int    length = (DataStream[2] << 16) | (DataStream[3] << 8) | DataStream[4];
                ushort type   = (ushort)((DataStream[0] << 8) | DataStream[1]);
                if (DataStream.Count - 7 >= length)
                {
                    object obj;
                    byte[] packet = DataStream.Take(7 + length).ToArray();
                    using (PacketReader br = new PacketReader(new MemoryStream(packet)))
                        obj = MessageFactory.Read(this, br, type);
                    if (obj != null)
                    {
                        p      = (Message)obj;
                        result = true;
                    }
                    else
                    {
#if DEBUG
                        if (!Directory.Exists("unhandled-message-dumps"))
                        {
                            Directory.CreateDirectory("unhandled-message-dumps");
                        }

                        var time     = DateTime.Now;
                        var fileName = $"{time.ToString("yy_MM_dd__hh_mm_ss")} - ({length})({type}).bin";
                        var path     = Path.Combine("unhandled-message-dumps", fileName);
                        File.WriteAllBytes(path, packet);
#endif
                        if (Constants.IsRc4)
                        {
                            //Update Decryption Key
                            Decrypt(DataStream.Skip(7).Take(length).ToArray());
                        }
                        else
                        {
                            CSNonce.Increment();
                        }
                    }
                    DataStream.RemoveRange(0, 7 + length);
                }
            }
            return(result);
        }
        public bool TryGetPacket(out Message _Message)
        {
            _Message = null;
            bool result = false;

            if (DataStream.Count >= 5)
            {
                int    length = (DataStream[2] << 16) | (DataStream[3] << 8) | DataStream[4];
                ushort type   = (ushort)((DataStream[0] << 8) | DataStream[1]);
                if (DataStream.Count - 7 >= length)
                {
                    object obj;
                    byte[] packet = DataStream.Take(7 + length).ToArray();
                    using (PacketReader br = new PacketReader(new MemoryStream(packet)))
                    {
                        obj = MessageFactory.Read(this, br, type);
                    }
                    if (obj != null)
                    {
                        _Message = (Message)obj;
                        result   = true;
                    }
                    else
                    {
                        if (Constants.IsRc4)
                        {
                            Decrypt(DataStream.Skip(7).Take(length).ToArray());
                        }
                        else
                        {
                            CSNonce.Increment();
                        }
                    }
                    DataStream.RemoveRange(0, 7 + length);
                }
                else
                {
                    DataStream.RemoveRange(0, 7 + length);
                    this.Socket.Close();
                }
            }
            return(result);
        }
Exemple #3
0
        public bool TryGetPacket(out Message message)
        {
            const int HEADER_LEN = 7;

            message = default(Message);

            var result = false;

            if (DataStream.Count >= 5)
            {
                // Get packet length and type but ignore packet version.
                int    length = (DataStream[2] << 16) | (DataStream[3] << 8) | DataStream[4];
                ushort type   = (ushort)((DataStream[0] << 8) | DataStream[1]);

                if (DataStream.Count - HEADER_LEN >= length)
                {
                    // Avoid LINQ cause we can.
                    var packet = new byte[length];
                    for (int i = 0; i < packet.Length; i++)
                    {
                        packet[i] = DataStream[i + HEADER_LEN];
                    }

                    message = (Message)MessageFactory.Read(this, new PacketReader(packet), type);
                    if (message != null)
                    {
                        message.SetData(packet);
                        message.SetMessageType(type); // Just in case they don't do it in the constructor.

                        try { message.Decrypt(); }
                        catch (Exception ex)
                        {
                            ExceptionLogger.Log(ex, $"Unable to decrypt message with ID: {type}");
                        }

                        try { message.Decode(); }
                        catch (Exception ex)
                        {
                            ExceptionLogger.Log(ex, $"Unable to decode message with ID: {type}");
                        }
                        result = true;
                    }
                    else
                    {
                        Logger.Say("Unhandled message " + type);

                        // Make sure we don't break the RC4 stream.
                        if (Constants.IsRc4)
                        {
                            Decrypt(packet);
                        }
                        else
                        {
                            CSNonce.Increment();
                        }
                    }

                    // Clean up.
                    DataStream.RemoveRange(0, HEADER_LEN + length);
                }
            }
            return(result);
        }