Beispiel #1
0
        /// <summary>
        /// Processes the packet within the context. Returns true whether the packet was processed or throttled.
        /// </summary>
        /// <param name="channel">The through which the packet is coming/going out.</param>
        /// <param name="context">The packet context for this operation.</param>
        /// <returns>True whether the packet was processed or throttled, false otherwise.</returns>
        public static ProcessingState Process(Emitter.Connection channel, ProcessingContext context)
        {
            // Get the buffer to decompress
            var input = context.Buffer.AsSegment();

            try
            {
                // Reserve the buffer, we know exactly how many bytes to decompress
                var output = context.BufferReserve(
                    (int)VarInt.UvarInt(input.Array, input.Offset).Value
                    );

                // Decompress
                var length = Snappy.Decode(
                    input.Array, input.Offset, input.Count,
                    output.Array, output.Offset, output.Size
                    );

                // Switch the buffer to the decompressed one
                context.SwitchBuffer(output);
            }
            catch (Exception ex)
            {
                ex.Log();
            }

            return(ProcessingState.Success);
        }
Beispiel #2
0
        /// <summary>
        /// Applies a compressin to the buffer and returns a result within a stream.
        /// </summary>
        /// <param name="buffer">The buffer to compress.</param>
        /// <param name="padding">The padding offset to pre-allocate.</param>
        /// <param name="context">The context of the processing.</param>
        /// <returns>The length segment</returns>
        public static BufferSegment Process(ByteStream stream, int padding, ProcessingContext context)
        {
            stream.Flush();

            // Calculate the maximum compressed length
            var outSize = Snappy.MaxEncodedLen((int)stream.Length);
            var output  = context.BufferReserve(outSize + padding);

            // Acquire a snappy encoder which comes in with a table for state
            using (var snappy = Snappy.Acquire())
            {
                output.Size = snappy.Encode(stream.GetBuffer(), 0, (int)stream.Length, output, padding) + padding;
                return(output);
            }
        }