Beispiel #1
0
        public static byte[] CompressPacketsForWrapper(List <Packet> packets, CompressionLevel compressionLevel = CompressionLevel.Fastest)
        {
            long length = 0;

            foreach (Packet packet in packets)
            {
                length += packet.Encode().Length;
            }

            compressionLevel = length > 1000 ? compressionLevel : CompressionLevel.NoCompression;

            using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream())
            {
                int checksum;
                using (var compressStream = new DeflateStream(stream, compressionLevel, true))
                {
                    foreach (Packet packet in packets)
                    {
                        byte[] bs = packet.Encode();
                        if (bs != null && bs.Length > 0)
                        {
                            BatchUtils.WriteLength(compressStream, bs.Length);
                            compressStream.Write(bs, 0, bs.Length);
                        }
                        packet.PutPool();
                    }
                    compressStream.Flush();
                }

                byte[] bytes = stream.ToArray();
                return(bytes);
            }
        }
Beispiel #2
0
        public static byte[] CompressPacketsForWrapper(List <Packet> packets, CompressionLevel compressionLevel = CompressionLevel.Fastest)
        {
            long length = 0;

            foreach (Packet packet in packets)
            {
                length += packet.Encode().Length;
            }

            compressionLevel = length > 1000 ? compressionLevel : CompressionLevel.NoCompression;

            using (MemoryStream stream = MiNetServer.MemoryStreamManager.GetStream())
            {
                stream.WriteByte(0x78);                 // zlib header
                switch (compressionLevel)
                {
                case CompressionLevel.Optimal:
                    stream.WriteByte(0xda);
                    break;

                case CompressionLevel.Fastest:
                    stream.WriteByte(0x9c);
                    break;

                case CompressionLevel.NoCompression:
                    stream.WriteByte(0x01);
                    break;
                }
                int checksum;
                using (var compressStream = new ZLibStream(stream, compressionLevel, true))
                {
                    foreach (Packet packet in packets)
                    {
                        byte[] bs = packet.Encode();
                        if (bs != null && bs.Length > 0)
                        {
                            BatchUtils.WriteLength(compressStream, bs.Length);
                            compressStream.Write(bs, 0, bs.Length);
                        }
                        packet.PutPool();
                    }
                    checksum = compressStream.Checksum;
                    compressStream.Flush();
                }

                var checksumBytes = BitConverter.GetBytes(checksum);
                if (BitConverter.IsLittleEndian)
                {
                    // Adler32 checksum is big-endian
                    Array.Reverse(checksumBytes);
                }
                stream.Write(checksumBytes);

                byte[] bytes = stream.ToArray();
                return(bytes);
            }
        }