Beispiel #1
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);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Internal builder creation routine.
 /// </summary>
 /// <param name="parent">Parent builder.</param>
 /// <param name="obj">binary object.</param>
 /// <param name="desc">Type descriptor.</param>
 /// <returns>Builder.</returns>
 private BinaryObjectBuilder Builder0(BinaryObjectBuilder parent, BinaryObject obj,
                                      IBinaryTypeDescriptor desc)
 {
     return(new BinaryObjectBuilder(this, parent, obj, desc));
 }
Beispiel #3
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);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Create child builder.
        /// </summary>
        /// <param name="obj">binary object.</param>
        /// <returns>Child builder.</returns>
        public BinaryObjectBuilder Child(BinaryObject obj)
        {
            var desc = _binary.Marshaller.GetDescriptor(true, obj.TypeId);

            return(new BinaryObjectBuilder(_binary, null, obj, desc));
        }
Beispiel #5
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);
            }
        }
Beispiel #6
0
 /// <summary>
 /// Gets the data starting position.
 /// </summary>
 private static int GetDataStart(BinaryObject binObj)
 {
     return(binObj.Offset + BinaryObjectHeader.Size);
 }
Beispiel #7
0
 /// <summary>
 /// Gets the non-raw data length.
 /// </summary>
 private static int GetDataLength(BinaryObject binObj)
 {
     return(binObj.Header.FooterStartOffset - BinaryObjectHeader.Size);
 }
        /**
         * <summary>Write binary object.</summary>
         */
        private static void WriteBinary(BinaryWriter ctx, BinaryObject obj)
        {
            ctx.Stream.WriteByte(BinaryUtils.TypeBinary);

            BinaryUtils.WriteBinary(ctx.Stream, obj);
        }
Beispiel #9
0
        /** <inheritdoc /> */
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }

            BinaryObject that = obj as BinaryObject;

            if (that != null)
            {
                if (_data == that._data && _offset == that._offset)
                {
                    return(true);
                }

                // 1. Check headers
                if (_header == that._header)
                {
                    // 2. Check if objects have the same field sets.
                    InitializeFields();
                    that.InitializeFields();

                    if (_fields.Keys.Count != that._fields.Keys.Count)
                    {
                        return(false);
                    }

                    foreach (int id in _fields.Keys)
                    {
                        if (!that._fields.ContainsKey(id))
                        {
                            return(false);
                        }
                    }

                    // 3. Check if objects have the same field values.
                    foreach (KeyValuePair <int, int> field in _fields)
                    {
                        object fieldVal     = GetField <object>(field.Value, null);
                        object thatFieldVal = that.GetField <object>(that._fields[field.Key], null);

                        if (!Equals(fieldVal, thatFieldVal))
                        {
                            return(false);
                        }
                    }

                    // 4. Check if objects have the same raw data.
                    // ReSharper disable ImpureMethodCallOnReadonlyValueField (method is not impure)
                    using (var stream = new BinaryHeapStream(_data))
                        using (var thatStream = new BinaryHeapStream(that._data))
                        {
                            var rawOffset     = _header.GetRawOffset(stream, _offset);
                            var thatRawOffset = that._header.GetRawOffset(thatStream, that._offset);

                            return(BinaryUtils.CompareArrays(_data, _offset + rawOffset, _header.Length - rawOffset,
                                                             that._data, that._offset + thatRawOffset, that._header.Length - thatRawOffset));
                        }
                    // ReSharper restore ImpureMethodCallOnReadonlyValueField
                }
            }

            return(false);
        }