Exemple #1
0
 public EventDeserializer(ITypeNameMapper typeNameMapper = null, ITypeConvertorGenerator typeConvertorGenerator = null)
 {
     TypeNameMapper     = typeNameMapper ?? new FullNameTypeMapper();
     ConvertorGenerator = typeConvertorGenerator ?? new DefaultTypeConvertorGenerator();
     _id2Info.Add(null); // 0 = null
     _id2Info.Add(null); // 1 = back reference
     foreach (var predefinedType in BasicSerializersFactory.TypeDescriptors)
     {
         var infoForType = new DeserializerTypeInfo
         {
             Id         = _id2Info.Count,
             Descriptor = predefinedType
         };
         _typeOrDescriptor2Info[predefinedType] = infoForType;
         _id2Info.Add(infoForType);
         _typeOrDescriptor2Info[predefinedType.GetPreferedType()] = infoForType;
         var descriptorMultipleNativeTypes = predefinedType as ITypeDescriptorMultipleNativeTypes;
         if (descriptorMultipleNativeTypes == null)
         {
             continue;
         }
         foreach (var type in descriptorMultipleNativeTypes.GetNativeTypes())
         {
             _typeOrDescriptor2Info[type] = infoForType;
         }
     }
     while (_id2Info.Count < ReservedBuildinTypes)
     {
         _id2Info.Add(null);
     }
 }
Exemple #2
0
 public EventDeserializer(ITypeNameMapper typeNameMapper = null, ITypeConvertorGenerator typeConvertorGenerator = null)
 {
     TypeNameMapper = typeNameMapper ?? new FullNameTypeMapper();
     ConvertorGenerator = typeConvertorGenerator ?? new DefaultTypeConvertorGenerator();
     _id2Info.Add(null); // 0 = null
     _id2Info.Add(null); // 1 = back reference
     foreach (var predefinedType in BasicSerializersFactory.TypeDescriptors)
     {
         var infoForType = new DeserializerTypeInfo
         {
             Id = _id2Info.Count,
             Descriptor = predefinedType
         };
         _typeOrDescriptor2Info[predefinedType] = infoForType;
         _id2Info.Add(infoForType);
         _typeOrDescriptor2Info[predefinedType.GetPreferedType()] = infoForType;
         var descriptorMultipleNativeTypes = predefinedType as ITypeDescriptorMultipleNativeTypes;
         if (descriptorMultipleNativeTypes == null) continue;
         foreach (var type in descriptorMultipleNativeTypes.GetNativeTypes())
         {
             _typeOrDescriptor2Info[type] = infoForType;
         }
     }
     while (_id2Info.Count < ReservedBuildinTypes) _id2Info.Add(null);
 }
Exemple #3
0
        public void ProcessMetadataLog(ByteBuffer buffer)
        {
            var reader = new ByteBufferReader(buffer);
            var typeId = reader.ReadVInt32();

            while (typeId != 0)
            {
                var             typeCategory = (TypeCategory)reader.ReadUInt8();
                ITypeDescriptor descriptor;
                switch (typeCategory)
                {
                case TypeCategory.BuildIn:
                    throw new ArgumentOutOfRangeException();

                case TypeCategory.Class:
                    descriptor = new ObjectTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.List:
                    descriptor = new ListTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Dictionary:
                    descriptor = new DictionaryTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Enum:
                    descriptor = new EnumTypeDescriptor(this, reader);
                    break;

                case TypeCategory.Nullable:
                    descriptor = new NullableTypeDescriptor(this, reader, NestedDescriptorReader);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                while (-typeId - 1 >= _id2InfoNew.Count)
                {
                    _id2InfoNew.Add(null);
                }
                if (_id2InfoNew[-typeId - 1] == null)
                {
                    _id2InfoNew[-typeId - 1] = new DeserializerTypeInfo {
                        Id = typeId, Descriptor = descriptor
                    }
                }
                ;
                typeId = reader.ReadVInt32();
            }
            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                _id2InfoNew[i].Descriptor.MapNestedTypes(d =>
                {
                    var placeHolderDescriptor = d as PlaceHolderDescriptor;
                    return(placeHolderDescriptor != null ? _id2InfoNew[-placeHolderDescriptor.TypeId - 1].Descriptor : d);
                });
            }
            // This additional cycle is needed to fill names of recursive structures
            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                _id2InfoNew[i].Descriptor.MapNestedTypes(d => d);
            }
            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                var infoForType = _id2InfoNew[i];
                for (var j = ReservedBuildinTypes; j < _id2Info.Count; j++)
                {
                    if (infoForType.Descriptor.Equals(_id2Info[j].Descriptor))
                    {
                        _remapToOld[infoForType.Descriptor] = _id2Info[j].Descriptor;
                        _id2InfoNew[i] = _id2Info[j];
                        infoForType    = _id2InfoNew[i];
                        break;
                    }
                }
                if (infoForType.Id < 0)
                {
                    infoForType.Id = _id2Info.Count;
                    _id2Info.Add(infoForType);
                    _typeOrDescriptor2Info[infoForType.Descriptor] = infoForType;
                }
            }
            for (var i = 0; i < _id2InfoNew.Count; i++)
            {
                _id2InfoNew[i].Descriptor.MapNestedTypes(d =>
                {
                    ITypeDescriptor res;
                    return(_remapToOld.TryGetValue(d, out res) ? res : d);
                });
            }
            _id2InfoNew.Clear();
            _remapToOld.Clear();
        }

        Func <AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeDescriptor, object> LoaderFactory(ITypeDescriptor descriptor)
        {
            var loadAsType    = LoadAsType(descriptor);
            var methodBuilder = ILBuilder.Instance.NewMethod <Func <AbstractBufferedReader, ITypeBinaryDeserializerContext, ITypeDescriptor, object> >("DeserializerFor" + descriptor.Name);
            var il            = methodBuilder.Generator;

            descriptor.GenerateLoad(il, ilGen => ilGen.Ldarg(0), ilGen => ilGen.Ldarg(1), ilGen => ilGen.Ldarg(2), loadAsType);
            if (loadAsType.IsValueType)
            {
                il.Box(loadAsType);
            }
            else if (loadAsType != typeof(object))
            {
                il.Castclass(typeof(object));
            }
            il.Ret();
            return(methodBuilder.Create());
        }
Exemple #4
0
 public void ProcessMetadataLog(ByteBuffer buffer)
 {
     var reader = new ByteBufferReader(buffer);
     var typeId = reader.ReadVInt32();
     while (typeId != 0)
     {
         var typeCategory = (TypeCategory)reader.ReadUInt8();
         ITypeDescriptor descriptor;
         switch (typeCategory)
         {
             case TypeCategory.BuildIn:
                 throw new ArgumentOutOfRangeException();
             case TypeCategory.Class:
                 descriptor = new ObjectTypeDescriptor(this, reader, NestedDescriptorReader);
                 break;
             case TypeCategory.List:
                 descriptor = new ListTypeDescriptor(this, reader, NestedDescriptorReader);
                 break;
             case TypeCategory.Dictionary:
                 descriptor = new DictionaryTypeDescriptor(this, reader, NestedDescriptorReader);
                 break;
             case TypeCategory.Enum:
                 descriptor = new EnumTypeDescriptor(this, reader);
                 break;
             default:
                 throw new ArgumentOutOfRangeException();
         }
         while (-typeId - 1 >= _id2InfoNew.Count)
             _id2InfoNew.Add(null);
         if (_id2InfoNew[-typeId - 1] == null)
             _id2InfoNew[-typeId - 1] = new DeserializerTypeInfo { Id = typeId, Descriptor = descriptor };
         typeId = reader.ReadVInt32();
     }
     for (var i = 0; i < _id2InfoNew.Count; i++)
     {
         _id2InfoNew[i].Descriptor.MapNestedTypes(d =>
         {
             var placeHolderDescriptor = d as PlaceHolderDescriptor;
             return placeHolderDescriptor != null ? _id2InfoNew[-placeHolderDescriptor.TypeId - 1].Descriptor : d;
         });
     }
     // This additional cycle is needed to fill names of recursive structures
     for (var i = 0; i < _id2InfoNew.Count; i++)
     {
         _id2InfoNew[i].Descriptor.MapNestedTypes(d => d);
     }
     for (var i = 0; i < _id2InfoNew.Count; i++)
     {
         var infoForType = _id2InfoNew[i];
         for (var j = ReservedBuildinTypes; j < _id2Info.Count; j++)
         {
             if (infoForType.Descriptor.Equals(_id2Info[j].Descriptor))
             {
                 _remapToOld[infoForType.Descriptor] = _id2Info[j].Descriptor;
                 _id2InfoNew[i] = _id2Info[j];
                 infoForType = _id2InfoNew[i];
                 break;
             }
         }
         if (infoForType.Id < 0)
         {
             infoForType.Id = _id2Info.Count;
             _id2Info.Add(infoForType);
             _typeOrDescriptor2Info[infoForType.Descriptor] = infoForType;
         }
     }
     for (var i = 0; i < _id2InfoNew.Count; i++)
     {
         _id2InfoNew[i].Descriptor.MapNestedTypes(d =>
         {
             ITypeDescriptor res;
             return _remapToOld.TryGetValue(d, out res) ? res : d;
         });
     }
     _id2InfoNew.Clear();
     _remapToOld.Clear();
 }