Beispiel #1
0
        private static void DecodingBasicType(BinaryReader br, ref object obj, TTLVStructNode node, int valueLength, TypeCode valueBufferTypeCode)
        {
            byte[] valueBuffer = new byte[valueLength];
            br.Read(valueBuffer, 0, valueBuffer.Length);
            object value = TTLVCommon.ConvterBack(valueBuffer, valueBufferTypeCode, 0, valueLength);

            SetTTLVNodeValue(obj, node, value);
        }
Beispiel #2
0
 private static void AddTTLVNodeValue(List <byte> buffer, TTLVStructNode node, byte[] valueBuffer)
 {
     _codec.WriteNode(buffer, node.Tag, node.TypeCode, valueBuffer);
     //buffer.AddRange(BitConverter.GetBytes(node.Tag));
     //buffer.Add((byte)node.TypeCode);
     //buffer.AddRange(BitConverter.GetBytes(valueBuffer.Length));
     //buffer.AddRange(valueBuffer);
 }
Beispiel #3
0
        private static void EncodingObject(List <byte> buffer, TTLVStructNode node, object objValue)
        {
            List <byte> buffer2 = new List <byte>();
            var         childs  = GetTTLVStructNodeChilds(node);

            PrimitiveEncoding(buffer2, childs, objValue);
            AddTTLVNodeValue(buffer, node, buffer2.ToArray());
        }
Beispiel #4
0
        private static void DecodingConvert(BinaryReader br, ref object obj, TTLVStructNode node, int valueLength)
        {
            byte[] valueBuffer = new byte[valueLength];
            br.Read(valueBuffer, 0, valueBuffer.Length);
            object memberValue = node.GetValue(obj);
            object value       = node.Converter.ConvertBackObject(node.Tag, valueBuffer, memberValue, node.GetMemberType(), node.ConverterPara);

            SetTTLVNodeValue(obj, node, value);
        }
Beispiel #5
0
        private static void InitConverterNode(TTLVAttribute ttlvAttribute, TTLVStructNode node, Type eleType)
        {
            if (ttlvAttribute.ConverterType.GetInterface(_ttlvIConverterTypeName) == null)
            {
                throw new ArgumentException(string.Format("转换器类型[{0}]未实现[{1}]接口", ttlvAttribute.ConverterType.FullName, _ttlvIConverterTypeName));
            }

            node.Converter     = CreateTTLVConverter(ttlvAttribute);
            node.ConverterPara = ttlvAttribute.ConverterPara;
            node.TTLVType      = TTLVType.Convert;
            node.ElementType   = eleType;
        }
Beispiel #6
0
        private static void EncodingCollection(List <byte> buffer, TTLVStructNode node, object collectionObj)
        {
            List <byte> buffer2    = new List <byte>();
            var         collection = (IEnumerable)collectionObj;

            foreach (var item in collection)
            {
                EncodingObject(buffer2, node, item);
            }

            AddTTLVNodeValue(buffer, node, buffer2.ToArray());
        }
Beispiel #7
0
        private static void DecodingIList(BinaryReader br, ref object obj, TTLVStructNode node, int valueLength)
        {
            var list = (IList)node.GetValue(obj);

            if (list == null)
            {
                list = (IList)node.CreateMemberInstance();
                SetTTLVNodeValue(obj, node, list);
            }

            DecodingCollection(list, br, node, valueLength);
        }
Beispiel #8
0
        private static void DecodingArray(BinaryReader br, ref object obj, TTLVStructNode node, int valueLength)
        {
            var list = new List <object>();

            DecodingCollection(list, br, node, valueLength);
            Array array = Array.CreateInstance(node.ElementType, list.Count);

            for (int i = 0; i < array.Length; i++)
            {
                array.SetValue(list[i], i);
            }

            SetTTLVNodeValue(obj, node, array);
        }
Beispiel #9
0
        private static List <TTLVStructNode> GetTTLVStructNodeChilds(TTLVStructNode node)
        {
            List <TTLVStructNode> childs;

            if (node.RefNode != null)
            {
                childs = node.RefNode.Childs;
            }
            else
            {
                childs = node.Childs;
            }

            return(childs);
        }
Beispiel #10
0
        private static void DecodingObject(BinaryReader br, ref object obj, TTLVStructNode node, int valueLength)
        {
            var obj2 = node.GetValue(obj);

            if (obj2 == null)
            {
                obj2 = node.CreateMemberInstance();
                SetTTLVNodeValue(obj, node, obj2);
            }

            var endPosition = br.BaseStream.Position + valueLength;
            var childs      = GetTTLVStructNodeChilds(node);

            PrimitiveDecoding(br, endPosition, ref obj2, childs);
            br.BaseStream.Position = endPosition;
        }
Beispiel #11
0
        private static void PrimitiveDecoding(BinaryReader br, long endPosition, ref object obj, List <TTLVStructNode> childs)
        {
            var nodeInfo = new TTLVNodeInfo();

            while (br.BaseStream.Position < endPosition)
            {
                //int tag = br.ReadInt32();
                //TypeCode valueBufferTypeCode = (TypeCode)br.ReadByte();
                //int length = br.ReadInt32();
                _codec.ReadNodeInfo(br, nodeInfo);
                int      length = nodeInfo.Length;
                TypeCode valueBufferTypeCode = nodeInfo.Type;

                TTLVStructNode node = childs.Find(t => { return(t.Tag == nodeInfo.Tag); });
                if (node == null || !node.CheckCompatible(valueBufferTypeCode))
                {
                    br.BaseStream.Position += length;
                    continue;
                }

                switch (node.TTLVType)
                {
                case TTLVType.BasicType:
                    DecodingBasicType(br, ref obj, node, length, valueBufferTypeCode);
                    break;

                case TTLVType.Convert:
                    DecodingConvert(br, ref obj, node, length);
                    break;

                case TTLVType.Object:
                    DecodingObject(br, ref obj, node, length);
                    break;

                case TTLVType.Array:
                    DecodingArray(br, ref obj, node, length);
                    break;

                case TTLVType.IList:
                    DecodingIList(br, ref obj, node, length);
                    break;

                default:
                    throw new NotImplementedException(string.Format("未知类型", node.TTLVType.ToString()));
                }
            }
        }
Beispiel #12
0
        private static void DecodingCollection(IList list, BinaryReader br, TTLVStructNode node, int valueLength)
        {
            TTLVCommon.CheckHasNoParaConstructor(node.ElementType);
            var endPosition = br.BaseStream.Position + valueLength;
            var nodeInfo    = new TTLVNodeInfo();

            while (br.BaseStream.Position < endPosition)
            {
                //int tag = br.ReadInt32();
                //TypeCode valueBufferTypeCode = (TypeCode)br.ReadByte();
                //int length = br.ReadInt32();
                _codec.ReadNodeInfo(br, nodeInfo);

                object instance = Activator.CreateInstance(node.ElementType);
                PrimitiveDecoding(br, br.BaseStream.Position + nodeInfo.Length, ref instance, node.Childs);
                list.Add(instance);
            }

            br.BaseStream.Position = endPosition;
        }
Beispiel #13
0
        private static TTLVStructNode GetTypeStructTree(Type type)
        {
            TTLVStructNode typeStructTree;

            if (_typeStructTreeDic.TryGetValue(type, out typeStructTree))
            {
                return(typeStructTree);
            }

            lock (_typeStructTreeDic)
            {
                if (_typeStructTreeDic.TryGetValue(type, out typeStructTree))
                {
                    return(typeStructTree);
                }

                typeStructTree = new TTLVStructNode(0, TypeCode.Empty, null, null);
                var allNodeDic = new Dictionary <Type, TTLVStructNode>();
                CreateTypeStructTree(allNodeDic, typeStructTree, type);
                _typeStructTreeDic[type] = typeStructTree;
                return(typeStructTree);
            }
        }
Beispiel #14
0
        private static void CreateTypeFieldStructTree(Dictionary <Type, TTLVStructNode> allNodeDic, TTLVStructNode parentNode, Type type)
        {
            FieldInfo[] fieldInfoArr = type.GetFields(_memberAcessFlags);
            foreach (FieldInfo fieldInfo in fieldInfoArr)
            {
                object[] objAtts = fieldInfo.GetCustomAttributes(_ttlvAttributeType, true);
                if (objAtts.Length < 1)
                {
                    //未标记,忽略
                    continue;
                }

                var typeCode      = Type.GetTypeCode(fieldInfo.FieldType);
                var ttlvAttribute = (TTLVAttribute)objAtts[0];
                var node          = new TTLVStructNode(ttlvAttribute.Tag, typeCode, fieldInfo);

                if (typeCode == TypeCode.Object)
                {
                    if (ttlvAttribute.ConverterType != null)
                    {
                        //优先使用自定义转换
                        InitConverterNode(ttlvAttribute, node, fieldInfo.FieldType);
                    }
                    else
                    {
                        InitNoConverterNode(allNodeDic, fieldInfo, ttlvAttribute, node);
                    }
                }
                else
                {
                    node.TTLVType = TTLVType.BasicType;
                }

                parentNode.Childs.Add(node);
            }
        }
Beispiel #15
0
 private static void SetTTLVNodeValue(object obj, TTLVStructNode node, object value)
 {
     node.SetValue(obj, value);
 }
Beispiel #16
0
 private static void CreateTypeStructTree(Dictionary <Type, TTLVStructNode> allNodeDic, TTLVStructNode parentNode, Type type)
 {
     CreateTypePropertyStructTree(allNodeDic, parentNode, type); //属性
     CreateTypeFieldStructTree(allNodeDic, parentNode, type);    //字段
 }
Beispiel #17
0
        private static void EncodingConvert(List <byte> buffer, TTLVStructNode node, object objValue)
        {
            var valueBuffer = node.Converter.ConvertToBytes(node.Tag, objValue, node.ElementType, node.ConverterPara);

            AddTTLVNodeValue(buffer, node, valueBuffer);
        }
Beispiel #18
0
 private static void EncodingBasicType(List <byte> buffer, TTLVStructNode node, object objValue)
 {
     byte[] valueBuffer = TTLVCommon.ConvertToBytes(node.TypeCode, objValue);
     AddTTLVNodeValue(buffer, node, valueBuffer);
 }
Beispiel #19
0
        private static void InitNoConverterNode(Dictionary <Type, TTLVStructNode> allNodeDic, FieldInfo fieldInfo, TTLVAttribute ttlvAttribute, TTLVStructNode node)
        {
            switch (ttlvAttribute.PropertyType)
            {
            case TTLVPropertyType.Collection:
                Type     eleType;
                TTLVType ttlvType;
                TTLVCommon.GetCollectionElementType(fieldInfo.FieldType, out eleType, out ttlvType);
                node.TTLVType    = ttlvType;
                node.ElementType = eleType;
                CreateTypeStructTree(allNodeDic, node, eleType);
                break;

            case TTLVPropertyType.SingleObject:
                if (allNodeDic.ContainsKey(fieldInfo.FieldType))
                {
                    //解决递归类型,即类型同嵌套自身类型
                    node.RefNode = allNodeDic[fieldInfo.FieldType];
                }
                else
                {
                    allNodeDic.Add(fieldInfo.FieldType, node);
                    node.ElementType = fieldInfo.FieldType;
                    CreateTypeStructTree(allNodeDic, node, fieldInfo.FieldType);
                }

                node.TTLVType = TTLVType.Object;
                break;

            default:
                throw new NotImplementedException($"未实现的TTLVPropertyType[{ttlvAttribute.PropertyType.ToString()}]");
            }
        }