private void handlePacket() { cRead = new Thread(() => { try { while (Client.Connected) { int data_len = readNextVarIntRAW(); int packetID = 0; List <byte> packetData = new List <byte>(readDataRAW(data_len)); if (compression_treshold > 0) { int compressed_length = readNextVarInt(packetData); if (compressed_length > 0)//封包已压缩 { byte[] uncompress = ZlibUtils.Decompress(packetData.ToArray(), compressed_length); packetData = new List <byte>(uncompress); } } packetID = readNextVarInt(packetData); var type = protocol.getPacketOutgoingType(packetID); switch (type) { case PacketOutgoingType.ChatMessage: string chatmsg = readNextString(packetData); Debug.Log("Chat:" + chatmsg, Username); if (chatmsg.StartsWith("/")) { if (OnCommand(chatmsg)) { continue; } } packetData = new List <byte>(getString(chatmsg)); break; case PacketOutgoingType.ClientSettings: clientSettings = new ClientSettings(); clientSettings.ReadBuffer(packetData, protocol.protocolVersion); Proxy.SendPacket(clientSettings); continue; } //Console.Write(packetID+" "); Proxy.SendPacket(packetID, packetData); } } catch { Close(); } }); cRead.Start(); }
private void readNextPacket(ref int packetID, List <byte> packetData) { packetData.Clear(); int size = readNextVarIntRAW(); //Packet size packetData.AddRange(readDataRAW(size)); //Packet contents //Handle packet decompression if (protocolversion >= MCVersion.MC18Version && compression_treshold > 0) { int sizeUncompressed = readNextVarInt(packetData); if (sizeUncompressed != 0) // != 0 means compressed, let's decompress { byte[] toDecompress = packetData.ToArray(); byte[] uncompressed = ZlibUtils.Decompress(toDecompress, sizeUncompressed); packetData.Clear(); packetData.AddRange(uncompressed); } } packetID = readNextVarInt(packetData); //Packet ID }