/// <summary>
        ///     从元数据转换为第三方客户数据
        /// </summary>
        /// <param name="instance">目标对象</param>
        /// <param name="result">分析结果</param>
        /// <param name="container">网络数据容器</param>
        public override GetObjectResultTypes Process(object instance, GetObjectAnalyseResult result, INetworkDataContainer container)
        {
            byte keyType, valueType;
            int count;
            if (!container.TryReadByte(out keyType)) return GetObjectResultTypes.NotEnoughData;
            if (!container.TryReadByte(out valueType)) return GetObjectResultTypes.NotEnoughData;
            if (!container.TryReadInt32(out count)) return GetObjectResultTypes.NotEnoughData;
            count = count.ToLittleEndian();
            Dictionary<string, Cell> value = new Dictionary<string, Cell>(count);
            for (int i = 0; i < count; i++)
            {
                int stringCount;
                if (!container.TryReadInt32(out stringCount)) return GetObjectResultTypes.NotEnoughData;
                stringCount = stringCount.ToLittleEndian();
                string keyContent;
                if (!container.TryReadString(Encoding.UTF8, stringCount, out keyContent)) return GetObjectResultTypes.NotEnoughData;

                Cell valueContent;
                GetObjectResultTypes type = ThriftObjectEngine.TryGetObject(typeof(Cell), container, out valueContent, true);
                if (type != GetObjectResultTypes.Succeed) return type;
                value.Add(keyContent, valueContent);
            }
            result.SetValue(instance, value);
            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)
        {
            byte keyType, valueType;
            int  count;

            if (!container.TryReadByte(out keyType))
            {
                return(GetObjectResultTypes.NotEnoughData);
            }
            if (!container.TryReadByte(out valueType))
            {
                return(GetObjectResultTypes.NotEnoughData);
            }
            if (!container.TryReadInt32(out count))
            {
                return(GetObjectResultTypes.NotEnoughData);
            }
            count = count.ToLittleEndian();
            Dictionary <string, string> value = new Dictionary <string, string>(count);

            for (int i = 0; i < count; i++)
            {
                int stringCount;
                if (!container.TryReadInt32(out stringCount))
                {
                    return(GetObjectResultTypes.NotEnoughData);
                }
                stringCount = stringCount.ToLittleEndian();
                string keyContent;
                if (!container.TryReadString(Encoding.UTF8, stringCount, out keyContent))
                {
                    return(GetObjectResultTypes.NotEnoughData);
                }

                if (!container.TryReadInt32(out stringCount))
                {
                    return(GetObjectResultTypes.NotEnoughData);
                }
                stringCount = stringCount.ToLittleEndian();
                string valueContent;
                if (!container.TryReadString(Encoding.UTF8, stringCount, out valueContent))
                {
                    return(GetObjectResultTypes.NotEnoughData);
                }
                value.Add(keyContent, valueContent);
            }
            result.SetValue(instance, value);
            return(GetObjectResultTypes.Succeed);
        }
 /// <summary>
 ///     ��Ԫ����ת��Ϊ�������ͻ�����
 /// </summary>
 /// <param name="instance">Ŀ�����</param>
 /// <param name="result">�������</param>
 /// <param name="container">������������</param>
 public override GetObjectResultTypes Process(object instance, GetObjectAnalyseResult result, INetworkDataContainer container)
 {
     byte value;
     if(!container.TryReadByte(out value)) return GetObjectResultTypes.NotEnoughData;
     result.SetValue(instance, value);
     return GetObjectResultTypes.Succeed;
 }
        /// <summary>
        ///     从元数据转换为第三方客户数据
        /// </summary>
        /// <param name="instance">目标对象</param>
        /// <param name="result">分析结果</param>
        /// <param name="container">网络数据容器</param>
        public override GetObjectResultTypes Process(object instance, GetObjectAnalyseResult result, INetworkDataContainer container)
        {
            byte type;
            int  count;

            if (!container.TryReadByte(out type))
            {
                return(GetObjectResultTypes.NotEnoughData);
            }
            if (!container.TryReadInt32(out count))
            {
                return(GetObjectResultTypes.NotEnoughData);
            }
            count = count.ToLittleEndian();
            string[] value = new string[count];
            for (int i = 0; i < count; i++)
            {
                int length;
                if (!container.TryReadInt32(out length))
                {
                    return(GetObjectResultTypes.NotEnoughData);
                }
                length = length.ToLittleEndian();
                string content;
                if (!container.TryReadString(Encoding.UTF8, length, out content))
                {
                    return(GetObjectResultTypes.NotEnoughData);
                }
                value[i] = content;
            }
            result.SetValue(instance, value);
            return(GetObjectResultTypes.Succeed);
        }
        /// <summary>
        ///     从元数据转换为第三方客户数据
        /// </summary>
        /// <param name="instance">目标对象</param>
        /// <param name="result">分析结果</param>
        /// <param name="container">网络数据容器</param>
        public override GetObjectResultTypes Process(object instance, GetObjectAnalyseResult result, INetworkDataContainer container)
        {
            byte value;

            if (!container.TryReadByte(out value))
            {
                return(GetObjectResultTypes.NotEnoughData);
            }
            result.SetValue(instance, value);
            return(GetObjectResultTypes.Succeed);
        }
 /// <summary>
 ///     从元数据转换为第三方客户数据
 /// </summary>
 /// <param name="instance">目标对象</param>
 /// <param name="result">分析结果</param>
 /// <param name="container">网络数据容器</param>
 public override GetObjectResultTypes Process(object instance, GetObjectAnalyseResult result, INetworkDataContainer container)
 {
     byte type;
     int count;
     if (!container.TryReadByte(out type)) return GetObjectResultTypes.NotEnoughData;
     if (!container.TryReadInt32(out count)) return GetObjectResultTypes.NotEnoughData;
     count = count.ToLittleEndian();
     string[] value = new string[count];
     for (int i = 0; i < count; i++)
     {
         int length;
         if (!container.TryReadInt32(out length)) return GetObjectResultTypes.NotEnoughData;
         length = length.ToLittleEndian();
         string content;
         if (!container.TryReadString(Encoding.UTF8, length, out content)) return GetObjectResultTypes.NotEnoughData;
         value[i] = content;
     }
     result.SetValue(instance, value);
     return GetObjectResultTypes.Succeed;
 }
示例#7
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
        }