Beispiel #1
0
        private void StartEventListener(TcpClient tcpClient)
        {
            eventListenerThread = new Thread(delegate()
            {
                eventListenerStarted        = true;
                byte[] buffer               = new byte[tcpClient.ReceiveBufferSize];
                NetworkStream networkStream = tcpClient.GetStream();
                while (eventListenerStarted)
                {
                    try
                    {
                        while (!networkStream.DataAvailable)
                        {
                            Thread.Sleep(10);
                        }
                        BiliPackReader.IPack[] packs = BiliPackReader.ReadPack(networkStream);

                        foreach (BiliPackReader.IPack pack in packs)
                        {
                            switch (pack.PackType)
                            {
                            case BiliPackReader.PackTypes.Popularity:
                                PopularityRecieved?.Invoke(((BiliPackReader.PopularityPack)pack).Popularity.ToString());
                                break;

                            case BiliPackReader.PackTypes.Command:
                                JsonRecieved?.Invoke(((BiliPackReader.CommandPack)pack).Value);
                                break;

                            case BiliPackReader.PackTypes.Heartbeat:
                                ServerHeartbeatRecieved?.Invoke();
                                break;
                            }
                        }
                    }
                    catch (SocketException)
                    {
                        ConnectionFailed?.Invoke("数据读取失败");
                        Disconnect();
                    }
                    catch (IOException)
                    {
                        ConnectionFailed?.Invoke("数据读取失败");
                        Disconnect();
                    }
                }
            });
            eventListenerThread.Start();
        }
        private void StartEventListener()
        {
            EventListenerThread = new Thread(delegate()
            {
                IsEventListenerRunning = true;
                while (IsEventListenerRunning)
                {
                    try
                    {
                        BiliPackReader.IPack[] packs = PackReader.ReadPacksAsync();

                        List <Json.Value> jsons = new List <Json.Value>();
                        List <BiliLiveJsonParser.IItem> items = new List <BiliLiveJsonParser.IItem>();

                        foreach (BiliPackReader.IPack pack in packs)
                        {
                            switch (pack.PackType)
                            {
                            case BiliPackReader.PackTypes.Popularity:
                                PopularityRecieved?.Invoke(((BiliPackReader.PopularityPack)pack).Popularity);
                                break;

                            case BiliPackReader.PackTypes.Command:
                                Json.Value value = ((BiliPackReader.CommandPack)pack).Value;
                                jsons.Add(value);
                                BiliLiveJsonParser.IItem item = BiliLiveJsonParser.Parse(value);
                                if (item != null)
                                {
                                    items.Add(item);
                                }
                                break;

                            case BiliPackReader.PackTypes.Heartbeat:
                                ServerHeartbeatRecieved?.Invoke();
                                break;
                            }
                        }

                        if (jsons.Count > 0)
                        {
                            JsonsRecieved?.Invoke(jsons.ToArray());
                        }
                        if (items.Count > 0)
                        {
                            ItemsRecieved?.Invoke(items.ToArray());
                        }
                    }
                    catch (SocketException)
                    {
                        ConnectionFailed?.Invoke("数据读取失败");
                        Disconnect();
                    }
                    catch (IOException)
                    {
                        ConnectionFailed?.Invoke("数据读取失败");
                        Disconnect();
                    }
                }
            });
            EventListenerThread.Start();
        }