Ejemplo n.º 1
0
        public ThriftMethodHandler(ThriftMethodMetadata methodMetadata, ThriftCodecManager codecManager)
        {
            this.QualifiedName         = methodMetadata.QualifiedName;
            this.Name                  = methodMetadata.Name;
            this._invokeAsynchronously = methodMetadata.IsAsync;
            this._oneway               = !_invokeAsynchronously && methodMetadata.IsOneWay;

            ParameterHandler[] parameters = new ParameterHandler[methodMetadata.Parameters.Count()];

            foreach (var fieldMetadata in methodMetadata.Parameters)
            {
                ThriftParameterInjection parameter = (ThriftParameterInjection)fieldMetadata.Injections.First();

                ParameterHandler handler = new ParameterHandler(
                    fieldMetadata.Id,
                    fieldMetadata.Name,
                    codecManager.GetCodec(fieldMetadata.ThriftType));

                parameters[parameter.ParameterIndex] = handler;
            }
            this._parameterCodecs = parameters;

            var builder = ImmutableDictionary.CreateBuilder <short, IThriftCodec>();

            foreach (var entry in methodMetadata.Exceptions)
            {
                builder.Add(entry.Key, codecManager.GetCodec(entry.Value));
            }
            _exceptionCodecs = builder.ToImmutableDictionary();

            // get the thrift codec for the return value
            _successCodec = codecManager.GetCodec(methodMetadata.ReturnType);
        }
Ejemplo n.º 2
0
 public void DeclaredUserException(Exception t, IThriftCodec exceptionCodec)
 {
     foreach (var entry in contexts)
     {
         entry.Key.DeclaredUserException(entry.Value, methodName, t, exceptionCodec);
     }
 }
Ejemplo n.º 3
0
        public SetThriftCodec(ThriftType type, IThriftCodec <T> elementCodec)
        {
            Guard.ArgumentNotNull(type, nameof(type));
            Guard.ArgumentNotNull(elementCodec, nameof(elementCodec));

            this._type         = type;
            this._elementCodec = elementCodec;
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
0
        public CoercionThriftCodec(IThriftCodec <T> codec, TypeCoercion typeCoercion)
        {
            Guard.ArgumentNotNull(codec, nameof(codec));
            Guard.ArgumentNotNull(typeCoercion, nameof(typeCoercion));

            this._codec        = codec;
            this._typeCoercion = typeCoercion;
            this.Type          = typeCoercion.ThriftType;
        }
Ejemplo n.º 6
0
        public ListThriftCodec(ThriftType type, IThriftCodec <T> elementCodec)
        {
            Guard.ArgumentNotNull(type, nameof(type));
            Guard.ArgumentNotNull(elementCodec, nameof(elementCodec));

            this.type         = type;
            this.elementCodec = elementCodec;
            this._isArray     = type.CSharpType.IsArray;
        }
Ejemplo n.º 7
0
        public MapThriftCodec(ThriftType type, IThriftCodec <K> keyCodec, IThriftCodec <V> valueCodec)
        {
            Guard.ArgumentNotNull(type, nameof(type));
            Guard.ArgumentNotNull(keyCodec, nameof(keyCodec));
            Guard.ArgumentNotNull(valueCodec, nameof(valueCodec));

            this._thriftType = type;
            this._keyCodec   = keyCodec;
            this._valueCodec = valueCodec;
        }
Ejemplo n.º 8
0
        public void WriteField(String name, short id, IThriftCodec codec, Object value)
        {
            if (value == null)
            {
                return;
            }

            Protocol.WriteFieldBegin(new TField(name, codec.Type.ProtocolType.ToTType(), id));
            codec.WriteObject(value, Protocol);
            Protocol.WriteFieldEnd();
        }
Ejemplo n.º 9
0
        public void WriteMapField <K, V>(String name, short id, IThriftCodec <IDictionary <K, V> > codec, IDictionary <K, V> map)
        {
            if (map == null)
            {
                return;
            }

            Protocol.WriteFieldBegin(new TField(name, TType.Map, id));
            codec.Write(map, Protocol);
            Protocol.WriteFieldEnd();
        }
Ejemplo n.º 10
0
        public void WriteListField <E>(String name, short id, IThriftCodec <IList <E> > codec, IList <E> list)
        {
            if (list == null)
            {
                return;
            }

            Protocol.WriteFieldBegin(new TField(name, TType.List, id));
            codec.Write(list, Protocol);
            Protocol.WriteFieldEnd();
        }
Ejemplo n.º 11
0
        public void WriteSetField <E>(String name, short id, IThriftCodec <ISet <E> > codec, ISet <E> set)
        {
            if (set == null)
            {
                return;
            }

            Protocol.WriteFieldBegin(new TField(name, TType.Set, id));
            codec.Write(set, Protocol);
            Protocol.WriteFieldEnd();
        }
Ejemplo n.º 12
0
        public void WriteStructField <T>(String name, short id, IThriftCodec <T> codec, T fieldValue)
        {
            if (fieldValue == null)
            {
                return;
            }

            Protocol.WriteFieldBegin(new TField(name, TType.Struct, id));
            codec.Write(fieldValue, Protocol);
            Protocol.WriteFieldEnd();
        }
Ejemplo n.º 13
0
        public T ReadStructField <T>(IThriftCodec <T> codec)
        {
            if (!CheckReadState(TType.Struct))
            {
                return(default(T));
            }
            currentField = null;
            T fieldValue = codec.Read(this.Protocol);

            Protocol.ReadFieldEnd();
            return(fieldValue);
        }
Ejemplo n.º 14
0
        public IEnumerable <E> ReadSetField <E>(IThriftCodec <HashSet <E> > setCodec)
        {
            if (!CheckReadState(TType.Set))
            {
                return(null);
            }
            currentField = null;
            IEnumerable <E> fieldValue = setCodec.Read(this.Protocol);

            Protocol.ReadFieldEnd();
            return(fieldValue);
        }
Ejemplo n.º 15
0
        public List <E> ReadListField <E>(IThriftCodec <List <E> > listCodec)
        {
            if (!CheckReadState(TType.List))
            {
                return(null);
            }
            currentField = null;
            List <E> read = listCodec.Read(this.Protocol);

            Protocol.ReadFieldEnd();
            return(read);
        }
Ejemplo n.º 16
0
        public IDictionary <K, V> ReadMapField <K, V>(IThriftCodec <Dictionary <K, V> > mapCodec)
        {
            if (!CheckReadState(TType.Map))
            {
                return(null);
            }
            currentField = null;
            IDictionary <K, V> fieldValue = mapCodec.Read(this.Protocol);

            this.Protocol.ReadFieldEnd();
            return(fieldValue);
        }
Ejemplo n.º 17
0
        private IThriftCodec <T> GetCodecFromCache()
        {
            IThriftCodec <T> codec = codecManager.GetCachedCodecIfPresent <T>();

            if (codec == null)
            {
                throw new ThriftyException(
                          "Tried to encodec/decode using a DelegateCodec before the target codec was " +
                          "built (likely a bug in recursive type support)");
            }
            return(codec);
        }
Ejemplo n.º 18
0
        public void WriteEnumField <T>(String name, short id, IThriftCodec <T> codec, T enumValue)
            where T : struct
        {
            if (enumValue.Equals(default(T)))
            {
                return;
            }

            Protocol.WriteFieldBegin(new TField(name, TType.I32, id));
            codec.Write(enumValue, Protocol);
            Protocol.WriteFieldEnd();
        }
Ejemplo n.º 19
0
        public Object ReadField(IThriftCodec codec)
        {
            if (!CheckReadState(codec.Type.ProtocolType.ToTType()))
            {
                return(null);
            }
            currentField = null;
            Object fieldValue = codec.ReadObject(Protocol);

            Protocol.ReadFieldEnd();
            return(fieldValue);
        }
Ejemplo n.º 20
0
        public ThriftMethodProcessor(
            Func <IRequestContext, Object> service,
            String serviceName,
            ThriftMethodMetadata methodMetadata,
            ThriftCodecManager codecManager,
            ILoggerFactory loggerFactory = null)
        {
            Guard.ArgumentNotNull(service, nameof(service));

            _logger = loggerFactory?.CreateLogger <ThriftMethodProcessor>() ?? (ILogger)NullLogger.Instance;

            this._serviceFactory   = service;
            this.ServiceName       = serviceName;
            this.Name              = methodMetadata.Name;
            this.QualifiedName     = methodMetadata.QualifiedName;
            this._resultStructName = $"{this.Name}_result";
            this._method           = methodMetadata.Method;
            this._oneway           = methodMetadata.IsOneWay && !methodMetadata.IsAsync;
            this._parameters       = methodMetadata.Parameters;

            var builder = ImmutableDictionary.CreateBuilder <short, IThriftCodec>();

            foreach (ThriftFieldMetadata fieldMetadata in methodMetadata.Parameters)
            {
                builder.Add(fieldMetadata.Id, codecManager.GetCodec(fieldMetadata.ThriftType));
            }
            this._parameterCodecs = builder.ToImmutableDictionary();

            // Build a mapping from thrift parameter ID to a position in the formal argument list
            var   parameterOrderingBuilder = ImmutableDictionary.CreateBuilder <short, short>();
            short argumentPosition         = 0;

            foreach (ThriftFieldMetadata fieldMetadata in methodMetadata.Parameters)
            {
                parameterOrderingBuilder.Add(fieldMetadata.Id, argumentPosition++);
            }
            _thriftParameterIdToCSharpArgument = parameterOrderingBuilder.ToImmutableDictionary();

            var exceptions = ImmutableDictionary.CreateBuilder <Type, ExceptionProcessor>();

            foreach (var entry in methodMetadata.Exceptions)
            {
                ExceptionProcessor processor = new ExceptionProcessor(entry.Key, codecManager.GetCodec(entry.Value));
                exceptions.Add(entry.Value.CSharpType, processor);
            }
            this._exceptionCodecs = exceptions.ToImmutableDictionary();

            this._successCodec = codecManager.GetCodec(methodMetadata.ReturnType);
        }
Ejemplo n.º 21
0
        public void WriteSet <T>(IThriftCodec <T> elementCodec, ISet <T> set)
        {
            if (set == null)
            {
                return;
            }

            Protocol.WriteSetBegin(new TSet(elementCodec.Type.ProtocolType.ToTType(), set.Count));

            foreach (T element in set)
            {
                elementCodec.Write(element, Protocol);
            }

            Protocol.WriteSetEnd();
        }
Ejemplo n.º 22
0
        public void WriteList <T>(IThriftCodec <T> elementCodec, IEnumerable <T> list)
        {
            if (list == null)
            {
                return;
            }

            Protocol.WriteListBegin(new TList(elementCodec.Type.ProtocolType.ToTType(), list.Count()));

            foreach (T element in list)
            {
                elementCodec.Write(element, Protocol);
            }

            Protocol.WriteListEnd();
        }
Ejemplo n.º 23
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);
        }
Ejemplo n.º 24
0
        public void WriteMap <K, V>(IThriftCodec <K> keyCodec, IThriftCodec <V> valueCodec, IDictionary <K, V> map)
        {
            if (map == null)
            {
                return;
            }

            Protocol.WriteMapBegin(new TMap(keyCodec.Type.ProtocolType.ToTType(), valueCodec.Type.ProtocolType.ToTType(), map.Count));

            foreach (var entry in map)
            {
                keyCodec.Write(entry.Key, Protocol);
                valueCodec.Write(entry.Value, Protocol);
            }

            Protocol.WriteMapEnd();
        }
Ejemplo n.º 25
0
        private void WriteResponse(TProtocol outProtocol,
                                   int sequenceId,
                                   TMessageType responseType,
                                   String responseFieldName,
                                   short responseFieldId,
                                   IThriftCodec responseCodec,
                                   Object result)
        {
            outProtocol.WriteMessageBegin(new TMessage(this.QualifiedName, responseType, sequenceId));

            TProtocolWriter writer = new TProtocolWriter(outProtocol);

            writer.WriteStructBegin(_resultStructName);
            writer.WriteField(responseFieldName, (short)responseFieldId, responseCodec, result);
            writer.WriteStructEnd();

            outProtocol.WriteMessageEnd();
            outProtocol.Transport.Flush();
        }
Ejemplo n.º 26
0
        public IList <E> ReadList <E>(IThriftCodec <E> elementCodec)
        {
            TList     tList = Protocol.ReadListBegin();
            IList <E> list  = new List <E>();

            for (int i = 0; i < tList.Count; i++)
            {
                try
                {
                    E element = elementCodec.Read(Protocol);
                    list.Add(element);
                }
                catch (Exception e)
                {
                    // continue
                    e.ThrowIfNecessary();
                }
            }
            Protocol.ReadListEnd();
            return(list);
        }
Ejemplo n.º 27
0
        public ISet <E> ReadSet <E>(IThriftCodec <E> elementCodec)
        {
            TSet     tSet = Protocol.ReadSetBegin();
            ISet <E> set  = new HashSet <E>();

            for (int i = 0; i < tSet.Count; i++)
            {
                try
                {
                    E element = elementCodec.Read(Protocol);
                    set.Add(element);
                }
                catch (ArgumentException e)
                {
                    // continue
                    e.ThrowIfNecessary();
                }
            }
            Protocol.ReadSetEnd();
            return(set);
        }
Ejemplo n.º 28
0
        public T ReadEnumField <T>(IThriftCodec <T> enumCodec)
            where T : struct
        {
            if (!CheckReadState(TType.I32))
            {
                return(default(T));
            }
            currentField = null;
            T fieldValue = default(T);

            try
            {
                fieldValue = enumCodec.Read(Protocol);
            }
            catch (ArgumentException)
            {
                // return null
            }
            Protocol.ReadFieldEnd();
            return(fieldValue);
        }
Ejemplo n.º 29
0
        public IDictionary <K, V> ReadMap <K, V>(IThriftCodec <K> keyCodec, IThriftCodec <V> valueCodec)
        {
            TMap tMap             = Protocol.ReadMapBegin();
            Dictionary <K, V> map = new Dictionary <K, V>();

            for (int i = 0; i < tMap.Count; i++)
            {
                try
                {
                    K key   = keyCodec.Read(this.Protocol);
                    V value = valueCodec.Read(this.Protocol);
                    map[key] = value;
                }
                catch (Exception e)
                {
                    // continue
                    e.ThrowIfNecessary();
                }
            }
            Protocol.ReadMapEnd();
            return(map);
        }
Ejemplo n.º 30
0
 public virtual void DeclaredUserException(Object o, String methodName, Exception t, IThriftCodec exceptionCodec)
 {
 }