Ejemplo n.º 1
0
        public async Task RequestProcessing()
        {
            //Listener.AcceptSocketAsync
            using (TcpClient client = await TcpListener.AcceptTcpClientAsync().ConfigureAwait(false))
            {
                Logger.LogInformation($"New connection from: {client.Client.RemoteEndPoint}");
                // Get a stream object for reading and writing
                using (NetworkStream netStream = client.GetStream())
                {
                    // Stream Checks =================================================================
                    if (!netStream.CanRead)
                    {
                        Logger.LogCritical("Can Not Read Stream".ToErrorString(this));
                        netStream.Close();
                        client.Close();
                        return;
                    }

                    if (!netStream.CanWrite)
                    {
                        Logger.LogCritical("Can Not Write To The Stream".ToErrorString(this));
                        netStream.Close();
                        client.Close();
                        return;
                    }

                    // Stream Checks =================================================================

                    ExecutedCommand executedCommand = null;
                    Request         request         = null;
                    Exception       exception       = null;



                    try
                    {
#if DEBUG
                        request = await Request.BuildRequest(netStream, Settings.RequestSettings, ServiceProvider.GetRequiredService <ILogger <Request> >()).ConfigureAwait(false);

                        //request.LogPacket();
#else
                        request = await Request.BuildRequest(netStream, Settings.RequestSettings).ConfigureAwait(false);
#endif
                    }
                    catch (MalformedRequestException MalformedRequestException)
                    {
                        exception = MalformedRequestException;
                    }

                    if (request is not null && RequestedEndpoint(request) is ExecutedCommand executedCommand1)
                    {
                        executedCommand = executedCommand1;
                    }

                    var response = new Response(Settings.ResponseSettings);

                    var middleware = ContextBuilder.CreateContext(executedCommand?.ClassExecuted, Middleware, netStream, request, response, ServiceProvider);

                    object bodyContent;
                    bool   isReturnTypeVoid = false;

                    if (exception is not null)
                    {
                        bodyContent = await middleware.BadRequest(exception).ConfigureAwait(false);
                    }
                    else if (executedCommand is null)
                    {
                        bodyContent = await middleware.NotFound(request).ConfigureAwait(false);
                    }
                    else
                    {
                        try
                        {
                            var temp = await middleware.ActionExecuting(executedCommand).ConfigureAwait(false);

                            isReturnTypeVoid = temp.IsReturnTypeVoid;
                            bodyContent      = temp.ReturnValue;
                        }

                        catch (IOException)
                        {
                            throw;
                        }
                        catch (Exception ex)
                        {
                            bodyContent = await middleware.InternalServerError(ex).ConfigureAwait(false);
                        }
                    }

                    await middleware.Context.WriteBodyAsync(isReturnTypeVoid, bodyContent).ConfigureAwait(false);
                }
            }
        }