示例#1
0
        private async Task ReceiveMessageLoop()
        {
            try
            {
                var stableBuffer = new byte[16];
                var buffer       = new byte[4096];
                while (this.Connected)
                {
                    await NetStream.ReadBAsync(stableBuffer, 0, 16);

                    var protocol = DanmakuProtocol.FromBuffer(stableBuffer);
                    if (protocol.PacketLength < 16)
                    {
                        throw new NotSupportedException("协议失败: (L:" + protocol.PacketLength + ")");
                    }
                    var payloadlength = protocol.PacketLength - 16;
                    if (payloadlength == 0)
                    {
                        continue;
                    }
                    buffer = new byte[payloadlength];
                    await NetStream.ReadBAsync(buffer, 0, payloadlength);

                    if (protocol.Version == 2 && protocol.Action == 5)
                    {
                        using (var ms = new MemoryStream(buffer, 2, payloadlength - 2))
                            using (var deflate = new DeflateStream(ms, CompressionMode.Decompress))
                            {
                                var headerbuffer = new byte[16];
                                try
                                {
                                    while (true)
                                    {
                                        await deflate.ReadBAsync(headerbuffer, 0, 16);

                                        var protocol_in = DanmakuProtocol.FromBuffer(headerbuffer);
                                        payloadlength = protocol_in.PacketLength - 16;
                                        var danmakubuffer = new byte[payloadlength];
                                        await deflate.ReadBAsync(danmakubuffer, 0, payloadlength);

                                        ProcessDanmaku(protocol.Action, danmakubuffer);
                                    }
                                }
                                catch { }
                            }
                    }
                    else
                    {
                        ProcessDanmaku(protocol.Action, buffer);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("ReceiveMessageLoop", ex);
                Disconnect();
            }
        }
示例#2
0
        private async Task ReceiveMessageLoop()
        {
            try
            {
                var stableBuffer = new byte[16];
                var buffer       = new byte[4096];
                while (this.Connected)
                {
                    await NetStream.ReadBAsync(stableBuffer, 0, 16);

                    var protocol = DanmakuProtocol.FromBuffer(stableBuffer);
                    if (protocol.PacketLength < 16)
                    {
                        throw new NotSupportedException("协议失败: (L:" + protocol.PacketLength + ")");
                    }
                    var payloadlength = protocol.PacketLength - 16;
                    if (payloadlength == 0)
                    {
                        continue; // 没有内容了
                    }

                    buffer = new byte[payloadlength];

                    await NetStream.ReadBAsync(buffer, 0, payloadlength);

                    if (protocol.Version == 2 && protocol.Action == 5)                  // 处理deflate消息
                    {
                        using (var ms = new MemoryStream(buffer, 2, payloadlength - 2)) // Skip 0x78 0xDA
                            using (var deflate = new DeflateStream(ms, CompressionMode.Decompress))
                            {
                                var headerbuffer = new byte[16];
                                try
                                {
                                    while (true)
                                    {
                                        await deflate.ReadBAsync(headerbuffer, 0, 16);

                                        var protocol_in = DanmakuProtocol.FromBuffer(headerbuffer);
                                        payloadlength = protocol_in.PacketLength - 16;
                                        var danmakubuffer = new byte[payloadlength];
                                        await deflate.ReadBAsync(danmakubuffer, 0, payloadlength);

                                        ProcessDanmaku(protocol.Action, danmakubuffer);
                                    }
                                }
                                catch (Exception e)
                                {
                                }
                            }
                    }
                    else if (protocol.Version == 3 && protocol.Action == 5) // brotli?
                    {
                        using (var ms = new MemoryStream(buffer))           // Skip 0x78 0xDA
                            using (var deflate = new Brotli.BrotliStream(ms, CompressionMode.Decompress))
                            {
                                var headerbuffer = new byte[16];
                                try
                                {
                                    while (true)
                                    {
                                        await deflate.ReadBAsync(headerbuffer, 0, 16);

                                        var protocol_in = DanmakuProtocol.FromBuffer(headerbuffer);
                                        payloadlength = protocol_in.PacketLength - 16;
                                        var danmakubuffer = new byte[payloadlength];
                                        await deflate.ReadBAsync(danmakubuffer, 0, payloadlength);

                                        ProcessDanmaku(protocol.Action, danmakubuffer);
                                    }
                                }
                                catch (Exception e)
                                {
                                }
                            }
                    }
                    else
                    {
                        ProcessDanmaku(protocol.Action, buffer);
                    }
                }
            }
            //catch (NotSupportedException ex)
            //{
            //    this.Error = ex;
            //    _disconnect();
            //}
            catch (Exception ex)
            {
                this.Error = ex;
                _disconnect();
            }
        }
示例#3
0
        /// <summary>
        /// 循环接受信息
        /// </summary>
        /// <returns></returns>
        private async Task ReceiveMessageLoop()
        {
            try
            {
                var stableBuffer = new byte[16];
                var buffer       = new byte[4096];
                while (this._mConnected)
                {
                    await _mNetStream.ReadBAsync(stableBuffer, 0, 16);

                    var protocol = DanmakuProtocolStruts.FromBuffer(stableBuffer);
                    if (protocol.PacketLength < 16)
                    {
                        throw new NotSupportedException("协议失败: (L:" + protocol.PacketLength + ")");
                    }
                    var payloadlength = protocol.PacketLength - 16;
                    if (payloadlength == 0)
                    {
                        continue; // 没有内容了
                    }

                    buffer = new byte[payloadlength];

                    await _mNetStream.ReadBAsync(buffer, 0, payloadlength);

                    if (protocol.Version == 2 && protocol.OpearateCode == OperateCode.表示具体命令Cmd) // 处理deflate消息
                    {
                        using (var ms = new MemoryStream(buffer, 2, payloadlength - 2))          // Skip 0x78 0xDA
                            using (var deflate = new DeflateStream(ms, CompressionMode.Decompress))
                            {
                                var headerbuffer = new byte[16];
                                try
                                {
                                    while (true)
                                    {
                                        await deflate.ReadBAsync(headerbuffer, 0, 16);

                                        var protocol_in = DanmakuProtocolStruts.FromBuffer(headerbuffer);
                                        payloadlength = protocol_in.PacketLength - 16;
                                        var danmakubuffer = new byte[payloadlength];
                                        await deflate.ReadBAsync(danmakubuffer, 0, payloadlength);

                                        HandleMsg(protocol.OpearateCode, danmakubuffer);
                                    }
                                }
                                catch (Exception)
                                {
                                }
                            }
                    }
                    else
                    {
                        HandleMsg(protocol.OpearateCode, buffer);
                    }
                }
            }
            catch (Exception ex)
            {
                Disconnect();
                Log?.Invoke(ex.StackTrace);
            }
        }