示例#1
0
        public static UncompressedPacket Decompress(ref CompressedPacket packet)
        {
            var targetPacket = default(UncompressedPacket);

            using (var br = new BinaryReader(new DeflateStream(new MemoryStream(packet.CompressedData), CompressionMode.Decompress)))
            {
                targetPacket.PacketId = br.ReadAsVarInt(out var packetIdLen);
                targetPacket.Data     = br.ReadBytes((int)packet.DataLength - packetIdLen);
            }

            targetPacket.Length = packet.DataLength;
            return(targetPacket);
        }
示例#2
0
        public static UncompressedPacket Decompress(ref CompressedPacket packet)
        {
            throw new NotImplementedException();

            /*
             * var targetPacket = default(UncompressedPacket);
             * using (var br = new BinaryReader(new DeflateStream(new MemoryStream(packet.CompressedData), CompressionMode.Decompress)))
             * {
             *  targetPacket.PacketId = br.ReadAsVarInt(out var packetIdLen);
             *  targetPacket.Data = br.ReadBytes((int)packet.DataLength - packetIdLen);
             * }
             *
             * targetPacket.Length = packet.DataLength;
             * return targetPacket;*/
        }
示例#3
0
        public static async Task <CompressedPacket> DeserializeAsync(Stream stream, CompressedPacket packet = null)
        {
            packet = packet ?? new CompressedPacket();
            int dataLengthLen;

            using (var br = new BinaryReader(stream, Encoding.UTF8, true))
            {
                packet.PacketLength = br.ReadAsVarInt(out _);
                packet.DataLength   = br.ReadAsVarInt(out dataLengthLen);
            }

            packet.CompressedData = new byte[packet.PacketLength - dataLengthLen];
            await stream.ReadExactAsync(packet.CompressedData, 0, packet.CompressedData.Length);

            return(packet);
        }
示例#4
0
        public CompressedPacket Compress(UncompressedPacket packet, uint threshold)
        {
            var targetPacket = new CompressedPacket();

            using (var stream = new MemoryStream())
            {
                var  dataLength     = packet.PacketId.SizeOfVarInt() + (uint)packet.Data.Count;
                bool useCompression = dataLength >= threshold;
                targetPacket.DataLength = useCompression ? dataLength : 0;

                using (var bw = new BinaryWriter(useCompression ? (Stream) new ZlibStream(stream, CompressionMode.Compress, CompressionLevel.BestSpeed) : stream))
                {
                    bw.WriteAsVarInt(packet.PacketId, out _);
                    bw.Write(packet.Data.Array, packet.Data.Offset, packet.Data.Count);
                    bw.Flush();
                }

                targetPacket.CompressedData = stream.ToArray();
            }

            return(targetPacket);
        }
示例#5
0
        public static UncompressedPacket Decompress(CompressedPacket packet, IBufferPoolScope <byte> bufferPool, uint threshold, UncompressedPacket targetPacket = null)
        {
            if (packet.DataLength != 0 && packet.DataLength < threshold)
            {
                throw new InvalidDataException("Uncompressed data length is lower than threshold.");
            }
            bool useCompression = packet.DataLength != 0;
            var  dataLength     = useCompression ? packet.DataLength : (uint)packet.CompressedData.Length;

            targetPacket = targetPacket ?? new UncompressedPacket();
            using (var stream = new MemoryStream(packet.CompressedData))
                using (var br = new BinaryReader(useCompression ? (Stream) new ZlibStream(stream, CompressionMode.Decompress, CompressionLevel.BestSpeed) : stream))
                {
                    targetPacket.PacketId = br.ReadAsVarInt(out var packetIdLen);

                    targetPacket.Data = bufferPool.Rent((int)(dataLength - packetIdLen));
                    br.Read(targetPacket.Data.Array, targetPacket.Data.Offset, targetPacket.Data.Count);
                }

            targetPacket.Length = packet.DataLength;
            return(targetPacket);
        }