Пример #1
0
        /// <summary>
        /// Reads the bool. Actual data may be bool or int/long.
        /// </summary>
        private static T ReadBool <T>(IBinaryRawReader reader, int count)
        {
            var obj = reader.ReadObject <object>();

            if (obj is bool)
            {
                return((T)obj);
            }

            if (obj is long)
            {
                return(TypeCaster <T> .Cast((long)obj != 0));
            }

            if (obj is int)
            {
                return(TypeCaster <T> .Cast((int)obj != 0));
            }

            throw new InvalidOperationException("Expected bool, got: " + obj);
        }
Пример #2
0
        public void Execute(ref IBaseMessage inmsg, IPipelineContext pc)
        {
            try
            {
                object value = null;
                value = StaticHelpers.ReadFromSSO(_SSOApplication, _SSOKey);

                if (promotion == ContextInstructionTypeEnum.Write)
                {
                    inmsg.Context.Write(propertyName, propertyNamespace, TypeCaster.GetTypedObject(value, type));
                }
                else if (promotion == ContextInstructionTypeEnum.Promote)
                {
                    inmsg.Context.Promote(propertyName, propertyNamespace, TypeCaster.GetTypedObject(value, type));
                }
            }
            catch (Exception e)
            {
                throw new Exception("Unable to set context property " + propertyNamespace + "#" + propertyName + " from SSO application " + _SSOApplication + " and SSO Key " + _SSOKey + ". Encountered error - " + e.ToString());
            }
        }
        /** <inheritdoc /> */
        public T ReadBinary <T>(BinaryReader reader, Type type, int pos)
        {
            var holder = new DateTimeHolder(reader);

            return(TypeCaster <T> .Cast(holder.Item));
        }
Пример #4
0
 public void Execute(ref IBaseMessage inmsg, IPipelineContext pc)
 {
     try
     {
         if (castRequired)
         {
             inmsg.GetPart(partName).PartProperties.Write(propertyName, propertyNamespace, TypeCaster.GetTypedObject(value, type));
         }
         else
         {
             inmsg.GetPart(partName).PartProperties.Write(propertyName, propertyNamespace, value);
         }
     }
     catch (Exception e)
     {
         throw new Exception("Unable to set part property " + propertyNamespace + "#" + propertyName + " on message part " + partName
                             + ". Encountered error - " + e.ToString());
     }
 }
 /** <inheritdoc /> */
 public void WriteBinary <T>(T obj, BinaryWriter writer)
 {
     TypeCaster <DateTimeHolder> .Cast(obj).WriteBinary(writer);
 }
Пример #6
0
        private T ReadFullObject <T>(int pos)
        {
            var hdr = BinaryObjectHeader.Read(Stream, pos);

            // Validate protocol version.
            BinaryUtils.ValidateProtocolVersion(hdr.Version);

            try
            {
                // Already read this object?
                object hndObj;

                if (_hnds != null && _hnds.TryGetValue(pos, out hndObj))
                {
                    return((T)hndObj);
                }

                if (hdr.IsUserType && _mode == BinaryMode.ForceBinary)
                {
                    BinaryObject portObj;

                    if (_detach)
                    {
                        Stream.Seek(pos, SeekOrigin.Begin);

                        portObj = new BinaryObject(_marsh, Stream.ReadByteArray(hdr.Length), 0, hdr);
                    }
                    else
                    {
                        portObj = new BinaryObject(_marsh, Stream.GetArray(), pos, hdr);
                    }

                    T obj = _builder == null ? TypeCaster <T> .Cast(portObj) : TypeCaster <T> .Cast(_builder.Child(portObj));

                    AddHandle(pos, obj);

                    return(obj);
                }
                else
                {
                    // Find descriptor.
                    var desc = hdr.TypeId == BinaryUtils.TypeUnregistered
                        ? _marsh.GetDescriptor(Type.GetType(ReadString(), true))
                        : _marsh.GetDescriptor(hdr.IsUserType, hdr.TypeId, true);

                    // Instantiate object.
                    if (desc.Type == null)
                    {
                        if (desc is BinarySurrogateTypeDescriptor)
                        {
                            throw new BinaryObjectException(string.Format(
                                                                "Unknown type ID: {0}. " +
                                                                "This usually indicates missing BinaryConfiguration. " +
                                                                "Make sure that all nodes have the same BinaryConfiguration.", hdr.TypeId));
                        }

                        throw new BinaryObjectException(string.Format(
                                                            "No matching type found for object [typeId={0}, typeName={1}]. " +
                                                            "This usually indicates that assembly with specified type is not loaded on a node. " +
                                                            "When using Apache.Ignite.exe, make sure to load assemblies with -assembly parameter.",
                                                            desc.TypeId, desc.TypeName));
                    }

                    // Preserve old frame.
                    var oldFrame = _frame;

                    // Set new frame.
                    _frame.Hdr = hdr;
                    _frame.Pos = pos;
                    SetCurSchema(desc);
                    _frame.Struct = new BinaryStructureTracker(desc, desc.ReaderTypeStructure);
                    _frame.Raw    = false;

                    // Read object.
                    var obj = desc.Serializer.ReadBinary <T>(this, desc, pos);

                    _frame.Struct.UpdateReaderStructure();

                    // Restore old frame.
                    _frame = oldFrame;

                    return(obj);
                }
            }
            finally
            {
                // Advance stream pointer.
                Stream.Seek(pos + hdr.Length, SeekOrigin.Begin);
            }
        }
Пример #7
0
 public void Execute(ref IBaseMessage inmsg, IPipelineContext pc)
 {
     if (throwException)
     {
         throw new Exception("Will fail");
     }
     else
     {
         try
         {
             if (promotion == ContextInstructionTypeEnum.Write)
             {
                 inmsg.Context.Write(propertyName, "https://BREPipelineFramework.TestProject.BREPipelineFramework_PropSchema", TypeCaster.GetTypedObject(value, type));
             }
             else if (promotion == ContextInstructionTypeEnum.Promote)
             {
                 inmsg.Context.Promote(propertyName, "https://BREPipelineFramework.TestProject.BREPipelineFramework_PropSchema", TypeCaster.GetTypedObject(value, type));
             }
         }
         catch (Exception e)
         {
             throw new Exception("Unable to set context property " + "https://BREPipelineFramework.TestProject.BREPipelineFramework_PropSchema" + "#" + propertyName + ". Encountered error - " + e.ToString());
         }
     }
 }
Пример #8
0
 /** <inheritdoc /> */
 public void Write <T>(BinaryWriter writer, T obj)
 {
     _writeAction(writer, TypeCaster <T1> .Cast(obj));
 }
Пример #9
0
 /** <inheritdoc /> */
 public void WriteBinary <T>(T obj, BinaryWriter writer)
 {
     TypeCaster <MultidimensionalArrayHolder> .Cast(obj).WriteBinary(writer);
 }
Пример #10
0
 public bool ConvertToBool(object obj)
 {
     return((bool)TypeCaster.GetTypedObject(obj, TypeEnum.Boolean));
 }
Пример #11
0
 public DateTime ConvertToDateTime(object obj)
 {
     return((DateTime)TypeCaster.GetTypedObject(obj, TypeEnum.DateTime));
 }
Пример #12
0
        /// <summary>
        /// Reads an object of predefined type.
        /// </summary>
        public static bool TryReadSystemType <T>(byte typeId, BinaryReader ctx, out T res)
        {
            var stream = ctx.Stream;

            switch (typeId)
            {
            case BinaryTypeId.Byte:
                res = TypeCaster <T> .Cast(stream.ReadByte());

                return(true);

            case BinaryTypeId.Short:
                res = TypeCaster <T> .Cast(stream.ReadShort());

                return(true);

            case BinaryTypeId.Int:
                res = TypeCaster <T> .Cast(stream.ReadInt());

                return(true);

            case BinaryTypeId.Long:
                res = TypeCaster <T> .Cast(stream.ReadLong());

                return(true);

            case BinaryTypeId.Float:
                res = TypeCaster <T> .Cast(stream.ReadFloat());

                return(true);

            case BinaryTypeId.Double:
                res = TypeCaster <T> .Cast(stream.ReadDouble());

                return(true);

            case BinaryTypeId.Char:
                res = TypeCaster <T> .Cast(stream.ReadChar());

                return(true);

            case BinaryTypeId.Bool:
                res = TypeCaster <T> .Cast(stream.ReadBool());

                return(true);

            case BinaryTypeId.String:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadString(stream));

                return(true);

            case BinaryTypeId.Guid:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadGuid(stream));

                return(true);

            case BinaryTypeId.ArrayByte:
                res = typeof(T) == typeof(sbyte[])
                        ? TypeCaster <T> .Cast(BinaryUtils.ReadSbyteArray(stream))
                        : TypeCaster <T> .Cast(BinaryUtils.ReadByteArray(stream));

                return(true);

            case BinaryTypeId.ArrayShort:
                res = typeof(T) == typeof(ushort[])
                        ? TypeCaster <T> .Cast(BinaryUtils.ReadUshortArray(stream))
                        : TypeCaster <T> .Cast(BinaryUtils.ReadShortArray(stream));

                return(true);

            case BinaryTypeId.ArrayInt:
                res = typeof(T) == typeof(uint[])
                        ? TypeCaster <T> .Cast(BinaryUtils.ReadUintArray(stream))
                        : TypeCaster <T> .Cast(BinaryUtils.ReadIntArray(stream));

                return(true);

            case BinaryTypeId.ArrayLong:
                res = typeof(T) == typeof(ulong[])
                        ? TypeCaster <T> .Cast(BinaryUtils.ReadUlongArray(stream))
                        : TypeCaster <T> .Cast(BinaryUtils.ReadLongArray(stream));

                return(true);

            case BinaryTypeId.ArrayFloat:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadFloatArray(stream));

                return(true);

            case BinaryTypeId.ArrayDouble:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadDoubleArray(stream));

                return(true);

            case BinaryTypeId.ArrayChar:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadCharArray(stream));

                return(true);

            case BinaryTypeId.ArrayBool:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadBooleanArray(stream));

                return(true);

            case BinaryTypeId.ArrayString:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadStringArray(stream));

                return(true);

            case BinaryTypeId.ArrayGuid:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadGuidArray(stream));

                return(true);

            case BinaryTypeId.Array:
                res = (T)ReadArray(ctx, typeof(T));
                return(true);

            case BinaryTypeId.Collection:
                res = (T)BinaryUtils.ReadCollection(ctx, null, null);
                return(true);

            case BinaryTypeId.Dictionary:
                res = (T)BinaryUtils.ReadDictionary(ctx, null);
                return(true);

            case BinaryTypeId.ArrayEnum:
                res = (T)ReadArray(ctx, typeof(T));
                return(true);

            case BinaryTypeId.Decimal:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadDecimal(stream));

                return(true);

            case BinaryTypeId.ArrayDecimal:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadDecimalArray(stream));

                return(true);

            case BinaryTypeId.Timestamp:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadTimestamp(stream, ctx.Marshaller.TimestampConverter));

                return(true);

            case BinaryTypeId.ArrayTimestamp:
                res = TypeCaster <T> .Cast(BinaryUtils.ReadTimestampArray(stream, ctx.Marshaller.TimestampConverter));

                return(true);

            case BinaryTypeId.OptimizedMarshaller:
                res = (T)(object)new OptimizedMarshallerObject(ctx.Stream);
                return(true);
            }

            res = default(T);
            return(false);
        }
Пример #13
0
 public int ConvertToInt(object obj)
 {
     return((int)TypeCaster.GetTypedObject(obj, TypeEnum.Integer));
 }
        /** <inheritdoc /> */
        public T ReadBinary <T>(BinaryReader reader, Type type, int pos)
        {
            var holder = new SerializableObjectHolder(reader);

            return(TypeCaster <T> .Cast(holder.Item));
        }
 /** <inheritdoc /> */
 public void WriteBinary <T>(T obj, BinaryWriter writer)
 {
     TypeCaster <SerializableObjectHolder> .Cast(obj).WriteBinary(writer);
 }
Пример #16
0
 public TResult Read <TResult>(PortableReaderImpl ctx)
 {
     return(TypeCaster <TResult> .Cast(PortableUtils.ReadGenericArray <T>(ctx, false)));
 }
Пример #17
0
        /// <summary>
        /// Gets the Ignite-specific hash code for the provided value.
        /// </summary>
        public static unsafe int GetHashCode <T>(T val, Marshaller marsh, IDictionary <int, int> affinityKeyFieldIds)
        {
            Debug.Assert(marsh != null);
            Debug.Assert(val != null);

            var type = val.GetType();

            if (type == typeof(int))
            {
                return(TypeCaster <int> .Cast(val));
            }

            if (type == typeof(long))
            {
                return(GetLongHashCode(TypeCaster <long> .Cast(val)));
            }

            if (type == typeof(bool))
            {
                return(TypeCaster <bool> .Cast(val) ? 1231 : 1237);
            }

            if (type == typeof(byte))
            {
                return(TypeCaster <byte> .Cast(val));
            }

            if (type == typeof(short))
            {
                return(TypeCaster <short> .Cast(val));
            }

            if (type == typeof(char))
            {
                return(TypeCaster <char> .Cast(val));
            }

            if (type == typeof(float))
            {
                var floatVal = TypeCaster <float> .Cast(val);

                return(*(int *)&floatVal);
            }

            if (type == typeof(double))
            {
                var doubleVal = TypeCaster <double> .Cast(val);

                return(GetLongHashCode(*(long *)&doubleVal));
            }

            if (type == typeof(sbyte))
            {
                var val0 = TypeCaster <sbyte> .Cast(val);

                return(*(byte *)&val0);
            }

            if (type == typeof(ushort))
            {
                var val0 = TypeCaster <ushort> .Cast(val);

                return(*(short *)&val0);
            }

            if (type == typeof(uint))
            {
                var val0 = TypeCaster <uint> .Cast(val);

                return(*(int *)&val0);
            }

            if (type == typeof(ulong))
            {
                var val0 = TypeCaster <ulong> .Cast(val);

                return(GetLongHashCode(*(long *)&val0));
            }

            if (type == typeof(IntPtr))
            {
                var val0 = TypeCaster <IntPtr> .Cast(val).ToInt64();

                return(GetLongHashCode(val0));
            }

            if (type == typeof(UIntPtr))
            {
                var val0 = TypeCaster <UIntPtr> .Cast(val).ToUInt64();

                return(GetLongHashCode(*(long *)&val0));
            }

            if (type == typeof(Guid))
            {
                return(GetGuidHashCode(TypeCaster <Guid> .Cast(val)));
            }

            // DateTime, when used as key, is always written as BinaryObject.
            return(GetComplexTypeHashCode(val, marsh, affinityKeyFieldIds));
        }
 /** <inheritDoc /> */
 public T1 ReadBinary <T1>(BinaryReader reader, Type type, int pos)
 {
     return(TypeCaster <T1> .Cast(_ctor(reader)));
 }
Пример #19
0
 public TResult Read <TResult>(BinaryReader ctx)
 {
     return(TypeCaster <TResult> .Cast(BinaryUtils.ReadArray <T>(ctx, false)));
 }
Пример #20
0
 /** <inheritDoc /> */
 public void WriteBinary<T1>(T1 obj, BinaryWriter writer)
 {
     TypeCaster<T>.Cast(obj).WriteBinary(writer);
 }
Пример #21
0
        private T ReadFullObject <T>(int pos)
        {
            var hdr = BinaryObjectHeader.Read(Stream, pos);

            // Validate protocol version.
            BinaryUtils.ValidateProtocolVersion(hdr.Version);

            try
            {
                // Already read this object?
                object hndObj;

                if (_hnds != null && _hnds.TryGetValue(pos, out hndObj))
                {
                    return((T)hndObj);
                }

                if (hdr.IsUserType && _mode == BinaryMode.ForceBinary)
                {
                    BinaryObject portObj;

                    if (_detach)
                    {
                        Stream.Seek(pos, SeekOrigin.Begin);

                        portObj = new BinaryObject(_marsh, Stream.ReadByteArray(hdr.Length), 0, hdr);
                    }
                    else
                    {
                        portObj = new BinaryObject(_marsh, Stream.GetArray(), pos, hdr);
                    }

                    T obj = _builder == null ? TypeCaster <T> .Cast(portObj) : TypeCaster <T> .Cast(_builder.Child(portObj));

                    AddHandle(pos, obj);

                    return(obj);
                }
                else
                {
                    // Find descriptor.
                    IBinaryTypeDescriptor desc;

                    if (!_descs.TryGetValue(BinaryUtils.TypeKey(hdr.IsUserType, hdr.TypeId), out desc))
                    {
                        throw new BinaryObjectException("Unknown type ID: " + hdr.TypeId);
                    }

                    // Instantiate object.
                    if (desc.Type == null)
                    {
                        throw new BinaryObjectException("No matching type found for object [typeId=" +
                                                        desc.TypeId + ", typeName=" + desc.TypeName + ']');
                    }

                    // Preserve old frame.
                    var  oldHdr       = _curHdr;
                    int  oldPos       = _curPos;
                    var  oldStruct    = _curStruct;
                    bool oldRaw       = _curRaw;
                    var  oldSchema    = _curSchema;
                    var  oldSchemaMap = _curSchemaMap;

                    // Set new frame.
                    _curHdr = hdr;
                    _curPos = pos;
                    SetCurSchema(desc);
                    _curStruct = new BinaryStructureTracker(desc, desc.ReaderTypeStructure);
                    _curRaw    = false;

                    // Read object.
                    Stream.Seek(pos + BinaryObjectHeader.Size, SeekOrigin.Begin);

                    object obj;

                    var sysSerializer = desc.Serializer as IBinarySystemTypeSerializer;

                    if (sysSerializer != null)
                    {
                        obj = sysSerializer.ReadInstance(this);
                    }
                    else
                    {
                        try
                        {
                            obj = FormatterServices.GetUninitializedObject(desc.Type);

                            // Save handle.
                            AddHandle(pos, obj);
                        }
                        catch (Exception e)
                        {
                            throw new BinaryObjectException("Failed to create type instance: " +
                                                            desc.Type.AssemblyQualifiedName, e);
                        }

                        desc.Serializer.ReadBinary(obj, this);
                    }

                    _curStruct.UpdateReaderStructure();

                    // Restore old frame.
                    _curHdr       = oldHdr;
                    _curPos       = oldPos;
                    _curStruct    = oldStruct;
                    _curRaw       = oldRaw;
                    _curSchema    = oldSchema;
                    _curSchemaMap = oldSchemaMap;

                    // Process wrappers. We could introduce a common interface, but for only 2 if-else is faster.
                    var wrappedSerializable = obj as SerializableObjectHolder;

                    if (wrappedSerializable != null)
                    {
                        return((T)wrappedSerializable.Item);
                    }

                    var wrappedDateTime = obj as DateTimeHolder;

                    if (wrappedDateTime != null)
                    {
                        return(TypeCaster <T> .Cast(wrappedDateTime.Item));
                    }

                    return((T)obj);
                }
            }
            finally
            {
                // Advance stream pointer.
                Stream.Seek(pos + hdr.Length, SeekOrigin.Begin);
            }
        }
 /** <inheritdoc /> */
 public void WriteBinary <T>(T obj, BinaryWriter writer)
 {
     TypeCaster <PeerLoadingObjectHolder> .Cast(obj).WriteBinary(writer);
 }
Пример #23
0
        private T ReadFullObject <T>(int pos)
        {
            var hdr = BinaryObjectHeader.Read(Stream, pos);

            // Validate protocol version.
            BinaryUtils.ValidateProtocolVersion(hdr.Version);

            try
            {
                // Already read this object?
                object hndObj;

                if (_hnds != null && _hnds.TryGetValue(pos, out hndObj))
                {
                    return((T)hndObj);
                }

                if (hdr.IsUserType && _mode == BinaryMode.ForceBinary)
                {
                    BinaryObject portObj;

                    if (_detach)
                    {
                        Stream.Seek(pos, SeekOrigin.Begin);

                        portObj = new BinaryObject(_marsh, Stream.ReadByteArray(hdr.Length), 0, hdr);
                    }
                    else
                    {
                        portObj = new BinaryObject(_marsh, Stream.GetArray(), pos, hdr);
                    }

                    T obj = _builder == null ? TypeCaster <T> .Cast(portObj) : TypeCaster <T> .Cast(_builder.Child(portObj));

                    AddHandle(pos, obj);

                    return(obj);
                }
                else
                {
                    // Find descriptor.
                    var desc = _marsh.GetDescriptor(hdr.IsUserType, hdr.TypeId);

                    // Instantiate object.
                    if (desc.Type == null)
                    {
                        if (desc is BinarySurrogateTypeDescriptor)
                        {
                            throw new BinaryObjectException("Unknown type ID: " + hdr.TypeId);
                        }

                        throw new BinaryObjectException("No matching type found for object [typeId=" +
                                                        desc.TypeId + ", typeName=" + desc.TypeName + ']');
                    }

                    // Preserve old frame.
                    var  oldHdr       = _curHdr;
                    int  oldPos       = _curPos;
                    var  oldStruct    = _curStruct;
                    bool oldRaw       = _curRaw;
                    var  oldSchema    = _curSchema;
                    var  oldSchemaMap = _curSchemaMap;

                    // Set new frame.
                    _curHdr = hdr;
                    _curPos = pos;
                    SetCurSchema(desc);
                    _curStruct = new BinaryStructureTracker(desc, desc.ReaderTypeStructure);
                    _curRaw    = false;

                    // Read object.
                    Stream.Seek(pos + BinaryObjectHeader.Size, SeekOrigin.Begin);

                    var obj = desc.Serializer.ReadBinary <T>(this, desc.Type, pos);

                    _curStruct.UpdateReaderStructure();

                    // Restore old frame.
                    _curHdr       = oldHdr;
                    _curPos       = oldPos;
                    _curStruct    = oldStruct;
                    _curRaw       = oldRaw;
                    _curSchema    = oldSchema;
                    _curSchemaMap = oldSchemaMap;

                    return(obj);
                }
            }
            finally
            {
                // Advance stream pointer.
                Stream.Seek(pos + hdr.Length, SeekOrigin.Begin);
            }
        }
Пример #24
0
        public void Execute(ref IBaseMessage inmsg, IPipelineContext pc)
        {
            XmlTextReader   xmlTextReader   = new XmlTextReader(inmsg.BodyPart.GetOriginalDataStream());
            XPathCollection xPathCollection = GetXPathCollection();
            XPathReader     xPathReader     = new XPathReader(xmlTextReader, xPathCollection);

            while (xPathReader.ReadUntilMatch())
            {
                for (int i = 0; i < xPathCollection.Count; i++)
                {
                    if (xPathReader.Match(i))
                    {
                        string value = null;

                        _XPathInstructions.ElementAt(i).IsFound = true;
                        switch (_XPathInstructions.ElementAt(i).XPathResultType)
                        {
                        case XPathResultTypeEnum.Name:
                            value = xPathReader.LocalName;
                            break;

                        case XPathResultTypeEnum.Namespace:
                            value = xPathReader.NamespaceURI;
                            break;

                        case XPathResultTypeEnum.Value:
                            value = xPathReader.ReadString();
                            break;
                        }

                        if (_XPathInstructions.ElementAt(i).Promotion == ContextInstructionTypeEnum.Promote)
                        {
                            inmsg.Context.Promote(_XPathInstructions.ElementAt(i).PropertyName, _XPathInstructions.ElementAt(i).PropertyNamespace, TypeCaster.GetTypedObject(value, _XPathInstructions.ElementAt(i).Type));
                        }
                        else
                        {
                            inmsg.Context.Write(_XPathInstructions.ElementAt(i).PropertyName, _XPathInstructions.ElementAt(i).PropertyNamespace, TypeCaster.GetTypedObject(value, _XPathInstructions.ElementAt(i).Type));
                        }
                    }
                }
            }

            for (int i = 0; i < xPathCollection.Count; i++)
            {
                if (_XPathInstructions.ElementAt(i).IsFound == false && _XPathInstructions.ElementAt(i).ExceptionIfNotFound == true)
                {
                    throw new Exception("Unable to evaluate XPath expression " + _XPathInstructions.ElementAt(i).XPathQuery + " against the message.");
                }
            }

            inmsg.BodyPart.Data.Position = 0;
        }
Пример #25
0
 /** <inheritDoc /> */
 public T1 ReadBinary<T1>(BinaryReader reader, IBinaryTypeDescriptor desc, int pos, Type typeOverride)
 {
     return TypeCaster<T1>.Cast(_ctor(reader));
 }
Пример #26
0
 /** <inheritDoc /> */
 public T1 ReadBinary <T1>(BinaryReader reader, IBinaryTypeDescriptor desc, int pos)
 {
     return(TypeCaster <T1> .Cast(_ctor(reader)));
 }
Пример #27
0
        /// <summary>
        /// Write primitive type.
        /// </summary>
        /// <param name="val">Object.</param>
        /// <param name="type">Type.</param>
        private unsafe void WritePrimitive <T>(T val, Type type)
        {
            // .Net defines 14 primitive types. We support 12 - excluding IntPtr and UIntPtr.
            // Types check sequence is designed to minimize comparisons for the most frequent types.

            if (type == typeof(int))
            {
                WriteIntField(TypeCaster <int> .Cast(val));
            }
            else if (type == typeof(long))
            {
                WriteLongField(TypeCaster <long> .Cast(val));
            }
            else if (type == typeof(bool))
            {
                WriteBooleanField(TypeCaster <bool> .Cast(val));
            }
            else if (type == typeof(byte))
            {
                WriteByteField(TypeCaster <byte> .Cast(val));
            }
            else if (type == typeof(short))
            {
                WriteShortField(TypeCaster <short> .Cast(val));
            }
            else if (type == typeof(char))
            {
                WriteCharField(TypeCaster <char> .Cast(val));
            }
            else if (type == typeof(float))
            {
                WriteFloatField(TypeCaster <float> .Cast(val));
            }
            else if (type == typeof(double))
            {
                WriteDoubleField(TypeCaster <double> .Cast(val));
            }
            else if (type == typeof(sbyte))
            {
                var val0 = TypeCaster <sbyte> .Cast(val);

                WriteByteField(*(byte *)&val0);
            }
            else if (type == typeof(ushort))
            {
                var val0 = TypeCaster <ushort> .Cast(val);

                WriteShortField(*(short *)&val0);
            }
            else if (type == typeof(uint))
            {
                var val0 = TypeCaster <uint> .Cast(val);

                WriteIntField(*(int *)&val0);
            }
            else if (type == typeof(ulong))
            {
                var val0 = TypeCaster <ulong> .Cast(val);

                WriteLongField(*(long *)&val0);
            }
            else
            {
                throw BinaryUtils.GetUnsupportedTypeException(type, val);
            }
        }
Пример #28
0
        private T ReadFullObject <T>(int pos)
        {
            // Read header.
            bool userType = Stream.ReadBool();
            int  typeId   = Stream.ReadInt();
            // ReSharper disable once UnusedVariable
            int hashCode  = Stream.ReadInt();
            int len       = Stream.ReadInt();
            int rawOffset = Stream.ReadInt();

            try
            {
                // Already read this object?
                object hndObj;

                if (_hnds != null && _hnds.TryGetValue(pos, out hndObj))
                {
                    return((T)hndObj);
                }

                if (userType && _mode == PortableMode.ForcePortable)
                {
                    PortableUserObject portObj;

                    if (_detach)
                    {
                        Stream.Seek(pos, SeekOrigin.Begin);

                        portObj = GetPortableUserObject(pos, 0, Stream.ReadByteArray(len));
                    }
                    else
                    {
                        portObj = GetPortableUserObject(pos, pos, Stream.Array());
                    }

                    T obj = _builder == null ? TypeCaster <T> .Cast(portObj) : TypeCaster <T> .Cast(_builder.Child(portObj));

                    AddHandle(pos, obj);

                    return(obj);
                }
                else
                {
                    // Find descriptor.
                    IPortableTypeDescriptor desc;

                    if (!_descs.TryGetValue(PortableUtils.TypeKey(userType, typeId), out desc))
                    {
                        throw new PortableException("Unknown type ID: " + typeId);
                    }

                    // Instantiate object.
                    if (desc.Type == null)
                    {
                        throw new PortableException("No matching type found for object [typeId=" +
                                                    desc.TypeId + ", typeName=" + desc.TypeName + ']');
                    }

                    // Preserve old frame.
                    int oldTypeId    = _curTypeId;
                    int oldPos       = _curPos;
                    int oldRawOffset = _curRawOffset;
                    IPortableNameMapper oldConverter = _curConverter;
                    IPortableIdMapper   oldMapper    = _curMapper;
                    bool oldRaw = _curRaw;

                    // Set new frame.
                    _curTypeId    = typeId;
                    _curPos       = pos;
                    _curRawOffset = rawOffset;
                    _curConverter = desc.NameConverter;
                    _curMapper    = desc.Mapper;
                    _curRaw       = false;

                    // Read object.
                    object obj;

                    var sysSerializer = desc.Serializer as IPortableSystemTypeSerializer;

                    if (sysSerializer != null)
                    {
                        obj = sysSerializer.ReadInstance(this);
                    }
                    else
                    {
                        try
                        {
                            obj = FormatterServices.GetUninitializedObject(desc.Type);

                            // Save handle.
                            AddHandle(pos, obj);
                        }
                        catch (Exception e)
                        {
                            throw new PortableException("Failed to create type instance: " +
                                                        desc.Type.AssemblyQualifiedName, e);
                        }

                        desc.Serializer.ReadPortable(obj, this);
                    }

                    // Restore old frame.
                    _curTypeId    = oldTypeId;
                    _curPos       = oldPos;
                    _curRawOffset = oldRawOffset;
                    _curConverter = oldConverter;
                    _curMapper    = oldMapper;
                    _curRaw       = oldRaw;

                    var wrappedSerializable = obj as SerializableObjectHolder;

                    return(wrappedSerializable != null ? (T)wrappedSerializable.Item : (T)obj);
                }
            }
            finally
            {
                // Advance stream pointer.
                Stream.Seek(pos + len, SeekOrigin.Begin);
            }
        }