/** <inheritDoc /> */ public void WriteBinary<T1>(T1 obj, BinaryWriter writer) { TypeCaster<T>.Cast(obj).WriteBinary(writer); }
/** <inheritdoc /> */ public void WriteBinary <T>(T obj, BinaryWriter writer) { TypeCaster <MultidimensionalArrayHolder> .Cast(obj).WriteBinary(writer); }
/** <inheritDoc /> */ public T1 ReadBinary <T1>(BinaryReader reader, Type type, int pos) { return(TypeCaster <T1> .Cast(_ctor(reader))); }
/// <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); } }
public TResult Read <TResult>(BinaryReader ctx) { return(TypeCaster <TResult> .Cast(BinaryUtils.ReadArray <T>(ctx, false))); }
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); } }
/** <inheritdoc /> */ public void Write <T>(BinaryWriter writer, T obj) { _writeAction(writer, TypeCaster <T1> .Cast(obj)); }
/** <inheritdoc /> */ public void WriteBinary <T>(T obj, BinaryWriter writer) { TypeCaster <SerializableObjectHolder> .Cast(obj).WriteBinary(writer); }
/** <inheritdoc /> */ public T ReadBinary <T>(BinaryReader reader, Type type, int pos) { var holder = new SerializableObjectHolder(reader); return(TypeCaster <T> .Cast(holder.Item)); }
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); } }
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); } }
public TResult Read <TResult>(PortableReaderImpl ctx) { return(TypeCaster <TResult> .Cast(PortableUtils.ReadGenericArray <T>(ctx, false))); }
/** <inheritDoc /> */ public T1 ReadBinary <T1>(BinaryReader reader, IBinaryTypeDescriptor desc, int pos) { return(TypeCaster <T1> .Cast(_ctor(reader))); }
/// <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(string)) { return(BinaryUtils.GetStringHashCode((string)(object)val)); } if (type == typeof(Guid)) { return(GetGuidHashCode(TypeCaster <Guid> .Cast(val))); } 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(bool)) { return(TypeCaster <bool> .Cast(val) ? 1231 : 1237); } if (type == typeof(byte)) { return(unchecked ((sbyte)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)) { return(TypeCaster <sbyte> .Cast(val)); } if (type == typeof(ushort)) { var val0 = TypeCaster <ushort> .Cast(val); return(*(short *)&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.IsArray) { return(GetArrayHashCode(val, marsh, affinityKeyFieldIds)); } if (type == typeof(BinaryObject)) { return(val.GetHashCode()); } // DateTime, when used as key, is always written as BinaryObject. return(GetComplexTypeHashCode(val, marsh, affinityKeyFieldIds)); }
/** <inheritDoc /> */ public T1 ReadBinary<T1>(BinaryReader reader, IBinaryTypeDescriptor desc, int pos, Type typeOverride) { return TypeCaster<T1>.Cast(_ctor(reader)); }
/// <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); }
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(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.Type, pos); _frame.Struct.UpdateReaderStructure(); // Restore old frame. _frame = oldFrame; return(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); }