Beispiel #1
0
        /// <summary>
        /// Write enum value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="val">Enum value.</param>
        public void WriteEnum <T>(T val)
        {
            // ReSharper disable once CompareNonConstrainedGenericWithNull
            if (val == null)
            {
                WriteNullField();
            }
            else
            {
                var desc = _marsh.GetDescriptor(val.GetType());

                if (desc != null)
                {
                    var metaHnd = _marsh.GetBinaryTypeHandler(desc);

                    _stream.WriteByte(BinaryUtils.TypeEnum);

                    BinaryUtils.WriteEnum(this, val);

                    SaveMetadata(desc, metaHnd.OnObjectWriteFinished());
                }
                else
                {
                    // Unregistered enum, write as serializable
                    Write(new SerializableObjectHolder(val));
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Internal deserialization routine.
        /// </summary>
        /// <param name="mode">The mode.</param>
        /// <returns>
        /// Deserialized object.
        /// </returns>
        private T Deserialize <T>(BinaryMode mode)
        {
            if (_deserialized == null)
            {
                T res;

                using (IBinaryStream stream = new BinaryHeapStream(_data))
                {
                    stream.Seek(_offset, SeekOrigin.Begin);

                    res = _marsh.Unmarshal <T>(stream, mode);
                }

                var desc = _marsh.GetDescriptor(true, _header.TypeId);

                if (!desc.KeepDeserialized)
                {
                    return(res);
                }

                _deserialized = res;
            }

            return((T)_deserialized);
        }
Beispiel #3
0
        /** <inheritDoc /> */
        public IBinaryObjectBuilder GetBuilder(Type type)
        {
            IgniteArgumentCheck.NotNull(type, "type");

            IBinaryTypeDescriptor desc = _marsh.GetDescriptor(type);

            if (desc == null)
                throw new IgniteException("Type is not binary (add it to BinaryConfiguration): " + 
                    type.FullName);

            return Builder0(null, null, desc);
        }
Beispiel #4
0
        /** <inheritDoc /> */
        public IBinaryType GetBinaryType(Type type)
        {
            IgniteArgumentCheck.NotNull(type, "type");

            var desc = Marshaller.GetDescriptor(type);

            return(desc == null ? null : Marshaller.GetBinaryType(desc.TypeId));
        }
Beispiel #5
0
        /** <inheritDoc /> */
        public IBinaryObject BuildEnum(string typeName, int value)
        {
            IgniteArgumentCheck.NotNullOrEmpty(typeName, "typeName");

            var desc = Marshaller.GetDescriptor(typeName);

            IgniteArgumentCheck.Ensure(desc.IsEnum, "typeName", "Type should be an Enum.");

            _marsh.PutBinaryType(desc);

            return(new BinaryEnum(GetTypeId(typeName), value, Marshaller));
        }
Beispiel #6
0
        /** <inheritDoc /> */
        public IBinaryObject BuildEnum(Type type, int value)
        {
            IgniteArgumentCheck.NotNull(type, "type");
            IgniteArgumentCheck.Ensure(type.IsEnum, "type", "Type should be an Enum.");
            
            var desc = Marshaller.GetDescriptor(type);

            IgniteArgumentCheck.Ensure(desc.IsEnum, "typeName", "Type should be an Enum.");

            _marsh.PutBinaryType(desc);

            return new BinaryEnum(desc.TypeId, value, Marshaller);
        }
Beispiel #7
0
        /** <inheritDoc /> */
        public IBinaryObject BuildEnum(Type type, string valueName)
        {
            IgniteArgumentCheck.NotNull(type, "type");
            IgniteArgumentCheck.NotNullOrEmpty(valueName, "valueName");

            var desc = Marshaller.GetDescriptor(type);

            IgniteArgumentCheck.Ensure(desc.IsEnum, "typeName", "Type should be an Enum.");

            _marsh.PutBinaryType(desc);

            var value = GetEnumValueAsInt(type.ToString(), valueName, desc);

            return(new BinaryEnum(desc.TypeId, value, Marshaller));
        }
Beispiel #8
0
        /** <inheritdoc /> */
        public override string ToString()
        {
            var meta = GetBinaryType();

            if (meta == null || meta == BinaryType.Empty)
            {
                return(string.Format("BinaryEnum [typeId={0}, enumValue={1}]", _typeId, _enumValue));
            }

            var desc          = _marsh.GetDescriptor(true, _typeId);
            var enumValueName = desc != null && desc.Type != null?Enum.GetName(desc.Type, _enumValue) : null;

            return(string.Format("{0} [typeId={1}, enumValue={2}, enumValueName={3}]",
                                 meta.TypeName, _typeId, _enumValue, enumValueName));
        }
Beispiel #9
0
        /** <inheritDoc /> */
        public int GetTypeId(string typeName)
        {
            IgniteArgumentCheck.NotNullOrEmpty(typeName, "typeName");

            return(Marshaller.GetDescriptor(typeName).TypeId);
        }
Beispiel #10
0
        /// <summary>
        /// Write object.
        /// </summary>
        /// <param name="obj">Object.</param>
        public void Write <T>(T obj)
        {
            // Handle special case for null.
            if (obj == null)
            {
                _stream.WriteByte(BinaryUtils.HdrNull);

                return;
            }

            // We use GetType() of a real object instead of typeof(T) to take advantage of
            // automatic Nullable'1 unwrapping.
            Type type = obj.GetType();

            // Handle common case when primitive is written.
            if (type.IsPrimitive)
            {
                WritePrimitive(obj, type);

                return;
            }

            // Handle special case for builder.
            if (WriteBuilderSpecials(obj))
            {
                return;
            }

            // Suppose that we faced normal object and perform descriptor lookup.
            IBinaryTypeDescriptor desc = type.IsEnum ? null : _marsh.GetDescriptor(type);

            if (desc != null)
            {
                // Writing normal object.
                var pos = _stream.Position;

                // Dealing with handles.
                if (!(desc.Serializer is IBinarySystemTypeSerializer) && WriteHandle(pos, obj))
                {
                    return;
                }

                // Skip header length as not everything is known now
                _stream.Seek(BinaryObjectHeader.Size, SeekOrigin.Current);

                // Preserve old frame.
                int oldTypeId = _curTypeId;
                IBinaryNameMapper oldConverter = _curConverter;
                IBinaryIdMapper   oldMapper    = _curMapper;
                int oldRawPos = _curRawPos;
                var oldPos    = _curPos;

                var oldStruct = _curStruct;

                // Push new frame.
                _curTypeId    = desc.TypeId;
                _curConverter = desc.NameMapper;
                _curMapper    = desc.IdMapper;
                _curRawPos    = 0;
                _curPos       = pos;

                _curStruct = new BinaryStructureTracker(desc, desc.WriterTypeStructure);
                var schemaIdx = _schema.PushSchema();

                try
                {
                    // Write object fields.
                    desc.Serializer.WriteBinary(obj, this);

                    // Write schema
                    var schemaOffset = _stream.Position - pos;

                    int schemaId;

                    var flags = desc.UserType
                        ? BinaryObjectHeader.Flag.UserType
                        : BinaryObjectHeader.Flag.None;

                    var hasSchema = _schema.WriteSchema(_stream, schemaIdx, out schemaId, ref flags);

                    if (hasSchema)
                    {
                        flags |= BinaryObjectHeader.Flag.HasSchema;

                        // Calculate and write header.
                        if (_curRawPos > 0)
                        {
                            _stream.WriteInt(_curRawPos - pos); // raw offset is in the last 4 bytes
                        }
                    }
                    else
                    {
                        schemaOffset = BinaryObjectHeader.Size;
                    }

                    if (_curRawPos > 0)
                    {
                        flags |= BinaryObjectHeader.Flag.HasRaw;
                    }

                    var len = _stream.Position - pos;

                    var header = new BinaryObjectHeader(desc.TypeId, obj.GetHashCode(), len,
                                                        schemaId, schemaOffset, flags);

                    BinaryObjectHeader.Write(header, _stream, pos);

                    Stream.Seek(pos + len, SeekOrigin.Begin); // Seek to the end
                }
                finally
                {
                    _schema.PopSchema(schemaIdx);
                }

                // Apply structure updates if any.
                _curStruct.UpdateWriterStructure(this);

                // Restore old frame.
                _curTypeId    = oldTypeId;
                _curConverter = oldConverter;
                _curMapper    = oldMapper;
                _curRawPos    = oldRawPos;
                _curPos       = oldPos;

                _curStruct = oldStruct;
            }
            else
            {
                // Are we dealing with a well-known type?
                var handler = BinarySystemHandlers.GetWriteHandler(type);

                if (handler == null)  // We did our best, object cannot be marshalled.
                {
                    throw new BinaryObjectException("Unsupported object type [type=" + type + ", object=" + obj + ']');
                }

                handler(this, obj);
            }
        }
Beispiel #11
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);
            }
        }