예제 #1
0
        private void ClientboundTasks()
        {
            while (_Client.Connected)
            {
                if (_ClientboundPackets.Count > 0)
                {
                    // Build a temporary write buffer
                    DataBuffer buffer = new DataBuffer();

                    // Pop the first packet
                    ClientboundPacket packet = _ClientboundPackets.Dequeue();

                    buffer.Writer.WriteVarInt(packet.PacketID); // Write the packet ID
                    packet.Write(buffer);                       // Fill the temporary buffer with the response packet

                    // Write the buffer to the clientbound stream along with relevant metadata
                    byte[] bytes = buffer.Reader.ReadAll();

                    // Compress the packet
                    if (Compression == CompressionState.Enabled)
                    {
                        int uncompressedSize = bytes.Length;

                        if (uncompressedSize > Settings.CompressionThreshold)
                        {
                            ZlibBuffer zlib = new ZlibBuffer(bytes, CompressionMode.Compress); // Compress the bytes into the compressed stream
                            bytes = zlib.Reader.ReadAll();
                        }
                        else
                        {
                            uncompressedSize = 0; // Compression did not meet the threshold and was ignored
                        }

                        buffer = new DataBuffer();

                        buffer.Writer.WriteVarInt(uncompressedSize); // Write the size of the uncompressed data
                        buffer.Writer.Write(bytes);

                        bytes = buffer.Reader.ReadAll();
                    }

                    // Build a buffer for the packet size and the payload
                    buffer = new DataBuffer();

                    // Prepend the packet size
                    buffer.Writer.WriteVarInt(bytes.Length);
                    buffer.Writer.Write(bytes);

                    // Send the bytes to the client
                    bytes = buffer.Reader.ReadAll();

                    _Stream.Writer.Write(bytes);

                    // Enable compression after sending the Set Compression packet
                    if (Compression == CompressionState.Enabling && packet is States.Login.SetCompressionPacket)
                    {
                        Compression = CompressionState.Enabled;
                    }
                }
            }
        }
예제 #2
0
 public void Queue(ClientboundPacket packet)
 {
     _ClientboundPackets.Enqueue(packet);
 }