Esempio n. 1
0
        public void MainReceivedDanMaku(ReceivedDanmakuArgs e)
        {
            try
            {
                ReceivedDanmaku?.Invoke(null, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "插件" + PluginName + "遇到了不明錯誤: 日誌已經保存在桌面, 請有空發給該插件作者 " + PluginAuth + ", 聯繫方式 " + PluginCont);
                try
                {
                    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);


                    using (StreamWriter outfile = new StreamWriter(path + @"\B站彈幕姬插件" + PluginName + "錯誤報告.txt"))
                    {
                        outfile.WriteLine("請有空發給聯繫方式 " + PluginCont + " 謝謝");
                        outfile.WriteLine(PluginName + " " + PluginVer);
                        outfile.Write(ex.ToString());
                    }
                }
                catch (Exception)
                {
                }
            }
        }
Esempio n. 2
0
        private void ProcessDanmu(int opt, byte[] buffer, int length)
        {
            switch (opt)
            {
            case 5:
            {
                var json = string.Empty;
                try
                {
                    json = Encoding.UTF8.GetString(buffer, 0, length);
                    ReceivedDanmaku?.Invoke(this, new ReceivedDanmakuArgs {
                            Danmaku = new Danmaku(json)
                        });
                }
                catch (Exception ex)
                {
                    if (ex is JsonException || ex is KeyNotFoundException)
                    {
                        LogEvent?.Invoke(this, new LogEventArgs {
                                Log = $@"[{_roomId}] 弹幕识别错误 {json}"
                            });
                    }
                    else
                    {
                        LogEvent?.Invoke(this, new LogEventArgs {
                                Log = $@"[{_roomId}] {ex}"
                            });
                    }
                }

                break;
            }
            }
        }
Esempio n. 3
0
        public void MainReceivedDanMaku(ReceivedDanmakuArgs e)
        {
            try
            {
                ReceivedDanmaku?.Invoke(null, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "插件" + PluginName + "遇到了不明错误: 日志已经保存在桌面, 请有空发给该插件作者 " + PluginAuth + ", 联系方式 " + PluginCont);
                try
                {
                    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);


                    using (StreamWriter outfile = new StreamWriter(path + @"\B站弹幕姬插件" + PluginName + "错误报告.txt"))
                    {
                        outfile.WriteLine("请有空发给联系方式 " + PluginCont + " 谢谢");
                        outfile.WriteLine(PluginName + " " + PluginVer);
                        outfile.Write(ex.ToString());
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        private void ProcessDanmaku(int action, byte[] buffer)
        {
            switch (action)
            {
            case 3:
            {
                var viewer = EndianBitConverter.BigEndian.ToUInt32(buffer, 0);
                ReceivedUserCount?.Invoke(this, new SocketReceivedUserCountArgs()
                    {
                        RoomID    = RoomID,
                        UserCount = viewer
                    });
                break;
            }

            case 5:
            {
                var json = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                if (IsDebug)
                {
                    LogMessage?.Invoke(this, new SocketLogMessageArgs
                        {
                            RoomID  = RoomID,
                            Message = json
                        });
                }
                try
                {
                    var dama = new BilibiliLiveDanmaku_SocketReceiveData(json, 2);
                    ReceivedDanmaku?.Invoke(this, new SocketReceivedDanmakuArgs()
                        {
                            RoomID  = RoomID,
                            Danmaku = dama
                        });
                }
                catch
                {
                }
                break;
            }

            case 8:
            {
                break;
            }

            default:
            {
                break;
            }
            }
        }
        private void ProcessDanmaku(int action, byte[] buffer)
        {
            switch (action)
            {
            case 3:                                                            // (OpHeartbeatReply)
            {
                var viewer = EndianBitConverter.BigEndian.ToUInt32(buffer, 0); //观众人数
                // Console.WriteLine(viewer);
                ReceivedRoomCount?.Invoke(this, new ReceivedRoomCountArgs()
                    {
                        UserCount = viewer
                    });
                break;
            }

            case 5:    //playerCommand (OpSendMsgReply)
            {
                var json = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                if (debuglog)
                {
                    Console.WriteLine(json);
                }
                try
                {
                    var dama = new DanmakuModel(json, 2);
                    ReceivedDanmaku?.Invoke(this, new ReceivedDanmakuArgs()
                        {
                            Danmaku = dama
                        });
                }
                catch (Exception)
                {
                    // ignored
                }
                break;
            }

            case 8:     // (OpAuthReply)
            {
                break;
            }

            default:
            {
                break;
            }
            }
        }
Esempio n. 6
0
        private void ReceiveMessageLoop()
        {
            //            lock (shit_lock)
            //            //ReceiveMessageLoop 似乎好像大概會同時運行兩個的bug, 但是不修了, 鎖上算了
            //            {
            try
            {
                var stableBuffer = new byte[Client.ReceiveBufferSize];

                while (this.Connected)
                {
                    NetStream.ReadB(stableBuffer, 0, 4);
                    var packetlength = BitConverter.ToInt32(stableBuffer, 0);
                    packetlength = IPAddress.NetworkToHostOrder(packetlength);

                    if (packetlength < 16)
                    {
                        throw new NotSupportedException("Failed: (L:" + packetlength + ")");
                    }

                    NetStream.ReadB(stableBuffer, 0, 2); //magic
                    NetStream.ReadB(stableBuffer, 0, 2); //protocol_version

                    NetStream.ReadB(stableBuffer, 0, 4);
                    var typeId = BitConverter.ToInt32(stableBuffer, 0);
                    typeId = IPAddress.NetworkToHostOrder(typeId);

                    //Console.WriteLine(typeId);
                    NetStream.ReadB(stableBuffer, 0, 4);//magic, params?
                    var playloadlength = packetlength - 16;
                    if (playloadlength == 0)
                    {
                        continue;        //没有内容了
                    }
                    typeId = typeId - 1; //和反编译的代码对应
                    var buffer = new byte[playloadlength];
                    NetStream.ReadB(buffer, 0, playloadlength);
                    switch (typeId)
                    {
                    case 0:
                    case 1:
                    case 2:
                    {
                        var viewer = BitConverter.ToUInt32(buffer.Take(4).Reverse().ToArray(), 0);     //观众人数
                                                                                                       //Console.WriteLine(viewer);
                        ReceivedRoomCount?.Invoke(this, new ReceivedRoomCountArgs()
                            {
                                UserCount = viewer
                            });
                        break;
                    }

                    case 3:
                    case 4:    //playerCommand
                    {
                        var json = Encoding.UTF8.GetString(buffer, 0, playloadlength);
                        try
                        {
                            DanmakuModel dama = new DanmakuModel(json, 2);
                            ReceivedDanmaku?.Invoke(this, new ReceivedDanmakuArgs()
                                {
                                    Danmaku = dama
                                });
                        }
                        catch (Exception)
                        {
                            // ignored
                        }

                        break;
                    }

                    case 5:    //newScrollMessage
                    {
                        break;
                    }

                    case 7:
                    {
                        break;
                    }

                    case 16:
                    {
                        break;
                    }

                    default:
                    {
                        break;
                    }
                        //
                    }
                }
            }
            catch (NotSupportedException ex)
            {
                this.Error = ex;
                _disconnect();
            }
            catch (Exception ex)
            {
                this.Error = ex;
                _disconnect();
            }
            //            }
        }
Esempio n. 7
0
        private void ReceiveMessageLoop()
        {
            logger.Log(Roomid, LogLevel.Trace, "ReceiveMessageLoop Started");
            try
            {
                var stableBuffer = new byte[16];
                var buffer       = new byte[4096];
                while (dmTcpConnected)
                {
                    dmNetStream.ReadB(stableBuffer, 0, 16);
                    Parse2Protocol(stableBuffer, out DanmakuProtocol protocol);

                    if (protocol.PacketLength < 16)
                    {
                        throw new NotSupportedException("协议失败: (L:" + protocol.PacketLength + ")");
                    }

                    var payloadlength = protocol.PacketLength - 16;
                    if (payloadlength == 0)
                    {
                        continue;//没有内容了
                    }

                    if (buffer.Length < payloadlength) // 不够长再申请
                    {
                        buffer = new byte[payloadlength];
                    }

                    dmNetStream.ReadB(buffer, 0, payloadlength);

                    if (protocol.Version == 2 && protocol.Action == 5) // 处理deflate消息
                    {
                        // Skip 0x78 0xDA
                        using (DeflateStream deflate = new DeflateStream(new MemoryStream(buffer, 2, payloadlength - 2), CompressionMode.Decompress))
                        {
                            while (deflate.Read(stableBuffer, 0, 16) > 0)
                            {
                                Parse2Protocol(stableBuffer, out protocol);
                                payloadlength = protocol.PacketLength - 16;
                                if (payloadlength == 0)
                                {
                                    continue;                      // 没有内容了
                                }
                                if (buffer.Length < payloadlength) // 不够长再申请
                                {
                                    buffer = new byte[payloadlength];
                                }
                                deflate.Read(buffer, 0, payloadlength);
                                ProcessDanmaku(protocol.Action, buffer, payloadlength);
                            }
                        }
                    }
                    else
                    {
                        ProcessDanmaku(protocol.Action, buffer, payloadlength);
                    }

                    void ProcessDanmaku(int action, byte[] local_buffer, int length)
                    {
                        switch (action)
                        {
                        case 3:
                            // var viewer = BitConverter.ToUInt32(local_buffer.Take(4).Reverse().ToArray(), 0); //观众人数
                            break;

                        case 5:    //playerCommand
                            var json = Encoding.UTF8.GetString(local_buffer, 0, length);
                            try
                            {
                                ReceivedDanmaku?.Invoke(this, new ReceivedDanmakuArgs()
                                {
                                    Danmaku = new DanmakuModel(json)
                                });
                            }
                            catch (Exception ex)
                            {
                                logger.Log(Roomid, LogLevel.Warn, "", ex);
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                dmError = ex;
                // logger.Error(ex);

                logger.Log(Roomid, LogLevel.Debug, "Disconnected");
                dmClient?.Close();
                dmNetStream = null;
                if (!(dmTokenSource?.IsCancellationRequested ?? true))
                {
                    logger.Log(Roomid, LogLevel.Warn, "弹幕连接被断开,将尝试重连", ex);
                    _ = ConnectWithRetryAsync();
                }
            }
        }
Esempio n. 8
0
        private void ReceiveMessageLoop()
        {
            try
            {
                var stableBuffer = new byte[_client.ReceiveBufferSize];
                while (DmTcpConnected)
                {
                    _dmNetStream.ReadB(stableBuffer, 0, 4);
                    var packetLength = BitConverter.ToInt32(stableBuffer, 0);
                    packetLength = IPAddress.NetworkToHostOrder(packetLength);

                    if (packetLength < 16)
                    {
                        throw new NotSupportedException($@"协议失败: (L:{packetLength})");
                    }

                    _dmNetStream.ReadB(stableBuffer, 0, 2);                    //magic
                    _dmNetStream.ReadB(stableBuffer, 0, 2);                    //protocol_version
                    _dmNetStream.ReadB(stableBuffer, 0, 4);
                    var typeId = BitConverter.ToInt32(stableBuffer, 0);
                    typeId = IPAddress.NetworkToHostOrder(typeId);

                    _dmNetStream.ReadB(stableBuffer, 0, 4);                    //magic, params?
                    var playLoadLength = packetLength - 16;
                    if (playLoadLength == 0)
                    {
                        continue;                        //没有内容了
                    }

                    typeId -= 1;                    //和反编译的代码对应
                    var buffer = new byte[playLoadLength];
                    _dmNetStream.ReadB(buffer, 0, playLoadLength);
                    switch (typeId)
                    {
                    case 0:
                    case 1:
                    case 2:
                    {
                        var unused = BitConverter.ToUInt32(buffer.Take(4).Reverse().ToArray(), 0);                                 //观众人数
                        break;
                    }

                    case 3:
                    case 4:                            //playerCommand
                    {
                        var json = Encoding.UTF8.GetString(buffer, 0, playLoadLength);
                        try
                        {
                            ReceivedDanmaku?.Invoke(this, new ReceivedDanmakuArgs {
                                    Danmaku = new Danmaku(json)
                                });
                        }
                        catch (KeyNotFoundException)
                        {
                            LogEvent?.Invoke(this, new LogEventArgs {
                                    Log = $@"[{_roomId}] 弹幕识别错误 {json}"
                                });
                        }
                        catch (Exception ex)
                        {
                            LogEvent?.Invoke(this, new LogEventArgs {
                                    Log = $@"[{_roomId}] {ex}"
                                });
                        }
                        break;
                    }
                    }
                }
            }
            catch
            {
                _client?.Close();
                _dmNetStream = null;
                if (!(_cts?.IsCancellationRequested ?? true))
                {
                    LogEvent?.Invoke(this, new LogEventArgs {
                        Log = $@"[{_roomId}] 弹幕连接被断开,将尝试重连"
                    });
                    ConnectWithRetry();
                }
            }
        }
Esempio n. 9
0
        private void ReceiveMessageLoop()
        {
            logger.Log(Roomid, LogLevel.Trace, "ReceiveMessageLoop Started");
            try
            {
                var stableBuffer = new byte[dmClient.ReceiveBufferSize];
                while (dmTcpConnected)
                {
                    dmNetStream.ReadB(stableBuffer, 0, 4);
                    var packetlength = BitConverter.ToInt32(stableBuffer, 0);
                    packetlength = IPAddress.NetworkToHostOrder(packetlength);

                    if (packetlength < 16)
                    {
                        throw new NotSupportedException("协议失败: (L:" + packetlength + ")");
                    }

                    dmNetStream.ReadB(stableBuffer, 0, 2); //magic
                    dmNetStream.ReadB(stableBuffer, 0, 2); //protocol_version
                    dmNetStream.ReadB(stableBuffer, 0, 4);
                    var typeId = BitConverter.ToInt32(stableBuffer, 0);
                    typeId = IPAddress.NetworkToHostOrder(typeId);

                    dmNetStream.ReadB(stableBuffer, 0, 4);//magic, params?
                    var playloadlength = packetlength - 16;
                    if (playloadlength == 0)
                    {
                        continue;//没有内容了
                    }

                    typeId = typeId - 1;//和反编译的代码对应
                    var buffer = new byte[playloadlength];
                    dmNetStream.ReadB(buffer, 0, playloadlength);
                    switch (typeId)
                    {
                    case 0:
                    case 1:
                    case 2:
                    {
                        var viewer = BitConverter.ToUInt32(buffer.Take(4).Reverse().ToArray(), 0);         //观众人数
                        break;
                    }

                    case 3:
                    case 4:    //playerCommand
                    {
                        var json = Encoding.UTF8.GetString(buffer, 0, playloadlength);
                        try
                        {
                            ReceivedDanmaku?.Invoke(this, new ReceivedDanmakuArgs()
                                {
                                    Danmaku = new DanmakuModel(json)
                                });
                        }
                        catch (Exception ex)
                        {
                            logger.Log(Roomid, LogLevel.Warn, "", ex);
                        }
                        break;
                    }

                    case 5:    //newScrollMessage
                    case 7:
                    case 16:
                    default:
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                dmError = ex;
                // logger.Error(ex);

                logger.Log(Roomid, LogLevel.Debug, "Disconnected");
                dmClient?.Close();
                dmNetStream = null;
                if (!(dmTokenSource?.IsCancellationRequested ?? true))
                {
                    logger.Log(Roomid, LogLevel.Warn, "弹幕连接被断开,将尝试重连", ex);
                    ConnectWithRetry();
                }
            }
        }
        private void ReceiveMessageLoop()
        {
            try
            {
                var stableBuffer = new byte[Client.ReceiveBufferSize];

                while (IsConnected)
                {
                    NetStream.ReadB(stableBuffer, 0, 4);
                    var packetlength = BitConverter.ToInt32(stableBuffer, 0);
                    packetlength = IPAddress.NetworkToHostOrder(packetlength);
                    if (packetlength < 16)
                    {
                        throw new NotSupportedException("协议失败: (L:" + packetlength + ")");
                    }
                    NetStream.ReadB(stableBuffer, 0, 2);
                    NetStream.ReadB(stableBuffer, 0, 2);
                    NetStream.ReadB(stableBuffer, 0, 4);
                    var typeId = BitConverter.ToInt32(stableBuffer, 0);
                    typeId = IPAddress.NetworkToHostOrder(typeId);
                    Console.WriteLine(typeId);
                    NetStream.ReadB(stableBuffer, 0, 4);
                    var playloadlength = packetlength - 16;
                    if (playloadlength == 0)
                    {
                        continue;
                    }
                    typeId = typeId - 1;
                    var buffer = new byte[playloadlength];
                    NetStream.ReadB(buffer, 0, playloadlength);
                    switch (typeId)
                    {
                    case 0:
                    case 1:
                    case 2:
                    {
                        var viewer = BitConverter.ToUInt32(buffer.Take(4).Reverse().ToArray(), 0);
                        ReceivedUserCount?.Invoke(this, new SocketReceivedUserCountArgs()
                            {
                                RoomID    = RoomID,
                                UserCount = viewer
                            });
                        break;
                    }

                    case 3:
                    case 4:
                    {
                        var json = Encoding.UTF8.GetString(buffer, 0, playloadlength);
                        try
                        {
                            BilibiliLiveDanmaku_SocketReceiveData dama = new BilibiliLiveDanmaku_SocketReceiveData(json, 2);
                            ReceivedDanmaku?.Invoke(this, new SocketReceivedDanmakuArgs()
                                {
                                    RoomID  = RoomID,
                                    Danmaku = dama
                                });
                        }
                        catch (Exception)
                        {
                        }
                        break;
                    }

                    case 5:
                    {
                        break;
                    }

                    case 7:
                    {
                        break;
                    }

                    case 16:
                    {
                        break;
                    }

                    default:
                    {
                        break;
                    }
                    }
                }
            }
            catch (NotSupportedException ex)
            {
                Disconnect(ex);
            }
            catch (Exception ex)
            {
                Disconnect(ex);
            }
        }