Пример #1
0
 public ProtocolV2(NetworkConfig config, IBinaryConverter serializer)
 {
     if (config == null)
     {
         throw new ArgumentNullException(nameof(config));
     }
     _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
     _magic      = config.Magic;
 }
 protected AbstractUtilsTest()
 {
     csvParser         = ContainerService.Instance.Resolve <ICsvParser>();
     recordConverter   = ContainerService.Instance.Resolve <IRecordConverter>();
     csvWriter         = ContainerService.Instance.Resolve <ICsvFileWriter>();
     fileWriter        = ContainerService.Instance.Resolve <IFileWriter>();
     gzip              = ContainerService.Instance.Resolve <IArchiver>();
     alphabet          = ContainerService.Instance.Resolve <IAlphabet>();
     checkpointFactory = ContainerService.Instance.Resolve <ICheckpointFactory>();
     binaryConverter   = ContainerService.Instance.Resolve <IBinaryConverter>();
     rndEventGenerator = ContainerService.Instance.Resolve <IRandomEventGenerator>();
     rndBitGenerator   = ContainerService.Instance.Resolve <IRandomBitGenerator>();
     binaryFileWriter  = ContainerService.Instance.Resolve <IBinaryFileWriter>();
 }
Пример #3
0
        /// <summary>
        /// Constructor
        /// </summary>
        public ProtocolSelector(NetworkConfig config, IBinaryConverter serializer)
        {
            // Set different protocols

            var v1 = new ProtocolV1(config, serializer);
            var v2 = new ProtocolV2(config, serializer);

            _protocols = new Dictionary <uint, ProtocolBase>
            {
                { v1.Version, v1 },
                { v2.Version, v2 }
            };

            DefaultProtocol = v2;
        }
Пример #4
0
        private IBinaryConverter CreateConverter(SerializeMetadata context)
        {
            IBinaryConverter converter = null;

            if (!ConverterCache.ContainsKey(context.Converter))
            {
                converter = Activator.CreateInstance(context.Converter) as IBinaryConverter;
                ConverterCache.TryAdd(context.Converter, converter);
            }
            else
            {
                converter = ConverterCache[context.Converter];
            }
            return(converter);
        }
Пример #5
0
        /// <summary>
        /// Constructor
        /// </summary>
        public ProtocolSelector(NetworkConfig config, IBinaryConverter serializer)
        {
            // Set different protocols

            var v1 = new ProtocolV1(config, serializer);
            var v2 = new ProtocolV2(config, serializer);

            _protocols = new Func <VersionPayload, ProtocolBase>[]
            {
                new Func <VersionPayload, ProtocolBase>
                (
                    // TODO: I don't know if we will use Version or Services
                    (v) => v.Version == 2 ? v2 : null
                )
            };

            // Default protocol, oficial protocol

            DefaultProtocol = v1;
        }
Пример #6
0
 public static void RegisterConvertor(IBinaryConverter <T> convertor, bool overrideExisting = false)
 {
     if (convertor == null)
     {
         throw new ArgumentNullException(nameof(convertor));
     }
     if (Size > 0)
     {
         throw new InvalidOperationException("Cannot register a custom convertor for fixed-size types");
     }
     if (_hasBinaryConverter && !overrideExisting)
     {
         throw new InvalidOperationException(
                   $"Type {typeof(T)} already implements IBinaryConverter<{typeof(T)}> interface. Use versioning to add a new convertor (not supported yet)");
     }
     if (convertor.Version > 0)
     {
         throw new NotImplementedException("Serialization versioning is not supported");
     }
     _hasBinaryConverter = true;
     _convertorInstance  = convertor;
     Size = convertor.Size;
 }
Пример #7
0
        /// <summary>
        /// Setup internal data based on given input read from native image.
        /// </summary>
        bool IBinaryAppender <ExportFunctionSection.ImageExportDirectory, ReaderWithOffsetArgs> .Attach(ref ExportFunctionSection.ImageExportDirectory s, uint size, ReaderWithOffsetArgs arg)
        {
            ExportFunctionSection x = new ExportFunctionSection();
            IBinaryConverter <ExportFunctionSection.ImageExportDirectory> c = x;
            DataSectionDescription e = arg.Tag as DataSectionDescription;

            if (!c.Convert(ref s, arg.Offset, size))
            {
                return(false);
            }

            if (e != null)
            {
                x.UpdateVirtualInfo(e.VirtualAddress, e.Size);
            }

            if (x.Read(arg))
            {
                Add(x);
                return(true);
            }

            return(false);
        }
Пример #8
0
        private static int Init()
        {
            var ty = typeof(T);

            if (ty == typeof(DateTime))
            {
                _isDateTime = true;
                return(8);
            }

            try {
                return(Marshal.SizeOf(ty));
            } catch {
                try {
                    // throw only once, exceptions are expensive
                    //usePinnedArray = true;
                    if (_array == null)
                    {
                        _array = new T[2];
                    }
                    _pinnedArray = GCHandle.Alloc(_array, GCHandleType.Pinned);
                    _tgt         = Marshal.UnsafeAddrOfPinnedArrayElement(_array, 0);
                    _ptr         = Marshal.UnsafeAddrOfPinnedArrayElement(_array, 1);
                    var size = (int)
                               (Marshal.UnsafeAddrOfPinnedArrayElement(_array, 1).ToInt64() -
                                Marshal.UnsafeAddrOfPinnedArrayElement(_array, 0).ToInt64());
                    _pinnedArray.Free();
                    _array = null;
                    return(size);
                } catch {
                    // NB we try to check interface as a last step, because some generic types
                    // could implement IBinaryConverter<T> but still be blittable for certain types,
                    // e.g. DateTime vs long in PersistentMap<K,V>.Entry
                    var tmp = default(T);
                    if (tmp is IBinaryConverter <T> )
                    {
                        _hasBinaryConverter = true;
                        IBinaryConverter <T> convertor;
                        try {
                            convertor = (IBinaryConverter <T>)Activator.CreateInstance <T>();
                        } catch {
                            //Trace.TraceWarning($"Type {typeof(T).FullName} is marked as IBinaryConverter and so it must have a parameterless constructor");
                            throw new ApplicationException($"Type T ({typeof(T).FullName}) is marked as IBlittable<T> and so it must have a parameterless constructor.");
                        }
                        if (convertor.Version > 0)
                        {
                            throw new InvalidOperationException("A type T implementing IBinaryConverter<T> should have default version. Register a custom convertor for versioning.");
                        }
                        _convertorInstance = convertor;
                        return(_convertorInstance.IsFixedSize ? _convertorInstance.Size : 0);
                    }
                    if (ty == typeof(byte[]))
                    {
                        _convertorInstance  = (IBinaryConverter <T>)(new ByteArrayBinaryConverter());
                        _hasBinaryConverter = true;
                        return(0);
                    }
                    if (ty == typeof(string))
                    {
                        _convertorInstance  = (IBinaryConverter <T>)(new StringBinaryConverter());
                        _hasBinaryConverter = true;
                        return(0);
                    }
                    //if (ty.IsArray) {
                    //    Console.WriteLine("IsArray");
                    //    var elementType = ty.GetElementType();
                    //    var convertor = (IBinaryConverter<T>)ArrayConvertorFactory.Create(elementType);
                    //    if (convertor != null) {
                    //        _convertorInstance = convertor;
                    //        _hasBinaryConverter = true;
                    //        Trace.Assert(!_convertorInstance.IsFixedSize);
                    //        Trace.Assert(_convertorInstance.Size == 0);
                    //        return 0;
                    //    }
                    //}
                    return(-1);
                }
            }
        }
Пример #9
0
 public EntryWriter(IBinaryStore store)
 {
     _store = store;
     _converter = EntryBinaryConverter.Instance;
 }
Пример #10
0
 private object ReadObject(object instance, BinaryMemberAttribute attribute, Type type)
 {
     if (attribute.Converter == null)
     {
         if (type == typeof(String))
         {
             if (attribute.StringFormat == StringDataFormat.Raw)
             {
                 return(ReadString(attribute.Length));
             }
             else
             {
                 return(ReadString(attribute.StringFormat));
             }
         }
         else if (type.IsEnumerable())
         {
             throw new InvalidOperationException("Multidimensional arrays cannot be read directly.");
         }
         else if (type == typeof(Boolean))
         {
             return(ReadBoolean(attribute.BooleanFormat));
         }
         else if (type == typeof(Byte))
         {
             return(ReadByte());
         }
         else if (type == typeof(DateTime))
         {
             return(ReadDateTime(attribute.DateTimeFormat));
         }
         else if (type == typeof(Decimal))
         {
             return(ReadDecimal());
         }
         else if (type == typeof(Double))
         {
             return(ReadDouble());
         }
         else if (type == typeof(Int16))
         {
             return(ReadInt16());
         }
         else if (type == typeof(Int32))
         {
             return(ReadInt32());
         }
         else if (type == typeof(Int64))
         {
             return(ReadInt64());
         }
         else if (type == typeof(SByte))
         {
             return(ReadSByte());
         }
         else if (type == typeof(Single))
         {
             return(ReadSingle());
         }
         else if (type == typeof(UInt16))
         {
             return(ReadUInt16());
         }
         else if (type == typeof(UInt32))
         {
             return(ReadUInt32());
         }
         else if (type == typeof(UInt64))
         {
             return(ReadUInt64());
         }
         else if (type.GetTypeInfo().IsEnum)
         {
             return(ReadEnum(type, attribute.Strict));
         }
         else
         {
             return(ReadCustomObject(type, null, Position));
         }
     }
     else
     {
         // Let a converter do all the work.
         IBinaryConverter converter = BinaryConverterCache.GetConverter(attribute.Converter);
         return(converter.Read(this, instance, attribute));
     }
 }
 public BinaryCalculator(IBinaryConverter binaryConverter, ILogger logger)
 {
     this.binaryConverter = binaryConverter;
     this.logger = logger;
 }
Пример #12
0
        private static void WriteObject(Stream stream, object instance, BinaryMemberAttribute attribute, Type type,
                                        object value, ByteConverter converter)
        {
            converter = converter ?? ByteConverter.System;

            if (attribute.Converter == null)
            {
                if (value == null)
                {
                    return;
                }
                else if (type == typeof(String))
                {
                    stream.Write((String)value, attribute.StringFormat, converter: converter);
                }
                else if (type.TryGetEnumerableElementType(out Type elementType))
                {
                    foreach (object element in (IEnumerable)value)
                    {
                        WriteObject(stream, null, BinaryMemberAttribute.Default, elementType, element, converter);
                    }
                }
                else if (type == typeof(Boolean))
                {
                    stream.Write((Boolean)value, attribute.BooleanFormat, converter);
                }
                else if (type == typeof(Byte))
                {
                    stream.Write((Byte)value);
                }
                else if (type == typeof(DateTime))
                {
                    stream.Write((DateTime)value, attribute.DateTimeFormat, converter);
                }
                else if (type == typeof(Decimal))
                {
                    stream.Write((Decimal)value);
                }
                else if (type == typeof(Double))
                {
                    stream.Write((Double)value, converter);
                }
                else if (type == typeof(Int16))
                {
                    stream.Write((Int16)value, converter);
                }
                else if (type == typeof(Int32))
                {
                    stream.Write((Int32)value, converter);
                }
                else if (type == typeof(Int64))
                {
                    stream.Write((Int64)value, converter);
                }
                else if (type == typeof(SByte))
                {
                    stream.Write((SByte)value);
                }
                else if (type == typeof(Single))
                {
                    stream.Write((Single)value, converter);
                }
                else if (type == typeof(UInt16))
                {
                    stream.Write((UInt16)value, converter);
                }
                else if (type == typeof(UInt32))
                {
                    stream.Write((UInt32)value, converter);
                }
                else if (type == typeof(UInt64))
                {
                    stream.Write((UInt64)value, converter);
                }
                else if (type.IsEnum)
                {
                    WriteEnum(stream, type, value, attribute.Strict, converter);
                }
                else
                {
                    if (stream.CanSeek)
                    {
                        WriteCustomObject(stream, type, value, stream.Position, converter);
                    }
                    else
                    {
                        WriteCustomObject(stream, type, value, -1, converter);
                    }
                }
            }
            else
            {
                // Let a binary converter do all the work.
                IBinaryConverter binaryConverter = BinaryConverterCache.GetConverter(attribute.Converter);
                binaryConverter.Write(stream, instance, attribute, value, converter);
            }
        }
 public PasswordHasher(IBinaryConverter binaryConverter, ISecureRandomGenerator secureRandomGenerator, IEnumerable <IPasswordFormatHasher> passwordFormatHashers)
 {
     _binaryConverter       = binaryConverter ?? throw new ArgumentNullException(nameof(binaryConverter));
     _secureRandomGenerator = secureRandomGenerator ?? throw new ArgumentNullException(nameof(secureRandomGenerator));
     _passwordFormatHashers = passwordFormatHashers ?? throw new ArgumentNullException(nameof(passwordFormatHashers));
 }
Пример #14
0
 private static object ReadObject(Stream stream, object instance, BinaryMemberAttribute attribute,
                                  Type type, ByteConverter converter)
 {
     if (attribute.Converter == null)
     {
         if (type == typeof(String))
         {
             if (attribute.StringFormat == StringCoding.Raw)
             {
                 return(stream.ReadString(attribute.Length));
             }
             else
             {
                 return(stream.ReadString(attribute.StringFormat, converter: converter));
             }
         }
         else if (type.IsEnumerable())
         {
             throw new InvalidOperationException("Multidimensional arrays cannot be read directly.");
         }
         else if (type == typeof(Boolean))
         {
             return(stream.ReadBoolean(attribute.BooleanFormat));
         }
         else if (type == typeof(Byte))
         {
             return(stream.Read1Byte());
         }
         else if (type == typeof(DateTime))
         {
             return(stream.ReadDateTime(attribute.DateTimeFormat, converter));
         }
         else if (type == typeof(Decimal))
         {
             return(stream.ReadDecimal());
         }
         else if (type == typeof(Double))
         {
             return(stream.ReadDouble(converter));
         }
         else if (type == typeof(Int16))
         {
             return(stream.ReadInt16(converter));
         }
         else if (type == typeof(Int32))
         {
             return(stream.ReadInt32(converter));
         }
         else if (type == typeof(Int64))
         {
             return(stream.ReadInt64(converter));
         }
         else if (type == typeof(SByte))
         {
             return(stream.ReadSByte());
         }
         else if (type == typeof(Single))
         {
             return(stream.ReadSingle(converter));
         }
         else if (type == typeof(UInt16))
         {
             return(stream.ReadUInt16(converter));
         }
         else if (type == typeof(UInt32))
         {
             return(stream.ReadUInt32(converter));
         }
         else if (type == typeof(UInt64))
         {
             return(stream.ReadUInt64(converter));
         }
         else if (type.IsEnum)
         {
             return(ReadEnum(stream, type, attribute.Strict, converter));
         }
         else
         {
             if (stream.CanSeek)
             {
                 return(ReadCustomObject(stream, type, null, stream.Position, converter));
             }
             else
             {
                 return(ReadCustomObject(stream, type, null, -1, converter));
             }
         }
     }
     else
     {
         // Let a binary converter do all the work.
         IBinaryConverter binaryConverter = BinaryConverterCache.GetConverter(attribute.Converter);
         return(binaryConverter.Read(stream, instance, attribute, converter));
     }
 }
Пример #15
0
 public EntityBinaryConverter(IKeyExtractor keyExtractor, IBinaryConverter entityConverter, IBinaryConverter keyConverter)
 {
     KeyExtractor    = keyExtractor;
     EntityConverter = entityConverter;
     KeyConverter    = keyConverter;
 }
Пример #16
0
 private void WriteObject(object instance, BinaryMemberAttribute attribute, Type type, object value)
 {
     if (attribute.Converter == null)
     {
         if (value == null)
         {
             return;
         }
         if (type == typeof(String))
         {
             Write((String)value, attribute.StringFormat);
         }
         else if (type.TryGetEnumerableElementType(out Type elementType))
         {
             foreach (object element in (IEnumerable)value)
             {
                 WriteObject(null, BinaryMemberAttribute.Default, elementType, element);
             }
         }
         else if (type == typeof(Boolean))
         {
             Write((Boolean)value, attribute.BooleanFormat);
         }
         else if (type == typeof(Byte))
         {
             Write((Byte)value);
         }
         else if (type == typeof(DateTime))
         {
             Write((DateTime)value, attribute.DateTimeFormat);
         }
         else if (type == typeof(Decimal))
         {
             Write((Decimal)value);
         }
         else if (type == typeof(Double))
         {
             Write((Double)value);
         }
         else if (type == typeof(Int16))
         {
             Write((Int16)value);
         }
         else if (type == typeof(Int32))
         {
             Write((Int32)value);
         }
         else if (type == typeof(Int64))
         {
             Write((Int64)value);
         }
         else if (type == typeof(SByte))
         {
             Write((SByte)value);
         }
         else if (type == typeof(Single))
         {
             Write((Single)value);
         }
         else if (type == typeof(UInt16))
         {
             Write((UInt16)value);
         }
         else if (type == typeof(UInt32))
         {
             Write((UInt32)value);
         }
         else if (type == typeof(UInt64))
         {
             Write((UInt32)value);
         }
         else if (type.GetTypeInfo().IsEnum)
         {
             WriteEnum(type, value, attribute.Strict);
         }
         else
         {
             WriteCustomObject(type, value, Position);
         }
     }
     else
     {
         // Let a converter do all the work.
         IBinaryConverter converter = BinaryConverterCache.GetConverter(attribute.Converter);
         converter.Write(this, instance, attribute, value);
     }
 }
Пример #17
0
 public EntryReader(IBinaryStore store)
 {
     _data = store.ReadAll(out _start);
     _converter = EntryBinaryConverter.Instance;
 }