示例#1
0
        private Object[] ReadArguments(TProtocol inProtocol)
        {
            try
            {
                int             numArgs = _method.GetParameters().Length;
                Object[]        args    = new Object[numArgs];
                TProtocolReader reader  = new TProtocolReader(inProtocol);

                // Map incoming arguments from the ID passed in on the wire to the position in the
                // java argument list we expect to see a parameter with that ID.
                reader.ReadStructBegin();
                while (reader.NextField())
                {
                    short fieldId = reader.GetFieldId();

                    IThriftCodec codec = null;
                    if (!_parameterCodecs.TryGetValue(fieldId, out codec))
                    {
                        // unknown field
                        reader.SkipFieldData();
                    }
                    else
                    {
                        // Map the incoming arguments to an array of arguments ordered as the java
                        // code for the handler method expects to see them
                        args[_thriftParameterIdToCSharpArgument[fieldId]] = reader.ReadField(codec);
                    }
                }
                reader.ReadStructEnd();

                // Walk through our list of expected parameters and if no incoming parameters were
                // mapped to a particular expected parameter, fill the expected parameter slow with
                // the default for the parameter type.
                int argumentPosition = 0;
                foreach (ThriftFieldMetadata argument in _parameters)
                {
                    if (args[argumentPosition] == null)
                    {
                        Type argumentType = argument.ThriftType.CSharpType;

                        if (!argumentType.Equals(typeof(void)))
                        {
                            args[argumentPosition] = ThriftyUtilities.GetDefaultValue(argumentType);
                        }
                    }
                    argumentPosition++;
                }

                return(args);
            }
            catch (TProtocolException e)
            {
                // TProtocolException is the only recoverable exception
                // Other exceptions may have left the input stream in corrupted state so we must
                // tear down the socket.
                throw new TApplicationException(TApplicationException.ExceptionType.ProtocolError, e.Message);
            }
        }
        public override T Read(TProtocol protocol)
        {
            TProtocolReader reader = new TProtocolReader(protocol);

            reader.ReadStructBegin();

            IDictionary <short, Object> data = new Dictionary <short, Object>(_metadata.Fields.Count());

            while (reader.NextField())
            {
                short fieldId = reader.GetFieldId();

                // do we have a codec for this field
                IThriftCodec codec;
                if (!_fields.TryGetValue(fieldId, out codec))
                {
                    reader.SkipFieldData();
                    continue;
                }

                // is this field readable
                ThriftFieldMetadata field = _metadata.GetField(fieldId);
                if (field.ReadOnly || field.Type != FieldKind.ThriftField)
                {
                    reader.SkipFieldData();
                    continue;
                }

                // read the value
                Object value = reader.ReadField(codec);
                if (value == null)
                {
                    if (field.Required == ThriftFieldAttribute.Requiredness.Required)
                    {
                        throw new TProtocolException($"'{field.Name}(id: {fieldId})' is a required field, but it was not set, thrift type: '{field.ThriftType.ToString()}', codec: '{codec.GetType().Name}'");
                    }
                    else
                    {
                        continue;
                    }
                }
                data[fieldId] = value;
            }
            reader.ReadStructEnd();

            // build the struct
            return(ConstructStruct(data));
        }
示例#3
0
        private Object ReadResponse(TProtocol inProtocol)
        {
            TProtocolReader reader = new TProtocolReader(inProtocol);

            reader.ReadStructBegin();
            Object    results   = null;
            Exception exception = null;

            while (reader.NextField())
            {
                if (reader.GetFieldId() == 0)
                {
                    results = reader.ReadField(_successCodec);
                }
                else
                {
                    IThriftCodec exceptionCodec = null;
                    if (_exceptionCodecs.TryGetValue(reader.GetFieldId(), out exceptionCodec))
                    {
                        exception = (Exception)reader.ReadField(exceptionCodec);
                    }
                    else
                    {
                        reader.SkipFieldData();
                    }
                }
            }
            reader.ReadStructEnd();
            inProtocol.ReadMessageEnd();

            if (exception != null)
            {
                throw exception;
            }

            if (_successCodec.Type == ThriftType.Void)
            {
                // TODO: check for non-null return from a void function?
                return(null);
            }

            if (results == null)
            {
                throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, $"{this.QualifiedName} failed: unknown result");
            }
            return(results);
        }