/// <summary>
 ///     ��Ԫ����ת��Ϊ�������ͻ�����
 /// </summary>
 /// <param name="instance">Ŀ�����</param>
 /// <param name="result">�������</param>
 /// <param name="container">������������</param>
 public override GetObjectResultTypes Process(object instance, GetObjectAnalyseResult result, INetworkDataContainer container)
 {
     short value;
     if(!container.TryReadInt16(out value)) return GetObjectResultTypes.NotEnoughData;
     result.SetValue(instance, value.ToLittleEndian());
     return GetObjectResultTypes.Succeed;
 }
示例#2
0
        /// <summary>
        ///     从元数据转换为第三方客户数据
        /// </summary>
        /// <param name="instance">目标对象</param>
        /// <param name="result">分析结果</param>
        /// <param name="container">网络数据容器</param>
        public override GetObjectResultTypes Process(object instance, GetObjectAnalyseResult result, INetworkDataContainer container)
        {
            short value;

            if (!container.TryReadInt16(out value))
            {
                return(GetObjectResultTypes.NotEnoughData);
            }
            result.SetValue(instance, value.ToLittleEndian());
            return(GetObjectResultTypes.Succeed);
        }
示例#3
0
        /// <summary>
        ///     将一个元数据转换为特定类型的对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="target">特定的对象</param>
        /// <param name="container">网络数据容器</param>
        /// <param name="value">如果解析成功, 则此字段为解析成功后的值</param>
        /// <param name="isInnerObject">
        ///     是否为内部对象
        ///     <para>* 如果此值被设置为false(默认值), 则会对当前类型尝试解析MessageIdentity字段</para>
        /// </param>
        /// <returns>返回一个解析后的状态</returns>
        /// <exception cref="ArgumentNullException">参数不能为空</exception>
        /// <exception cref="Exception">转换失败</exception>
        public static GetObjectResultTypes TryGetObject <T>(Type target, INetworkDataContainer container, out T value, bool isInnerObject = false)
        {
            value = default(T);
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            Dictionary <short, GetObjectAnalyseResult> result = Analyser.GetObjectAnalyser.Analyse(target);

            if (result == null)
            {
                return(GetObjectResultTypes.UnknownObjectType);
            }
            GetObjectResultTypes tmpFieldParsingResult;
            //create instance for new obj.
            Object       instance     = Activator.CreateInstance(target);
            ThriftObject thriftObject = instance as ThriftObject;

            if (thriftObject == null)
            {
                throw new Exception("Cannot convert target object to Intellect Object! #type: " + target.FullName);
            }

            #region Data Parsing.

            GetObjectAnalyseResult analyzeResult;
            if (!isInnerObject)
            {
                #region Message Identity.

                if (!result.TryGetValue(-1, out analyzeResult))
                {
                    throw new Exception("#Lost Thrift protocol object processor which it can handles type of MessageIdentity");
                }
                if (analyzeResult.HasCacheFinished)
                {
                    if ((tmpFieldParsingResult = analyzeResult.CacheProcess(instance, analyzeResult, container)) != GetObjectResultTypes.Succeed)
                    {
                        container.ResetOffset();
                        return(tmpFieldParsingResult);
                    }
                }
                else
                {
                    IThriftTypeProcessor processor = ThriftTypeProcessorMapping.Instance.GetProcessor(analyzeResult.Property.PropertyType);
                    if (processor == null)
                    {
                        throw new Exception("#Lost Thrift protocol object processor which it can handles type of MessageIdentity");
                    }
                    analyzeResult.CacheProcess     = processor.Process;
                    analyzeResult.HasEnoughData    = processor.HasEnoughData;
                    analyzeResult.HasCacheFinished = true;
                    if ((tmpFieldParsingResult = analyzeResult.CacheProcess(instance, analyzeResult, container)) != GetObjectResultTypes.Succeed)
                    {
                        container.ResetOffset();
                        return(tmpFieldParsingResult);
                    }
                }

                #endregion
            }

            while (true)
            {
                byte pType;
                //get property type.
                if (!container.TryReadByte(out pType))
                {
                    container.ResetOffset();
                    return(GetObjectResultTypes.NotEnoughData);
                }
                PropertyTypes propertyType = (PropertyTypes)pType;
                //succeed parsing binary data to a Thrift protocol object by finding Thrift protocol's STOP flag.
                if (propertyType == PropertyTypes.Stop)
                {
                    value = (T)instance;
                    return(GetObjectResultTypes.Succeed);
                }
                //get id.
                short id;
                if (!container.TryReadInt16(out id))
                {
                    container.ResetOffset();
                    return(GetObjectResultTypes.NotEnoughData);
                }
                id = id.ToLittleEndian();
                //get analyze result.
                if (!result.TryGetValue(id, out analyzeResult))
                {
                    throw new Exception(string.Format("Illegal data contract, non-exists id! #id: {0} \r\n#Formatted MSG info: {1}, data: \r\n{2}", id, instance, container.Dump()));
                }
                //set current property value to the target object.
                if ((tmpFieldParsingResult = InstanceHelper.SetInstance(thriftObject, analyzeResult, container)) != GetObjectResultTypes.Succeed)
                {
                    container.ResetOffset();
                    return(tmpFieldParsingResult);
                }
            }

            #endregion
        }