コード例 #1
0
        /// <summary>
        /// Parser method to copy all body bytes.
        /// </summary>
        /// <param name="reader"> </param>
        /// <returns></returns>
        /// <remarks>Needed since a TCP packet can contain multiple messages
        /// after each other, or partial messages.</remarks>
        private bool ParseBody(IBufferReader reader)
        {
            if (reader.RemainingLength == 0)
            {
                return(false);
            }

            if (_currentMessage.Body == null)
            {
                if (_currentMessage.ContentLength > _bufferSize)
                {
                    _currentMessage.Body =
                        new FileStream(
                            Path.Combine(Path.GetTempPath(), "http." + Guid.NewGuid().ToString("N") + ".tmp"),
                            FileMode.CreateNew);
                }
                else
                {
                    var slice = _bufferPool.Pop();
                    _currentMessage.Body = new SliceStream(slice);
                }
            }

            var bytesLeft =
                (int)Math.Min(_currentMessage.ContentLength - _currentMessage.Body.Length, reader.RemainingLength);

            reader.CopyTo(_currentMessage.Body, bytesLeft);
            return(_currentMessage.Body.Length == _currentMessage.ContentLength);
        }
コード例 #2
0
        /// <summary>
        /// Append more bytes to your message building
        /// </summary>
        /// <param name="reader">Contains bytes which was received from the other end</param>
        /// <returns><c>true</c> if a complete message has been built; otherwise <c>false</c>.</returns>
        /// <remarks>You must handle/read everything which is available in the buffer</remarks>
        public bool Append(IBufferReader reader)
        {
            _headerParser.Parse(reader);
            if (_bodyBytestLeft > 0)
            {
                var bytesToRead = Math.Min(reader.RemainingLength, _bodyBytestLeft);
                reader.CopyTo(_bodyStream, bytesToRead);
                _bodyBytestLeft -= bytesToRead;

                if (_bodyBytestLeft == 0)
                {
                    _bodyStream.Position = 0;
                    _messages.Enqueue(_message);
                    _message = null;
                }

                if (reader.RemainingLength > 0)
                {
                    _headerParser.Parse(reader);
                }
            }

            return _messages.Count > 0;
        }
コード例 #3
0
        /// <summary>
        /// Read all body bytes from the incoming buffer
        /// </summary>
        /// <param name="reader">Contains received bytes</param>
        /// <returns><c>false</c> if we have not received all bytes yet; otherwise <c>true</c>.</returns>
        protected virtual bool ReadBodyBytes(IBufferReader reader)
        {
            var bytesLeftInStream = reader.Count - reader.Position;
            var bytesToCopy       = bytesLeftInStream < _bytesLeft
                                  ? bytesLeftInStream
                                  : _bytesLeft;


            reader.CopyTo(_packet.Message, bytesToCopy);

            _bytesLeft -= bytesToCopy;
            if (_bytesLeft > 0)
            {
                return(false);
            }

            _packet.Message.Position = 0;
            _messages.Enqueue(_packet);
            _packet = null;

            _bytesLeft    = Packet.HeaderLength;
            _parserMethod = ReadHeaderBytes;
            return(true);
        }
コード例 #4
0
        /// <summary>
        /// Append more bytes to your message building
        /// </summary>
        /// <param name="reader">Contains bytes which was received from the other end</param>
        /// <returns><c>true</c> if a complete message has been built; otherwise <c>false</c>.</returns>
        /// <remarks>You must handle/read everything which is available in the buffer</remarks>
        public bool Append(IBufferReader reader)
        {
            this.headerParser.Parse(reader);
            if (this.bodyBytestLeft > 0)
            {
                var bytesToRead = Math.Min(reader.RemainingLength, this.bodyBytestLeft);
                reader.CopyTo(this.bodyStream, bytesToRead);
                this.bodyBytestLeft -= bytesToRead;

                if (this.bodyBytestLeft == 0)
                {
                    this.bodyStream.Position = 0;
                    this.messages.Enqueue(this.message);
                    this.message = null;
                }

                if (reader.RemainingLength > 0)
                {
                    this.headerParser.Parse(reader);
                }
            }

            return(this.messages.Count > 0);
        }
コード例 #5
0
        /// <summary>
        /// Read all body bytes from the incoming buffer
        /// </summary>
        /// <param name="reader">Contains received bytes</param>
        /// <returns><c>false</c> if we have not received all bytes yet; otherwise <c>true</c>.</returns>
        protected virtual bool ReadBodyBytes(IBufferReader reader)
        {
            var bytesLeftInStream = reader.Count - reader.Position;
            var bytesToCopy = bytesLeftInStream < _bytesLeft
                                  ? bytesLeftInStream
                                  : _bytesLeft;


            reader.CopyTo(_packet.Message, bytesToCopy);

            _bytesLeft -= bytesToCopy;
            if (_bytesLeft > 0)
                return false;

            _packet.Message.Position = 0;
            _messages.Enqueue(_packet);
            _packet = null;

            _bytesLeft = Packet.HeaderLength;
            _parserMethod = ReadHeaderBytes;
            return true;
        }
コード例 #6
0
        /// <summary>
        /// Parser method to copy all body bytes.
        /// </summary>
        /// <param name="reader"> </param>
        /// <returns></returns>
        /// <remarks>Needed since a TCP packet can contain multiple messages
        /// after each other, or partial messages.</remarks>
        private bool ParseBody(IBufferReader reader)
        {
            if (reader.RemainingLength == 0)
                return false;

            if (_currentMessage.Body == null)
            {
                if (_currentMessage.ContentLength > _bufferSize)
                    _currentMessage.Body =
                        new FileStream(
                            Path.Combine(Path.GetTempPath(), "http." + Guid.NewGuid().ToString("N") + ".tmp"),
                            FileMode.CreateNew);
                else
                {
                    var slice = _bufferPool.Pop();
                    _currentMessage.Body = new SliceStream(slice);
                }
            }

            var bytesLeft =
                (int) Math.Min(_currentMessage.ContentLength - _currentMessage.Body.Length, reader.RemainingLength);
            reader.CopyTo(_currentMessage.Body, bytesLeft);
            return _currentMessage.Body.Length == _currentMessage.ContentLength;
        }
コード例 #7
0
 /// <summary>
 /// Synchronously reads the bytes from the current <see cref="IBufferReader{Byte}"/> and writes them to a Stream
 /// </summary>
 /// <param name="source">The current buffer reader to read from</param>
 /// <param name="target">The stream to write to</param>
 public static void CopyTo(this IBufferReader <byte> source, Stream target) => source.CopyTo(target, 81920);        // Same buffer size as Stream.CopyTo