Example #1
0
 public void CopyFrom(HttpSocketContext other)
 {
     RequestHeadersLength  = other.RequestHeadersLength;
     ResponseHeadersLength = 0;
     if (RequestHeaders.Length < RequestHeadersLength)
     {
         var newHeaders = new HeaderPair[other.RequestHeaders.Length];
         Array.Copy(other.RequestHeaders, newHeaders, RequestHeadersLength);
         RequestHeaders = newHeaders;
     }
     else
     {
         Array.Copy(other.RequestHeaders, RequestHeaders, RequestHeadersLength);
     }
     RawUrl        = other.RawUrl;
     positionInTmp = other.positionInTmp;
     Pipeline      = other.Pipeline;
     IsHttp10      = other.IsHttp10;
     totalBytes    = other.totalBytes;
     Buffer.BlockCopy(other.InputTemp, 0, InputTemp, 0, positionInTmp);
     other.InputStream.CopyTo(InputStream);
     InputStream.Position = other.InputStream.Position;
     InputStream.SetLength(other.InputStream.Length);
     HttpMethod               = other.HttpMethod;
     HttpProtocolVersion      = other.HttpProtocolVersion;
     TemplateMatch            = other.TemplateMatch;
     ResponseStatus           = HttpStatusCode.OK;
     ResponseLength           = null;
     ResponseContentType      = null;
     ResponseIsJson           = false;
     ContentTypeResponseIndex = -1;
     offsetInOutput           = 0;
 }
Example #2
0
        private static void RunLoop <T>(
            int repeat,
            Action <object, ChunkedMemoryStream> serialize,
            Func <ChunkedMemoryStream, Type, object> deserialize,
            BenchType type,
            ChunkedMemoryStream ms,
            Func <int, T> factory) where T : IEquatable <T>
        {
            var  sw        = Stopwatch.StartNew();
            var  incorrect = 0;
            long size      = 0;

            for (int i = 0; i < repeat; i++)
            {
                ms.SetLength(0);
                var instance = factory(i);
                if (type == BenchType.None)
                {
                    continue;
                }
                serialize(instance, ms);
                size += ms.Position;
                if (type == BenchType.Both || type == BenchType.Check)
                {
                    ms.Position = 0;
                    var deser = (T)deserialize(ms, typeof(T));
                    if (type == BenchType.Check && !instance.Equals(deser))
                    {
                        incorrect++;
                        //throw new SerializationException("not equal");
                    }
                }
            }
            ReportStatsAndRestart(sw, size, incorrect);
        }
        public void Write_64k_bytes_Truncate_17k_Read_64k()
        {
            byte[] bytes  = GetBytes(64 * 1024);
            var    stream = new ChunkedMemoryStream();

            stream.Write(bytes, 0, bytes.Length);
            stream.SetLength(17 * 1024);
            stream.Position = 0;

            byte[] buffer = new byte[bytes.Length];
            int    read   = stream.Read(buffer, 0, buffer.Length);

            Assert.AreEqual(17 * 1024, read);
            Assert.AreEqual(bytes.Take(17 * 1024).ToArray(), buffer.Take(17 * 1024).ToArray());
            Assert.AreEqual(0, stream.Read(buffer, 0, buffer.Length));
        }
Example #4
0
        public void Write_64k_bytes_Truncate_17k_Read_64k()
        {
            byte[] bytes = GetBytes(64 * 1024);
            var stream = new ChunkedMemoryStream();
            stream.Write(bytes, 0, bytes.Length);
            stream.SetLength(17 * 1024);
            stream.Position = 0;

            byte[] buffer = new byte[bytes.Length];
            int read = stream.Read(buffer, 0, buffer.Length);
            Assert.AreEqual(17 * 1024, read);
            Assert.AreEqual(bytes.Take(17 * 1024).ToArray(), buffer.Take(17 * 1024).ToArray());
            Assert.AreEqual(0, stream.Read(buffer, 0, buffer.Length));
        }
 public void TestSetLengthNegative()
 {
     using (var stream = new ChunkedMemoryStream(4)) {
     stream.SetLength(-1);
       }
 }
        public void TestSetLength()
        {
            using (var stream = new ChunkedMemoryStream(4)) {
            for (var len = 0L; len < 12L; len++) {
              stream.SetLength(len);
              Assert.AreEqual(len, stream.Length);
              Assert.AreEqual(0L, stream.Position);
            }

            stream.Position = stream.Length;

            for (var len = 12L; len <= 0L; len--) {
              stream.SetLength(len);
              Assert.AreEqual(len, stream.Length);
              Assert.AreEqual(len, stream.Position);
            }

            stream.SetLength(22L);
            Assert.AreEqual(22L, stream.Length);

            stream.Position = 22L;

            stream.SetLength(14L);
            Assert.AreEqual(14L, stream.Length);
            Assert.AreEqual(14L, stream.Position);
            Assert.AreEqual(-1, stream.ReadByte());
            Assert.AreEqual(14L, stream.Position);

            stream.SetLength(3L);
            Assert.AreEqual(3L, stream.Length);
            Assert.AreEqual(3L, stream.Position);
            Assert.AreEqual(-1, stream.ReadByte());
            Assert.AreEqual(3L, stream.Position);

            stream.SetLength(13L);
            Assert.AreEqual(13L, stream.Length);
            Assert.AreEqual(3L, stream.Position);
            Assert.AreEqual(0, stream.ReadByte());
            Assert.AreEqual(4L, stream.Position);
              }
        }
        public void TestAllocateDisposeChunk()
        {
            var allocated = new List<TestChunk>();

              using (var stream = new ChunkedMemoryStream(4, delegate(int size) {
            Assert.AreEqual(4, size);
            return new TestChunk(size, allocated);
              })) {
            Assert.AreEqual(1, allocated.Count, "first chunk");

            var writer = new System.IO.BinaryWriter(stream);

            writer.Write(new byte[] {0x00, 0x01, 0x02});
            writer.Flush();

            Assert.AreEqual(3L, stream.Length);
            Assert.AreEqual(1, allocated.Count);

            writer.Write(new byte[] {0x03, 0x04, 0x05});
            writer.Flush();

            Assert.AreEqual(6L, stream.Length);
            Assert.AreEqual(2, allocated.Count, "extended by Write 1");

            writer.Write(new byte[] {0x06, 0x07, 0x08});
            writer.Flush();

            Assert.AreEqual(9L, stream.Length);
            Assert.AreEqual(3, allocated.Count, "extended by Write 2");

            stream.SetLength(stream.Length);
            Assert.AreEqual(9L, stream.Length);
            Assert.AreEqual(3, allocated.Count, "SetLength(stream.Length)");

            Console.WriteLine("set length 12");
            stream.SetLength(12L);
            Assert.AreEqual(12L, stream.Length);
            Assert.AreEqual(4, allocated.Count, "extended by SetLength 1");

            Console.WriteLine("set length 8");
            stream.SetLength(8L);
            Assert.AreEqual(8L, stream.Length);
            Assert.AreEqual(3, allocated.Count, "shorten by SetLength 1");

            Console.WriteLine("set length 7");
            stream.SetLength(7L);
            Assert.AreEqual(7L, stream.Length);
            Assert.AreEqual(2, allocated.Count, "shorten by SetLength 2");

            Console.WriteLine("set length 3");
            stream.SetLength(3L);
            Assert.AreEqual(3L, stream.Length);
            Assert.AreEqual(1, allocated.Count, "shorten by SetLength 3");

            Console.WriteLine("set length 0");
            stream.SetLength(0L);
            Assert.AreEqual(0L, stream.Length);
            Assert.AreEqual(1, allocated.Count, "shorten by SetLength 4");

            Console.WriteLine("set length 12");
            stream.SetLength(12L);
            Assert.AreEqual(12L, stream.Length);
            Assert.AreEqual(4, allocated.Count, "extended by SetLength 2");

            Console.WriteLine("set length 0");
            stream.SetLength(0L);
            Assert.AreEqual(0L, stream.Length);
            Assert.AreEqual(1, allocated.Count, "shorten by SetLength 5");
              }

              Assert.AreEqual(0, allocated.Count, "closed");
        }