Exemplo n.º 1
0
        private void completeSendPacket(params object[] args)
        {
            ClientBoundPacket Packet = (ClientBoundPacket)args[0];

            try
            {
                if (Server.CompressionEnabled)
                {
                    int dataLength = Packet.GetLength(Version) + DataUtils.MeasureVarInt(Packet.GetPacketID(Version));
                    if (dataLength > Server.CompressionThreshold)
                    {
                        byte[] uncompressedData = new byte[dataLength];
                        using (MemoryStream ms = new MemoryStream(uncompressedData))
                        {
                            ms.WriteVarInt(Packet.GetPacketID(Version));
                            Packet.WriteToStream(ms, Version);
                        }
                        byte[] compressedData = ZlibStream.CompressBuffer(uncompressedData);
                        tcpStream.WriteVarInt(DataUtils.MeasureVarInt(dataLength) + compressedData.Length);
                        tcpStream.WriteVarInt(dataLength);
                        tcpStream.Write(compressedData, 0, compressedData.Length);
                    }
                    else
                    {
                        tcpStream.WriteVarInt(dataLength + DataUtils.MeasureVarInt(0));
                        tcpStream.WriteVarInt(0); // uncompresed
                        tcpStream.WriteVarInt(Packet.GetPacketID(Version));
                        Packet.WriteToStream(tcpStream, Version);
                    }
                }
                else
                {
                    tcpStream.WritePacketHeader(Packet.GetLength(Version), Packet.GetPacketID(Version));
                    Packet.WriteToStream(tcpStream, Version);
                }
            } catch (IOException)
            {
                Server.ServerThread.Queue(completeDisconnect, DisconnectReason.ClientClosedConnection);
            }
        }
Exemplo n.º 2
0
 public void SendPacket(ClientBoundPacket Packet)
 {
     sendThread.Queue(completeSendPacket, Packet);
 }