private void ReceiveMessageLoop() { try { var stableBuffer = new byte[Client.ReceiveBufferSize]; while (this.Connected) { NetStream.Read(stableBuffer, 0, 2); var typeId = BitConverter.ToInt16(stableBuffer, 0); typeId = IPAddress.NetworkToHostOrder(typeId); Console.WriteLine(typeId); switch (typeId) { case 1: NetStream.Read(stableBuffer, 0, 4); var viewer = BitConverter.ToInt32(stableBuffer, 0); viewer = IPAddress.NetworkToHostOrder(viewer); //觀眾人數 if (ReceivedRoomCount != null) { ReceivedRoomCount(this, new ReceivedRoomCountArgs() {UserCount = viewer}); } break; case 2: //newCommentString { NetStream.Read(stableBuffer, 0, 2); var packetlength = BitConverter.ToInt16(stableBuffer, 0); packetlength = (short) (IPAddress.NetworkToHostOrder(packetlength) - 4); var buffer = new byte[packetlength]; NetStream.Read(buffer, 0, packetlength); var json = Encoding.UTF8.GetString(buffer, 0, packetlength); if (debuglog) { Console.WriteLine(json); } DanmakuModel dama = new DanmakuModel(json); if (ReceivedDanmaku != null) { ReceivedDanmaku(this, new ReceivedDanmakuArgs() {Danmaku = dama}); } break; } case 8: //x 条新的评论信息 { NetStream.Read(stableBuffer, 0, 2); break; } case 17: //_playerDebug { //server updated break; } case 4: //playerCommand { NetStream.Read(stableBuffer, 0, 2); var packetlength = BitConverter.ToInt16(stableBuffer, 0); packetlength = (short) (IPAddress.NetworkToHostOrder(packetlength) - 4); var buffer = new byte[packetlength]; NetStream.Read(buffer, 0, packetlength); var json = Encoding.UTF8.GetString(buffer, 0, packetlength); if (debuglog) { Console.WriteLine(json); } try { DanmakuModel dama = new DanmakuModel(json, 2); if (ReceivedDanmaku != null) { ReceivedDanmaku(this, new ReceivedDanmakuArgs() {Danmaku = dama}); } } catch (Exception ex) { // ignored } break; } case 5: //playerBroadcast case 6: //newScrollMessage default: { NetStream.Read(stableBuffer, 0, 2); var packetlength = BitConverter.ToInt16(stableBuffer, 0); packetlength = (short) (IPAddress.NetworkToHostOrder(packetlength) - 4); var buffer = new byte[packetlength]; NetStream.Read(buffer, 0, packetlength); break; } // // Disconnect(); // this.Error = new Exception("收到非法包"); // break; } } } catch (NotSupportedException ex) { this.Error = ex; _disconnect(); } catch (Exception ex) { this.Error = ex; _disconnect(); } }
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("协议失败: (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); if (ReceivedRoomCount != null) { ReceivedRoomCount(this, new ReceivedRoomCountArgs() { UserCount = viewer }); } break; } case 3: case 4: //playerCommand { var json = Encoding.UTF8.GetString(buffer, 0, playloadlength); if (debuglog) { Console.WriteLine(json); } try { DanmakuModel dama = new DanmakuModel(json, 2); if (ReceivedDanmaku != null) { ReceivedDanmaku(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(); } // } }