Example #1
0
        public void seeking_past_end_of_stream_throws_an_argumentexception()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            stream.Write(new byte[500], 0, 500);
            stream.Seek(501, SeekOrigin.Begin);
        }
 private void Clear()
 {
     _stream.Dispose();
     _stream     = null;
     _packetType = null;
     _header     = null;
 }
Example #3
0
        /// <summary>
        /// Deserialize the stream contents into a JSON object
        /// </summary>
        /// <param name="body">Stream with JSON</param>
        /// <returns>Generated request.</returns>
        protected virtual Request DeserializeRequest(BufferPoolStream body)
        {
            var reader = new StreamReader(body);
            var json   = reader.ReadToEnd();

            return(JsonConvert.DeserializeObject <Request>(json));
        }
Example #4
0
        /// <summary>
        /// Process message
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message"></param>
        /// <remarks>
        /// Should always call either <see cref="IPipelineHandlerContext.SendDownstream"/> or <see cref="IPipelineHandlerContext.SendUpstream"/>
        /// unless the handler really wants to stop the processing.
        /// </remarks>
        public void HandleDownstream(IPipelineHandlerContext context, IPipelineMessage message)
        {
            var msg = message as SendResponse;

            if (msg == null)
            {
                context.SendDownstream(message);
                return;
            }

            var buffer = _bufferPool.PopSlice();
            var stream = new BufferPoolStream(_bufferPool, buffer);

            _encoder.Encode(msg.Response, stream);
            stream.Position = 0;

            // send header
            var header = new byte[6];

            header[0] = 1;
            header[1] = _mapper.GetContentId(msg.Response);
            var lengthBuffer = BitConverter.GetBytes(buffer.Count);

            Buffer.BlockCopy(lengthBuffer, 0, header, 2, lengthBuffer.Length);
            context.SendDownstream(new SendBuffer(header, 0, 6));

            // send body
            context.SendDownstream(new SendStream(stream));
        }
Example #5
0
        /// <summary>
        /// Send all headers to the client
        /// </summary>
        /// <param name="response">Response containing call headers.</param>
        public Stream SerializeHeaders(IResponse response)
        {
            var stream = new BufferPoolStream(_pool, _pool.PopSlice());
            var writer = new StreamWriter(stream);

            writer.WriteLine("{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);

            var contentType = response.ContentType ?? "text/html";

            if (response.ContentEncoding != null)
            {
                contentType += ";charset=" + response.ContentEncoding.WebName;
            }

            // go through all property headers.
            writer.WriteLine("Content-Type: {0}", contentType);
            writer.WriteLine("Content-Length: {0}", response.ContentLength);
            //writer.WriteLine(response.KeepAlive ? "Connection: Keep-Alive" : "Connection: Close");

            if (response.Cookies != null && response.Cookies.Count > 0)
            {
                SerializeCookies(response, writer);
            }

            foreach (var header in response.Headers)
            {
                writer.WriteLine("{0}: {1}", header.Name, header.Value);
            }

            writer.WriteLine();
            writer.Flush();
            return(stream);
        }
        public void seeking_past_end_of_stream_throws_an_argumentexception()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            stream.Write(new byte[500], 0, 500);
            Assert.Throws <ArgumentOutOfRangeException>(() => { stream.Seek(501, SeekOrigin.Begin); });
        }
Example #7
0
        public void position_is_incremented()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            stream.Write(new byte[500], 0, 500);
            Assert.AreEqual(500, stream.Position);
        }
        private void HandleHeader(IPipelineHandlerContext context, IPipelineMessage message, ReceivedHeader headerMsg)
        {
            _packetType = _mapper.GetPacketType(_header.ContentId);
            if (_packetType == null)
            {
                // not supported, let the rest of the pipeline
                // handle the packet.
                context.SendUpstream(message);
            }
            else
            {
                _header = headerMsg.Header;
                var buffer = _bufferPool.PopSlice();
                if (_header.ContentLength > buffer.Capacity)
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "Buffer ({0} bytes) is less than the packet content ({1} bytes). Sorry, that's not possible in the current version.",
                                  buffer.Capacity, _header.ContentLength));
                }

                _bytesLeft = _header.ContentLength;
                _stream    = new BufferPoolStream(_bufferPool, buffer);
            }
        }
Example #9
0
        public void from_end_sets_relative_to_end()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            stream.Write(new byte[500], 0, 500);
            stream.Seek(-100, SeekOrigin.End);
            Assert.AreEqual(400, stream.Position);
        }
Example #10
0
        public void from_begin_sets_relative_to_beginning()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            stream.Write(new byte[500], 0, 500);
            stream.Seek(22, SeekOrigin.Begin);
            Assert.AreEqual(22, stream.Position);
        }
Example #11
0
        public void from_current_sets_relative_to_current()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            stream.Write(new byte[500], 0, 500);
            stream.Seek(-2, SeekOrigin.Current);
            stream.Seek(1, SeekOrigin.Current);
            Assert.AreEqual(499, stream.Position);
        }
 public void position_is_incremented()
 {
     BufferPoolStream stream = new BufferPoolStream(BufferPool);
     stream.Write(new byte[500], 0, 500);
     stream.Seek(0, SeekOrigin.Begin);
     Assert.AreEqual(0, stream.Position);
     int read = stream.Read(new byte[50], 0, 50);
     Assert.AreEqual(50, stream.Position);
 }
Example #13
0
        /// <summary>
        /// Gets a message from a <see cref="BufferPool"/>
        /// </summary>
        /// <param name="bufferPool">The BufferPool to get data from.</param>
        /// <returns></returns>
        public virtual T From(BufferPool bufferPool)
        {
            if (bufferPool == null)
            {
                throw new ArgumentNullException("bufferPool");
            }
            var stream = new BufferPoolStream(bufferPool);

            return(From(stream));
        }
 public void a_read_past_the_end_of_the_stream_returns_zero()
 {
     BufferPoolStream stream = new BufferPoolStream(BufferPool);
     stream.Write(new byte[500], 0, 500);
     stream.Position = 0;
     int read = stream.Read(new byte[500], 0, 500);
     Assert.AreEqual(500, read);
     read = stream.Read(new byte[500], 0, 500);
     Assert.AreEqual(0, read);
 }
Example #15
0
        public void position_is_incremented()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            stream.Write(new byte[500], 0, 500);
            stream.Seek(0, SeekOrigin.Begin);
            Assert.AreEqual(0, stream.Position);
            stream.Read(new byte[50], 0, 50);
            Assert.AreEqual(50, stream.Position);
        }
        public void reading_from_the_stream_with_StreamCopyTo_returns_all_data()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);
            int size = 20123;
            stream.Write(new byte[size], 0, size);
            stream.Position = 0;

            var destination = new MemoryStream();
            stream.CopyTo(destination);
            Assert.AreEqual(destination.Length, size);
        }
Example #17
0
        public void a_read_past_the_end_of_the_stream_returns_zero()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            stream.Write(new byte[500], 0, 500);
            stream.Position = 0;
            int read = stream.Read(new byte[500], 0, 500);

            Assert.AreEqual(500, read);
            read = stream.Read(new byte[500], 0, 500);
            Assert.AreEqual(0, read);
        }
        /// <summary>
        /// Gets a <see cref="BufferPool"></see> representing the IMessage provided.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>A <see cref="BufferPool"></see> with a representation of the message</returns>
        public override BufferPool ToBufferPool(T message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var bufferPool = new BufferPool(_initialBuffers, _bufferManager);
            var stream     = new BufferPoolStream(bufferPool);

            ProtoBuf.Serializer.Serialize(stream, message);
            return(bufferPool);
        }
Example #19
0
        public BufferPool ToBufferPool(byte[] message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            var bufferPool = new BufferPool(_initialBuffers, _bufferManager);
            var stream     = new BufferPoolStream(bufferPool);

            stream.Write(message, 0, message.Length);
            return(bufferPool);
        }
Example #20
0
        public void reading_from_the_stream_with_StreamCopyTo_returns_all_data()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);
            int size = 20123;

            stream.Write(new byte[size], 0, size);
            stream.Position = 0;

            var destination = new MemoryStream();

            stream.CopyTo(destination);
            Assert.AreEqual(destination.Length, size);
        }
 public void a_null_buffer_pool_throws_an_argumentnullexception()
 {
     BufferPoolStream stream = new BufferPoolStream(null);
 }
 public void from_begin_sets_relative_to_beginning()
 {
     BufferPoolStream stream = new BufferPoolStream(BufferPool);
     stream.Write(new byte[500], 0, 500);
     stream.Seek(22, SeekOrigin.Begin);
     Assert.AreEqual(22, stream.Position);
 }
Example #23
0
        public void the_internal_buffer_pool_is_set()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            Assert.AreEqual(BufferPool, stream.BufferPool);
        }
 public void from_end_sets_relative_to_end()
 {
     BufferPoolStream stream = new BufferPoolStream(BufferPool);
     stream.Write(new byte[500], 0, 500);
     stream.Seek(-100, SeekOrigin.End);
     Assert.AreEqual(400, stream.Position);
 }
Example #25
0
        public void a_negative_position_throws_an_argumentexception()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            stream.Seek(-1, SeekOrigin.Begin);
        }
 public void the_internal_buffer_pool_is_set()
 {
     BufferPoolStream stream = new BufferPoolStream(BufferPool);
     Assert.AreEqual(BufferPool, stream.BufferPool);
 }
 public void a_negative_position_throws_an_argumentexception()
 {
     BufferPoolStream stream = new BufferPoolStream(BufferPool);
     stream.Seek(-1, SeekOrigin.Begin);
 }
 public void seeking_past_end_of_stream_throws_an_argumentexception()
 {
     BufferPoolStream stream = new BufferPoolStream(BufferPool);
     stream.Write(new byte[500], 0, 500);
     stream.Seek(501, SeekOrigin.Begin);
 }
 public void a_null_buffer_pool_throws_an_argumentnullexception()
 {
     BufferPoolStream stream = new BufferPoolStream(null);
 }
 public HttpListenerResponseAdapter(HttpListenerResponse response, IBufferPool bufferPool)
 {
     StreamsToDispose = new List <Stream>();
     this.response    = response;
     OutputStream     = new BufferPoolStream(response.OutputStream, bufferPool);
 }
Example #31
0
 public void PartialBuffeR()
 {
     var buffer = new byte[512];
     var stream = new BufferPoolStream(new BufferSlice(buffer, 256, 256, 0));
 }
        public void a_negative_position_throws_an_argumentexception()
        {
            BufferPoolStream stream = new BufferPoolStream(BufferPool);

            Assert.Throws <ArgumentOutOfRangeException>(() => { stream.Seek(-1, SeekOrigin.Begin); });
        }
 public void from_current_sets_relative_to_current()
 {
     BufferPoolStream stream = new BufferPoolStream(BufferPool);
     stream.Write(new byte[500], 0, 500);
     stream.Seek(-2, SeekOrigin.Current);
     stream.Seek(1, SeekOrigin.Current);
     Assert.AreEqual(499, stream.Position);
 }
 public void position_is_incremented()
 {
     BufferPoolStream stream = new BufferPoolStream(BufferPool);
     stream.Write(new byte[500], 0, 500);
     Assert.AreEqual(500, stream.Position);
 }
Example #35
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BodyDecoder"/> class.
        /// </summary>
        public BodyDecoder()
        {
            var slice = _bufferPool.PopSlice();

            _stream = new BufferPoolStream(_bufferPool, slice);
        }