Exemplo n.º 1
0
        public void TestGoldEncodes()
        {
            var tests = new string[][] {
                new string[] { "upstream/alice29.txt", "gold/alice29.txt.gold" },
                new string[] { "upstream/asyoulik.txt", "gold/asyoulik.txt.gold" },
                new string[] { "upstream/fireworks.jpeg", "gold/fireworks.jpeg.gold" },
                new string[] { "upstream/geo.protodata", "gold/geo.protodata.gold" },
                new string[] { "upstream/html", "gold/html.gold" },
                new string[] { "upstream/html_x_4", "gold/html_x_4.gold" },
                new string[] { "upstream/kppkn.gtb", "gold/kppkn.gtb.gold" },
                new string[] { "upstream/lcet10.txt", "gold/lcet10.txt.gold" },
                new string[] { "upstream/paper-100k.pdf", "gold/paper-100k.pdf.gold" },
                new string[] { "upstream/plrabn12.txt", "gold/plrabn12.txt.gold" },
                new string[] { "upstream/urls.10K", "gold/urls.10K.gold" },
            };

            foreach (var test in tests)
            {
                var data = ReadAll(TestFile(test[0]));
                var gold = ReadAll(TestFile(test[1]));

                var comp = SnappyEncoder.Encode(data);

                Assert.AreEqual(gold.Length, comp.Length);
                AssertArraysEqual(gold, comp);

                Roundtrip(data);
            }
        }
Exemplo n.º 2
0
        void Roundtrip(byte[] data)
        {
            var comp   = SnappyEncoder.Encode(data);
            var decomp = SnappyDecoder.Decode(comp);

            Assert.AreEqual(data.Length, decomp.Length);
            AssertArraysEqual(data, decomp);
        }
Exemplo n.º 3
0
        public void TestEmpty()
        {
            byte[] data = new byte[] { };

            var comp = SnappyEncoder.Encode(data);

            AssertArraysEqual(new byte[] { 0x00 }, comp);
        }
Exemplo n.º 4
0
        public void TestPaddedEncode()
        {
            byte[] data = new byte[] {
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
            };

            var off  = Padded(data, 5, 0);
            var comp = SnappyEncoder.Encode(off, 5, off.Length - 5);

            AssertArraysEqual(SnappyEncoder.Encode(data), comp);
        }
Exemplo n.º 5
0
        public void TestSimple()
        {
            byte[] data = new byte[] {
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
            };

            var comp = SnappyEncoder.Encode(data);

            AssertArraysEqual(new byte[] {
                0x12, 0x20, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
                0x15, 0x09
            }, comp);

            Roundtrip(data);
        }
Exemplo n.º 6
0
        public void TestPaddedEncodeToOffset()
        {
            byte[] data = new byte[] {
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
            };

            var off = Padded(data, 5, 5);
            var dst = new byte[SnappyEncoder.MaxEncodedLen(off.Length) + 3];
            var len = SnappyEncoder.Encode(off, 5, data.Length, dst, 3, dst.Length - 3);

            var comp = new byte[len];

            Array.Copy(dst, 3, comp, 0, len);

            AssertArraysEqual(SnappyEncoder.Encode(data), comp);
        }
Exemplo n.º 7
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)
        {
            // It can only process packets
            var packet = context.Packet as MeshPacket;

            if (packet == null)
            {
                return(ProcessingState.Failure);
            }

            if (channel.Client.TransportType != TransportType.Tcp)
            {
                return(ProcessingState.Failure);
            }

            var command = packet as MeshEvent;

            if (command != null && command.Type == MeshEventType.Custom)
            {
                NetTrace.WriteLine("Sending " + command.Value, channel, NetTraceCategory.Mesh);
            }

            // Prepare a stream variable
            var stream = ByteStreamPool.Default.Acquire();
            var writer = PacketWriterPool.Default.Acquire(stream);

            try
            {
                // Write and compile the packet
                packet.Write(writer);

                // Compress the packet
                var encoded       = SnappyEncoder.Process(stream, PacketHeader.TotalSize, context);
                int messageLength = encoded.Size - PacketHeader.TotalSize;

                // Add a sample to the average
                NetStat.Compression.Sample(1 - ((double)messageLength / stream.Length));

                // Write mesh prefix
                encoded.Array[encoded.Offset + 0] = 77; // 'M'
                encoded.Array[encoded.Offset + 1] = 83; // 'S'
                encoded.Array[encoded.Offset + 2] = 72; // 'H'

                // Write message type
                if (packet is MeshFrame)
                {
                    encoded.Array[encoded.Offset + 3] = 70; // 'F'
                }
                else if (packet is MeshEvent)
                {
                    encoded.Array[encoded.Offset + 3] = 67; // 'C'
                }
                // Write message length
                encoded.Array[encoded.Offset + 4] = (byte)(messageLength >> 24);
                encoded.Array[encoded.Offset + 5] = (byte)(messageLength >> 16);
                encoded.Array[encoded.Offset + 6] = (byte)(messageLength >> 8);
                encoded.Array[encoded.Offset + 7] = (byte)messageLength;

                // Copy to a buffer segment
                context.SwitchBuffer(encoded);
            }
            finally
            {
                // Make sure we release the stream and the writer
                stream.TryRelease();
                writer.TryRelease();
            }

            return(ProcessingState.Success);
        }