public RtmpPacketReader(AmfReader reader)
        {
            this.reader = reader;
            this.rtmpHeaders = new Dictionary<int, RtmpHeader>();
            this.rtmpPackets = new Dictionary<int, RtmpPacket>();

            Continue = true;
        }
            public Amf3(SerializationContext context, AmfReader reader, Base b)
            {
                this.b       = b;
                this.reader  = reader;
                this.context = context;

                this.refObjects = new ReferenceList <object>();
                this.refStrings = new ReferenceList <string>();
                this.refClasses = new ReferenceList <ClassDescription>();
            }
        public static RtmpHandshake Read(Stream stream, bool readVersion)
        {
            var size = HandshakeSize + (readVersion ? 1 : 0);
            var buffer = StreamHelper.ReadBytes(stream, size);

            using (var reader = new AmfReader(new MemoryStream(buffer), null))
            {
                return new RtmpHandshake()
                {
                    Version = readVersion ? reader.ReadByte() : default(byte),
                    Time = reader.ReadUInt32(),
                    Time2 = reader.ReadUInt32(),
                    Random = reader.ReadBytes(HandshakeRandomSize)
                };
            }
        }
        private void UpdateReplays()
        {
            GamePanel.Children.Clear();

            var dir = new DirectoryInfo(Path.Combine(Client.ExecutingDirectory, "cabinet"));
            IOrderedEnumerable<DirectoryInfo> directories = dir.EnumerateDirectories()
                .OrderBy(d => d.CreationTime);

            string[] Replays = Directory.GetDirectories(Path.Combine(Client.ExecutingDirectory, "cabinet"));

            foreach (DirectoryInfo di in directories)
            {
                string d = di.Name;
                if (!File.Exists(Path.Combine(Client.ExecutingDirectory, "cabinet", d, "token")) ||
                    !File.Exists(Path.Combine(Client.ExecutingDirectory, "cabinet", d, "key")) ||
                    !File.Exists(Path.Combine(Client.ExecutingDirectory, "cabinet", d, "endOfGameStats")))
                    continue;

                byte[] base64Stats =
                    Convert.FromBase64String(
                        File.ReadAllText(Path.Combine(Client.ExecutingDirectory, "cabinet", d, "endOfGameStats")));
                var statsReader = new AmfReader(new MemoryStream(base64Stats), context);

                var stats = (EndOfReplayGameStats)statsReader.ReadAmf3Item();

                var item = new ReplayItem();

                //Use unoccupied variable
                stats.Difficulty = d;

                item.Tag = stats;
                item.GameId.Text = File.Exists(Path.Combine(Client.ExecutingDirectory, "cabinet", d, "name"))
                    ? File.ReadAllText(Path.Combine(Client.ExecutingDirectory, "cabinet", d, "name"))
                    : d;
                item.GameId.Tag = d;
                item.GameType.Content = stats.GameMode.ToLower();
                item.GameDate.Content = di.CreationTime.ToShortTimeString() + " " + di.CreationTime.ToShortDateString();
                double seconds = stats.GameLength % 60;
                double minutes = stats.GameLength / 60;
                item.GameTime.Content = string.Format("{0:0}:{1:00}", minutes, seconds);
                item.Margin = new Thickness(0, 5, 0, 0);

                foreach (
                    SmallChampionItem image in stats.TeamPlayerParticipantStats.Select(summary => new SmallChampionItem
                    {
                        Width = 38,
                        Height = 38,
                        ChampionImage =
                        {
                            Source = Client.GetImage(Path.Combine(Client.ExecutingDirectory, "Assets", "champion",
                                summary.SkinName + ".png"))
                        }
                    }))
                    item.TeamOnePanel.Children.Add(image);

                foreach (
                    SmallChampionItem image in
                        stats.OtherTeamPlayerParticipantStats.Select(summary => new SmallChampionItem
                        {
                            Width = 38,
                            Height = 38,
                            ChampionImage =
                            {
                                Source = Client.GetImage(Path.Combine(Client.ExecutingDirectory, "Assets", "champion",
                                    summary.SkinName + ".png"))
                            }
                        }))
                    item.TeamTwoPanel.Children.Add(image);

                item.MouseDown += item_MouseDown;
                item.GameId.MouseDoubleClick += GameId_MouseDoubleClick;
                item.GameId.MouseLeave += GameId_MouseLeave;
                item.KeyDown += item_KeyDown;

                //Insert on top
                GamePanel.Children.Insert(0, item);
            }
        }
        RtmpEvent ParsePacket(RtmpPacket packet, Func<AmfReader, RtmpEvent> handler)
        {
            var memoryStream = new MemoryStream(packet.Buffer, false);
            var packetReader = new AmfReader(memoryStream, reader.SerializationContext, reader.DeserializeToObjects, reader.DeserializeToDynamicWhenTypeNotFound);

            var header = packet.Header;
            var message = handler(packetReader);
            message.Header = header;
            message.Timestamp = header.Timestamp;
            return message;
        }
        static RtmpEvent ReadCommandOrData(AmfReader r, Command command)
        {
            var methodName = (string)r.ReadAmf0Item();

            command.InvokeId = Convert.ToInt32(r.ReadAmf0Item());
            command.ConnectionParameters = r.ReadAmf0Item();

            var parameters = new List<object>();
            while (r.DataAvailable)
                parameters.Add(r.ReadAmf0Item());

            command.MethodCall = new Method(methodName, parameters.ToArray());
            return command;
        }
        static int GetChunkStreamId(byte chunkBasicHeaderByte, AmfReader reader)
        {
            var chunkStreamId = chunkBasicHeaderByte & 0x3F;

            // 2 bytes
            if (chunkStreamId == 0)
                return reader.ReadByte() + 64;

            // 3 bytes
            if (chunkStreamId == 1)
                return reader.ReadByte() + reader.ReadByte() * 256 + 64;

            return chunkStreamId;
        }
        RtmpEvent ParsePacket(RtmpPacket packet, Func<AmfReader, RtmpEvent> handler)
        {
            var memoryStream = new MemoryStream(packet.Buffer, false);
            var packetReader = new AmfReader(memoryStream, reader.SerializationContext);

            var header = packet.Header;
            var message = handler(packetReader);
            message.Header = header;
            message.Timestamp = header.Timestamp;
            return message;
        }
        static int GetChunkStreamId(byte chunkBasicHeaderByte, AmfReader reader)
        {
            var chunkStreamId = chunkBasicHeaderByte & 0x3F;
            switch (chunkStreamId)
            {
                // 2 bytes
                case 0:
                    return reader.ReadByte() + 64;

                // 3 bytes
                case 1:
                    return reader.ReadByte() + reader.ReadByte() * 256 + 64;

                // 1 byte
                default:
                    return chunkStreamId;
            }
        }
        void UpdateReplays()
        {
            GamePanel.Children.Clear();

            var dir = new DirectoryInfo("cabinet");
            var directories = dir.EnumerateDirectories()
                                .OrderBy(d => d.CreationTime);

            string[] Replays = Directory.GetDirectories("cabinet");

            foreach (DirectoryInfo di in directories)
            {
                string d = di.Name;
                if (!File.Exists(Path.Combine("cabinet", d, "token")) ||
                    !File.Exists(Path.Combine("cabinet", d, "key")) ||
                    !File.Exists(Path.Combine("cabinet", d, "endOfGameStats")))
                    continue;

                byte[] Base64Stats = Convert.FromBase64String(File.ReadAllText(Path.Combine("cabinet", d, "endOfGameStats")));
                AmfReader statsReader = new AmfReader(new MemoryStream(Base64Stats), context);

                EndOfGameStats stats = (EndOfGameStats)statsReader.ReadAmf3Item();

                ReplayItem item = new ReplayItem();

                //Use unoccupied variable
                stats.Difficulty = d;

                item.Tag = stats;
                item.GameId.Content = d;
                item.GameType.Content = stats.GameMode.ToLower();
                item.GameDate.Content = di.CreationTime.ToShortTimeString() + " " + di.CreationTime.ToShortDateString();
                double seconds = stats.GameLength % 60;
                double minutes = stats.GameLength / 60;
                item.GameTime.Content = string.Format("{0:0}:{1:00}", minutes, seconds);
                item.Margin = new Thickness(0, 5, 0, 0);

                foreach (PlayerParticipantStatsSummary summary in stats.TeamPlayerParticipantStats)
                {
                    SmallChampionItem image = new SmallChampionItem();
                    image.Width = 38;
                    image.Height = 38;

                    Uri UriSource = new Uri("/LegendaryClient;component/Assets/champion/" + summary.SkinName + ".png", UriKind.RelativeOrAbsolute);
                    image.ChampionImage.Source = new BitmapImage(UriSource);

                    item.TeamOnePanel.Children.Add(image);
                }

                foreach (PlayerParticipantStatsSummary summary in stats.OtherTeamPlayerParticipantStats)
                {
                    SmallChampionItem image = new SmallChampionItem();
                    image.Width = 38;
                    image.Height = 38;

                    Uri UriSource = new Uri("/LegendaryClient;component/Assets/champion/" + summary.SkinName + ".png", UriKind.RelativeOrAbsolute);
                    image.ChampionImage.Source = new BitmapImage(UriSource);

                    item.TeamTwoPanel.Children.Add(image);
                }

                item.MouseDown += item_MouseDown;

                //Insert on top
                GamePanel.Children.Insert(0, item);
            }
        }