Ejemplo n.º 1
0
        public static CompressedPacket Compress(ref UncompressedPacket packet)
        {
            var targetPacket = default(CompressedPacket);

            using (var stream = new MemoryStream())
                using (var bw = new BinaryWriter(new DeflateStream(stream, CompressionMode.Compress)))
                {
                    bw.WriteAsVarInt(packet.PacketId, out var packetIdLen);
                    bw.Write(packet.Data);
                    bw.Flush();

                    targetPacket.DataLength     = packetIdLen + (uint)packet.Data.Length;
                    targetPacket.CompressedData = stream.ToArray();
                }

            return(targetPacket);
        }
Ejemplo n.º 2
0
        public static CompressedPacket Compress(ref UncompressedPacket packet)
        {
            throw new NotImplementedException();

            /*
             * var targetPacket = default(CompressedPacket);
             * using (var stream = new MemoryStream())
             * using (var bw = new BinaryWriter(new DeflateStream(stream, CompressionMode.Compress)))
             * {
             *  bw.WriteAsVarInt(packet.PacketId, out var packetIdLen);
             *  bw.Write(packet.Data);
             *  bw.Flush();
             *
             *  targetPacket.DataLength = packetIdLen + (uint)packet.Data.Length;
             *  targetPacket.CompressedData = stream.ToArray();
             * }
             *
             * return targetPacket;*/
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public UncompressedPacket Decompress(CompressedPacket packet, uint threshold)
        {
            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;

            var 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 = new byte[dataLength - packetIdLen];
                    br.Read(targetPacket.Data.Array, targetPacket.Data.Offset, targetPacket.Data.Count);
                }

            targetPacket.Length = packet.DataLength;
            return(targetPacket);
        }
Ejemplo n.º 5
0
        public static async Task <UncompressedPacket> DeserializeAsync(Stream stream, IBufferPoolScope <byte> bufferPool, UncompressedPacket packet = null)
        {
            packet = packet ?? new UncompressedPacket();
            int packetIdLen;

            using (var br = new BinaryReader(stream, Encoding.UTF8, true))
            {
                packet.Length = br.ReadAsVarInt(out _);
                Protocol.ValidatePacketLength(packet.Length);
                packet.PacketId = br.ReadAsVarInt(out packetIdLen);
            }

            packet.Data = bufferPool.Rent((int)(packet.Length - packetIdLen));
            await stream.ReadExactAsync(packet.Data.Array, packet.Data.Offset, packet.Data.Count);

            return(packet);
        }
Ejemplo n.º 6
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, true) : 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);
        }