Exemple #1
0
        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();
        }
Exemple #2
0
 private void SendPacket(int packetID, IEnumerable <byte> packetData)
 {
     byte[] the_packet = concatBytes(getVarInt(packetID), packetData.ToArray());
     if (protocol.protocolVersion > MCVersion.MC18Version && compression_treshold > 0)
     {
         if (the_packet.Length > compression_treshold)
         {
             int sizeUncompressed = the_packet.Length;
             the_packet = concatBytes(getVarInt(sizeUncompressed), ZlibUtils.Compress(the_packet));
         }
         else
         {
             the_packet = concatBytes(getVarInt(0), the_packet);
         }
     }
     SendRAW(concatBytes(getVarInt(the_packet.Length), the_packet));
 }
 public void SendPacket(int packetID, IEnumerable <byte> packetData)
 {
     //Console.Write(packetID + " ");
     byte[] the_packet = concatBytes(getVarInt(packetID), packetData.ToArray());
     if (compression_treshold > 0)
     {
         if (the_packet.Length >= compression_treshold)
         {
             byte[] compressed_packet = ZlibUtils.Compress(the_packet);
             the_packet = concatBytes(getVarInt(the_packet.Length), compressed_packet);
         }
         else
         {
             byte[] uncompressed_length = getVarInt(0); //Not compressed (short packet)
             the_packet = concatBytes(uncompressed_length, the_packet);
         }
     }
     SendRAW(concatBytes(getVarInt(the_packet.Length), the_packet));
 }
        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
        }