Exemplo n.º 1
0
        protected virtual void ProcessRequest(TcpConnection socket)
        {
            Log.LogVerbose("Processing Request");
            
            var requestBuffer = ArrayPool<byte>.Shared.Rent(RequestBufferSize);
            var requestByteCount = socket.Receive(requestBuffer);

            if(requestByteCount == 0) {
                socket.Close();
                return;
            }

            var requestBytes = requestBuffer.Slice(0, requestByteCount);
            var request = HttpRequest.Parse(requestBytes);
            Log.LogRequest(request);

            var formatter = new BufferFormatter(1024, FormattingData.InvariantUtf8);
            WriteResponse(formatter, request);

            ArrayPool<byte>.Shared.Return(requestBuffer);

            var response = formatter.Buffer.Slice(0, formatter.CommitedByteCount);

            Console.WriteLine("Response:");
            Console.WriteLine(new Utf8String(response));

            socket.Send(response);
            socket.Close();

            if (Log.IsVerbose)
            {
                Log.LogMessage(Log.Level.Verbose, "Request Processed and Response Sent", DateTime.UtcNow.Ticks);
            }
        }
Exemplo n.º 2
0
        protected virtual void ProcessRequest(TcpConnection socket)
        {
            Log.LogVerbose("Processing Request");
            
            var requestBuffer = s_pool.Rent(RequestBufferSize);
            var requestByteCount = socket.Receive(requestBuffer);

            if(requestByteCount == 0) {
                socket.Close();
                return;
            }

            var requestBytes = requestBuffer.Slice(0, requestByteCount);
            var request = HttpRequest.Parse(requestBytes);
            Log.LogRequest(request);

            using (var response = new HttpResponse(1024)) {
                WriteResponse(request, response);
                s_pool.Return(requestBuffer);

                foreach(var segment in response.Headers) {
                    socket.Send(segment);
                }
                foreach (var segment in response.Body) {
                    socket.Send(segment);
                }

                socket.Close();
            }

            if (Log.IsVerbose)
            {
                Log.LogMessage(Log.Level.Verbose, "Request Processed and Response Sent", DateTime.UtcNow.Ticks);
            }
        }
Exemplo n.º 3
0
        protected virtual void ProcessRequest(TcpConnection socket)
        {
            Log.LogVerbose("Processing Request");
            
            var requestBuffer = s_pool.Rent(RequestBufferSize);
            var requestByteCount = socket.Receive(requestBuffer);

            if(requestByteCount == 0) {
                socket.Close();
                return;
            }

            var requestBytes = requestBuffer.Slice(0, requestByteCount);
            var request = HttpRequest.Parse(requestBytes);
            Log.LogRequest(request);

            using (var responseData = new ResponseBuffer()) {
                var response = new HttpResponse(responseData);
                WriteResponse(request, response);
                s_pool.Return(requestBuffer);

                // TODO: this whole thing about segment order is very bad. It needs to be designed.
                for (int index = 0; index < responseData.Count; index++) {
                    var segment = responseData[index];
                    if (segment.Id == 2) {
                        socket.Send(segment.Commited);
                    }
                }
                for (int index = 0; index < responseData.Count; index++) {
                    var segment = responseData[index];
                    if (segment.Id == 1) {
                        socket.Send(segment.Commited);
                    }
                }

                socket.Close();
            }

            if (Log.IsVerbose)
            {
                Log.LogMessage(Log.Level.Verbose, "Request Processed and Response Sent", DateTime.UtcNow.Ticks);
            }
        }