Exemple #1
0
        protected virtual void ProcessRequest(TcpConnection socket)
        {
            Log.LogVerbose("Processing Request");

            using (BufferSequence rootBuffer = new BufferSequence(RequestBufferSize)) {
                BufferSequence requestBuffer = rootBuffer;
                int            totalWritten  = 0;
                while (true)
                {
                    Span <byte> requestSpan = requestBuffer.Free;

                    int requestBytesRead = socket.Receive(requestSpan);
                    if (requestBytesRead == 0)
                    {
                        socket.Close();
                        return;
                    }

                    requestBuffer.Advance(requestBytesRead);
                    totalWritten += requestBytesRead;
                    if (requestBytesRead == requestSpan.Length)
                    {
                        requestBuffer = requestBuffer.Append(RequestBufferSize);
                    }
                    else
                    {
                        break;
                    }
                }

                var requestBytes = new ReadOnlyBuffer <byte>(rootBuffer, 0, requestBuffer, requestBuffer.Memory.Length);

                var request = new HttpRequest();
                if (!s_parser.ParseRequestLine(ref request, requestBytes, out int consumed))
                {
                    throw new Exception();
                }
                requestBytes = requestBytes.Slice(consumed);
                if (!s_parser.ParseHeaders(ref request, requestBytes, out consumed))
                {
                    throw new Exception();
                }

                var requestBody = requestBytes.Slice(consumed);

                Log.LogRequest(request, requestBody);

                using (var response = new TcpConnectionFormatter(socket, ResponseBufferSize)) {
                    WriteResponse(ref request, requestBody, response);
                }

                socket.Close();
            }

            if (Log.IsVerbose)
            {
                Log.LogMessage(Log.Level.Verbose, "Request Processed and Response Sent", DateTime.UtcNow.Ticks);
            }
        }
Exemple #2
0
        public BufferSequence Append(int desiredSize = DefaultBufferSize)
        {
            var next = new BufferSequence(desiredSize)
            {
                RunningIndex = RunningIndex + Memory.Length
            };

            Next = next;
            return(next);
        }
Exemple #3
0
        protected virtual void Dispose(bool disposing)
        {
            var array = _array;

            _array = null;
            if (array != null)
            {
                ArrayPool <byte> .Shared.Return(array);
            }
            if (_next != null)
            {
                _next.Dispose();
            }
            _next = null;
        }
Exemple #4
0
 public BufferSequence Append(int desiredSize = DefaultBufferSize)
 {
     _next = new BufferSequence(desiredSize);
     return(_next);
 }