예제 #1
0
 public void NativePoolBasics()
 {
     using (var pool = new NativeBufferPool(256, 10))
     {
         List <ByteSpan> buffers = new List <ByteSpan>();
         for (byte i = 0; i < 10; i++)
         {
             var buffer = pool.Rent();
             buffers.Add(buffer);
             for (int bi = 0; bi < buffer.Length; bi++)
             {
                 buffer[bi] = i;
             }
         }
         for (byte i = 0; i < 10; i++)
         {
             var buffer = buffers[i];
             for (int bi = 0; bi < buffer.Length; bi++)
             {
                 Assert.Equal(i, buffer[bi]);
             }
             pool.Return(buffer);
         }
     }
 }
예제 #2
0
        protected virtual void ProcessRequest(TcpConnection socket)
        {
            Log.LogVerbose("Processing Request");

            var buffer   = _buffers.Rent();
            var received = socket.Receive(buffer);

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

            var receivedBytes = buffer.Slice(0, received);

            if (Log.IsVerbose)
            {
                var text = Encoding.UTF8.GetString(receivedBytes.CreateArray());
                Console.WriteLine(text);
            }

            var request = HttpRequest.Parse(receivedBytes);

            if (Log.IsVerbose)
            {
                Log.LogMessage(Log.Level.Verbose, "\tMethod:       {0}", request.RequestLine.Method);
                Log.LogMessage(Log.Level.Verbose, "\tRequest-URI:  {0}", request.RequestLine.RequestUri.ToString());
                Log.LogMessage(Log.Level.Verbose, "\tHTTP-Version: {0}", request.RequestLine.Version);

                Log.LogMessage(Log.Level.Verbose, "\tHttp Headers:");
                foreach (var httpHeader in request.Headers)
                {
                    Log.LogMessage(Log.Level.Verbose, "\t\tName: {0}, Value: {1}", httpHeader.Key, httpHeader.Value);
                }

                LogRestOfRequest(request.Body);
            }

            HttpServerBuffer responseBytes = CreateResponse(request);

            _buffers.Return(buffer);

            // send response
            var segment = responseBytes;

            socket.Send(segment._buffer, segment._count);

            if (!request.RequestLine.IsKeepAlive())
            {
                socket.Close();
            }

            responseBytes.Return();
            if (Log.IsVerbose)
            {
                Log.LogMessage(Log.Level.Verbose, "Request Processed", DateTime.UtcNow.Ticks);
            }
        }
예제 #3
0
        protected virtual void ProcessRequest(TcpConnection socket)
        {
            Log.LogVerbose("Processing Request");

            var buffer   = _buffers.Rent();
            var received = socket.Receive(buffer);

            var receivedBytes = buffer.Slice(0, received);
            HttpServerBuffer responseBytes;

            // parse request
            HttpRequestLine requestLine;
            int             requestLineBytes;

            if (!HttpRequestParser.TryParseRequestLine(receivedBytes, out requestLine, out requestLineBytes))
            {
                Log.LogError("invalid request line");
                responseBytes = CreateResponseFor400(receivedBytes);
            }
            else
            {
                var restOfRequestBytes = receivedBytes.Slice(requestLineBytes);

                if (Log.IsVerbose)
                {
                    Log.LogMessage(Log.Level.Verbose, "\tMethod:       {0}", requestLine.Method);
                    Log.LogMessage(Log.Level.Verbose, "\tRequest-URI:  {0}", requestLine.RequestUri.ToString());
                    Log.LogMessage(Log.Level.Verbose, "\tHTTP-Version: {0}", requestLine.Version);
                    LogRestOfRequest(restOfRequestBytes);
                }

                responseBytes = CreateResponse(requestLine, restOfRequestBytes);
            }
            _buffers.Return(ref buffer);

            // send response
            var segment = responseBytes;

            socket.Send(segment._buffer, segment._count);
            socket.Close();
            responseBytes.Return();
            if (Log.IsVerbose)
            {
                Log.LogMessage(Log.Level.Verbose, "Request Processed", DateTime.UtcNow.Ticks);
            }
        }