public async Task ProcessRequestAsync(HttpContext context, CancellationToken cancellationToken)
        {
            var transport = new TStreamTransport(context.Request.Body, context.Response.Body, Configuration);

            try
            {
                var intrans  = (InputTransportFactory != null) ? InputTransportFactory.GetTransport(transport) : transport;
                var outtrans = (OutputTransportFactory != null) ? OutputTransportFactory.GetTransport(transport) : transport;

                var input  = InputProtocolFactory.GetProtocol(intrans);
                var output = OutputProtocolFactory.GetProtocol(outtrans);

                while (await Processor.ProcessAsync(input, output, cancellationToken))
                {
                    if (!context.Response.HasStarted)  // oneway method called
                    {
                        await context.Response.Body.FlushAsync(cancellationToken);
                    }
                }
            }
            catch (TTransportException)
            {
                if (!context.Response.HasStarted)  // if something goes bust, let the client know
                {
                    context.Response.StatusCode = 500;
                }
            }
            finally
            {
                transport.Close();
            }
        }
Example #2
0
        public async Task ProcessRequestAsync(HttpContext context, CancellationToken cancellationToken)
        {
            var transport = new TStreamTransport(context.Request.Body, context.Response.Body);

            try
            {
                var intrans  = (InputTransportFactory != null) ? InputTransportFactory.GetTransport(transport) : transport;
                var outtrans = (OutputTransportFactory != null) ? OutputTransportFactory.GetTransport(transport) : transport;

                var input  = InputProtocolFactory.GetProtocol(intrans);
                var output = OutputProtocolFactory.GetProtocol(outtrans);

                while (await Processor.ProcessAsync(input, output, cancellationToken))
                {
                }
            }
            catch (TTransportException)
            {
                // Client died, just move on
                if (!context.Response.HasStarted)
                {
                    context.Response.StatusCode = 500;
                }
            }
            finally
            {
                transport.Close();
            }
        }
        public async Task ProcessRequestAsync(HttpContext context, CancellationToken cancellationToken)
        {
            var transport = new TStreamClientTransport(context.Request.Body, context.Response.Body);

            try
            {
                var input  = InputProtocolFactory.GetProtocol(transport);
                var output = OutputProtocolFactory.GetProtocol(transport);

                while (await Processor.ProcessAsync(input, output, cancellationToken))
                {
                }
            }
            catch (TTransportException)
            {
                // Client died, just move on
            }
            finally
            {
                transport.Close();
            }
        }
Example #4
0
        /// <summary>
        /// Loops on processing a client forever
        /// client will be a TTransport instance
        /// </summary>
        /// <param name="client"></param>
        private async Task ExecuteAsync(TTransport client)
        {
            var cancellationToken = ServerCancellationToken;

            using (client)
            {
                ITAsyncProcessor processor         = ProcessorFactory.GetAsyncProcessor(client, this);
                TTransport       inputTransport    = null;
                TTransport       outputTransport   = null;
                TProtocol        inputProtocol     = null;
                TProtocol        outputProtocol    = null;
                object           connectionContext = null;
                try
                {
                    try
                    {
                        inputTransport  = InputTransportFactory.GetTransport(client);
                        outputTransport = OutputTransportFactory.GetTransport(client);
                        inputProtocol   = InputProtocolFactory.GetProtocol(inputTransport);
                        outputProtocol  = OutputProtocolFactory.GetProtocol(outputTransport);

                        //Recover event handler (if any) and fire createContext server event when a client connects
                        if (ServerEventHandler != null)
                        {
                            connectionContext = await ServerEventHandler.CreateContextAsync(inputProtocol, outputProtocol, cancellationToken);
                        }

                        //Process client requests until client disconnects
                        while (!(stop || cancellationToken.IsCancellationRequested))
                        {
                            if (!await inputTransport.PeekAsync(cancellationToken))
                            {
                                break;
                            }

                            //Fire processContext server event
                            //N.B. This is the pattern implemented in C++ and the event fires provisionally.
                            //That is to say it may be many minutes between the event firing and the client request
                            //actually arriving or the client may hang up without ever makeing a request.
                            if (ServerEventHandler != null)
                            {
                                await ServerEventHandler.ProcessContextAsync(connectionContext, inputTransport, cancellationToken);
                            }

                            //Process client request (blocks until transport is readable)
                            if (!await processor.ProcessAsync(inputProtocol, outputProtocol, cancellationToken))
                            {
                                break;
                            }
                        }
                    }
                    catch (TTransportException)
                    {
                        //Usually a client disconnect, expected
                    }
                    catch (Exception x)
                    {
                        //Unexpected
                        LogError("Error: " + x);
                    }

                    //Fire deleteContext server event after client disconnects
                    if (ServerEventHandler != null)
                    {
                        await ServerEventHandler.DeleteContextAsync(connectionContext, inputProtocol, outputProtocol, cancellationToken);
                    }
                }
                finally
                {
                    //Close transports
                    inputTransport?.Close();
                    outputTransport?.Close();

                    // disposable stuff should be disposed
                    inputProtocol?.Dispose();
                    outputProtocol?.Dispose();
                    inputTransport?.Dispose();
                    outputTransport?.Dispose();
                }
            }
        }
Example #5
0
        public async Task <bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken = default)
        {
            if (iprot is TBinaryHeaderServerProtocol serverProtocolBinary)
            {
                serverProtocolBinary.MarkTFramedTransport(iprot);
                TMessage tMessage = await serverProtocolBinary.ReadMessageBeginAsync(cancellationToken);

                await serverProtocolBinary.ReadFieldZero(cancellationToken);

                IDictionary <string, string> headInfo = serverProtocolBinary.Head;

                string methodName = tMessage.Name;

                if (iprot.Transport is TSocketTransport socket)
                {
                    string hostAddress = socket.Host.MapToIPv4().ToString();
                }
                else if (iprot.Transport is TTlsSocketTransport socketTls)
                {
                    string hostAddress = socketTls.Host.MapToIPv4().ToString();
                }
                else if (iprot.Transport is TStreamTransport stream)
                {
                }
                else if (iprot.Transport is THttpTransport http)
                {
                    string hostAddress = http.RequestHeaders.Host;
                }
                else if (iprot.Transport is TNamedPipeTransport pipe)
                {
                }
                else if (iprot.Transport is TMemoryBufferTransport memory)
                {
                }

                //string traceId = headInfo.get(TRACE_ID.getValue());
                //string parentSpanId = headInfo.get(PARENT_SPAN_ID.getValue());
                //string isSampled = headInfo.get(IS_SAMPLED.getValue());
                //bool sampled = isSampled == null || bool.Parse(isSampled);

                //if (traceId != null && parentSpanId != null)
                //{
                //    //TraceUtils.startLocalTracer("rpc.thrift receive", traceId, parentSpanId, sampled);
                //    string methodName = tMessage.Name;
                //    //TraceUtils.submitAdditionalAnnotation(Constants.TRACE_THRIFT_METHOD, methodName);
                //    TTransport transport = iprot.Transport;

                //    //string hostAddress = ((TSocket)transport).getSocket().getRemoteSocketAddress().toString();
                //    //TraceUtils.submitAdditionalAnnotation(Constants.TRACE_THRIFT_SERVER, hostAddress);
                //}
                serverProtocolBinary.ResetTFramedTransport(iprot);
            }
            else if (iprot is TCompactHeaderServerProtocol serverProtocolCompact)
            {
            }
            else if (iprot is TJsonHeaderServerProtocol serverProtocolJson)
            {
            }

            bool result = await realProcessor.ProcessAsync(iprot, oprot, cancellationToken);

            if (iprot is TBinaryHeaderServerProtocol)
            {
                //TraceUtils.endAndSendLocalTracer();
            }
            return(result);
        }