Example #1
0
            public async Task <bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken)
            {
                try
                {
                    var msg = await iprot.ReadMessageBeginAsync(cancellationToken);

                    ProcessFunction fn;
                    processMap_.TryGetValue(msg.Name, out fn);

                    if (fn == null)
                    {
                        await TProtocolUtil.SkipAsync(iprot, TType.Struct, cancellationToken);

                        await iprot.ReadMessageEndAsync(cancellationToken);

                        var x = new TApplicationException(TApplicationException.ExceptionType.UnknownMethod, "Invalid method name: '" + msg.Name + "'");
                        await oprot.WriteMessageBeginAsync(new TMessage(msg.Name, TMessageType.Exception, msg.SeqID), cancellationToken);

                        await x.WriteAsync(oprot, cancellationToken);

                        await oprot.WriteMessageEndAsync(cancellationToken);

                        await oprot.Transport.FlushAsync(cancellationToken);

                        return(true);
                    }

                    await fn(msg.SeqID, iprot, oprot, cancellationToken);
                }
                catch (IOException)
                {
                    return(false);
                }

                return(true);
            }
Example #2
0
        public async Task <bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(await Task.FromCanceled <bool>(cancellationToken));
            }

            try
            {
                var message = await iprot.ReadMessageBeginAsync(cancellationToken);

                if (message.Name == "thrift.surging")
                {
                    var TransportMessage = await iprot.ReadBinaryAsync(cancellationToken);

                    await iprot.ReadMessageEndAsync(cancellationToken);

                    await _serverHandler.ChannelRead(oprot, _transportMessageDecoder.Decode(TransportMessage));

                    return(true);
                }
                if ((message.Type != TMessageType.Call) && (message.Type != TMessageType.Oneway))
                {
                    await FailAsync(oprot, message, TApplicationException.ExceptionType.InvalidMessageType,
                                    "Message exType CALL or ONEWAY expected", cancellationToken);

                    return(false);
                }

                // Extract the service name
                var index = message.Name.IndexOf(TMultiplexedProtocol.Separator, StringComparison.Ordinal);
                if (index < 0)
                {
                    await FailAsync(oprot, message, TApplicationException.ExceptionType.InvalidProtocol,
                                    $"Service name not found in message name: {message.Name}. Did you forget to use a TMultiplexProtocol in your client?",
                                    cancellationToken);

                    return(false);
                }

                // Create a new TMessage, something that can be consumed by any TProtocol
                var serviceName = message.Name.Substring(0, index);
                ITAsyncProcessor actualProcessor;
                if (!_serviceProcessorMap.TryGetValue(serviceName, out actualProcessor))
                {
                    await FailAsync(oprot, message, TApplicationException.ExceptionType.InternalError,
                                    $"Service name not found: {serviceName}. Did you forget to call RegisterProcessor()?",
                                    cancellationToken);

                    return(false);
                }

                // Create a new TMessage, removing the service name
                var newMessage = new TMessage(
                    message.Name.Substring(serviceName.Length + TMultiplexedProtocol.Separator.Length),
                    message.Type,
                    message.SeqID);

                // Dispatch processing to the stored processor
                return
                    (await
                     actualProcessor.ProcessAsync(new StoredMessageProtocol(iprot, newMessage), oprot,
                                                  cancellationToken));
            }
            catch (IOException)
            {
                return(false); // similar to all other processors
            }
        }