/// <summary>
        /// Deserializes an object from a binary stream
        /// </summary>
        /// <param name="expectedType">The type that is expected to be deserialized</param>
        /// <param name="reader">The <see cref="BinaryTokenStreamReader"/></param>
        /// <returns>The deserialized object</returns>
        public object Deserialize(Type expectedType, BinaryTokenStreamReader reader)
        {
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var           typeHandle = expectedType.TypeHandle;
            MessageParser parser     = null;

            if (!Parsers.TryGetValue(typeHandle, out parser))
            {
                throw new ArgumentException("No parser found for the expected type " + expectedType, "expectedType");
            }

            int length = reader.ReadInt();

            if (length == 0)
            {
                // the special null case.
                return(null);
            }
            byte[] data = reader.ReadBytes(length);

            object message = parser.ParseFrom(data);

            return(message);
        }
 private static object Deserialize(Type expected, BinaryTokenStreamReader stream)
 {
     DeserializeCounter++;
     var result = new ClassWithCustomSerializer();
     result.IntProperty = stream.ReadInt();
     result.StringProperty = stream.ReadString();
     return result;
 }
Exemple #3
0
        public static TypeKey ReadTypeKey(BinaryTokenStreamReader reader)
        {
            var hashCode = reader.ReadInt();
            var count    = reader.ReadUShort();
            var typeName = reader.ReadBytes(count);

            return(new TypeKey(hashCode, typeName));
        }
Exemple #4
0
        private Type ReadNamedType(BinaryTokenStreamReader reader)
        {
            var hashCode = reader.ReadInt();
            var count    = reader.ReadUShort();
            var typeName = reader.ReadBytes(count);

            return(this.typeKeyCache.GetOrAdd(
                       new TypeKey(hashCode, typeName),
                       k => Type.GetType(Encoding.UTF8.GetString(k.TypeName), throwOnError: true)));
        }
        public object Deserialize(Type expectedType, BinaryTokenStreamReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var n = reader.ReadInt();
            var bytes = reader.ReadBytes(n);
            var formatter = new BinaryFormatter();
            object retVal = null;
            using (var memoryStream = new MemoryStream(bytes))
            {
                retVal = formatter.Deserialize(memoryStream);
            }

            return retVal;
        }
Exemple #6
0
        public object Deserialize(Type expectedType, BinaryTokenStreamReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var    n         = reader.ReadInt();
            var    bytes     = reader.ReadBytes(n);
            var    formatter = new BinaryFormatter();
            object retVal    = null;

            using (var memoryStream = new MemoryStream(bytes))
            {
                retVal = formatter.Deserialize(memoryStream);
            }

            return(retVal);
        }
        /// <summary>
        /// Deserializes an object from a binary stream
        /// </summary>
        /// <param name="expectedType">The type that is expected to be deserialized</param>
        /// <param name="reader">The <see cref="BinaryTokenStreamReader"/></param>
        /// <returns>The deserialized object</returns>
        public object Deserialize(Type expectedType, BinaryTokenStreamReader reader)
        {
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var typeHandle = expectedType.TypeHandle;
            object parser = null;
            if (!Parsers.TryGetValue(typeHandle, out parser))
            {
                throw new ArgumentException("No parser found for the expected type " + expectedType, "expectedType");
            }

            int length = reader.ReadInt();
            if (length == 0)
            {
                // the special null case.
                return null;
            }
            byte[] data = reader.ReadBytes(length);

            dynamic dynamicParser = parser;
            object message = dynamicParser.ParseFrom(data);

            return message;
        }
 public static object Deserializer(Type expected, BinaryTokenStreamReader stream) =>
     Deserialize(stream.ReadBytes(stream.ReadInt()));
Exemple #9
0
            public static System.Object Deserializer(System.Type expected,  BinaryTokenStreamReader stream)
            {
                var result = new HeadersContainer();
                Orleans.Serialization.DeserializationContext.Current.RecordObject(result);
                var headers = (Headers)stream.ReadInt();

                if ((headers & Headers.CACHE_INVALIDATION_HEADER) != Headers.NONE)
                {
                    var n = stream.ReadInt();
                    if (n > 0)
                    {
                       var list = result.CacheInvalidationHeader = new List<ActivationAddress>(n);
                        for (int i = 0; i < n; i++)
                        {
                            list.Add((ActivationAddress)ReadObj(typeof(ActivationAddress), stream));
                        }
                    }
                }

                if ((headers & Headers.CATEGORY) != Headers.NONE)
                    result.Category = (Categories)stream.ReadByte();

                if ((headers & Headers.DEBUG_CONTEXT) != Headers.NONE)
                    result.DebugContext = stream.ReadString();

                if ((headers & Headers.DIRECTION) != Headers.NONE)
                    result.Direction = (Message.Directions)stream.ReadByte();

                if ((headers & Headers.EXPIRATION) != Headers.NONE)
                    result.Expiration = stream.ReadDateTime();

                if ((headers & Headers.FORWARD_COUNT) != Headers.NONE)
                    result.ForwardCount = stream.ReadInt();

                if ((headers & Headers.GENERIC_GRAIN_TYPE) != Headers.NONE)
                    result.GenericGrainType = stream.ReadString();

                if ((headers & Headers.CORRELATION_ID) != Headers.NONE)
                    result.Id = (Orleans.Runtime.CorrelationId)ReadObj(typeof(Orleans.Runtime.CorrelationId), stream);

                if ((headers & Headers.ALWAYS_INTERLEAVE) != Headers.NONE)
                    result.IsAlwaysInterleave = ReadBool(stream);

                if ((headers & Headers.IS_NEW_PLACEMENT) != Headers.NONE)
                    result.IsNewPlacement = ReadBool(stream);

                if ((headers & Headers.READ_ONLY) != Headers.NONE)
                    result.IsReadOnly = ReadBool(stream);

                if ((headers & Headers.IS_UNORDERED) != Headers.NONE)
                    result.IsUnordered = ReadBool(stream);

                if ((headers & Headers.NEW_GRAIN_TYPE) != Headers.NONE)
                    result.NewGrainType = stream.ReadString();

                if ((headers & Headers.REJECTION_INFO) != Headers.NONE)
                    result.RejectionInfo = stream.ReadString();

                if ((headers & Headers.REJECTION_TYPE) != Headers.NONE)
                    result.RejectionType = (RejectionTypes)stream.ReadByte();

                if ((headers & Headers.REQUEST_CONTEXT) != Headers.NONE)
                {
                    var c = stream.ReadInt();
                    var requestData = new Dictionary<string, object>(c);
                    for (int i = 0; i < c; i++)
                    {
                        requestData[stream.ReadString()] = SerializationManager.DeserializeInner(null, stream);
                    }
                    result.RequestContextData = requestData;
                }

                if ((headers & Headers.RESEND_COUNT) != Headers.NONE)
                    result.ResendCount = stream.ReadInt();

                if ((headers & Headers.RESULT) != Headers.NONE)
                    result.Result = (Orleans.Runtime.Message.ResponseTypes)stream.ReadByte();

                if ((headers & Headers.SENDING_ACTIVATION) != Headers.NONE)
                    result.SendingActivation = stream.ReadActivationId();

                if ((headers & Headers.SENDING_GRAIN) != Headers.NONE)
                    result.SendingGrain = stream.ReadGrainId();

                if ((headers & Headers.SENDING_SILO) != Headers.NONE)
                    result.SendingSilo = stream.ReadSiloAddress();

                if ((headers & Headers.TARGET_ACTIVATION) != Headers.NONE) 
                    result.TargetActivation = stream.ReadActivationId();

                if ((headers & Headers.TARGET_GRAIN) != Headers.NONE)
                    result.TargetGrain = stream.ReadGrainId();

                if ((headers & Headers.TARGET_OBSERVER) != Headers.NONE)
                    result.TargetObserverId = (Orleans.Runtime.GuidId)ReadObj(typeof(Orleans.Runtime.GuidId), stream);

                if ((headers & Headers.TARGET_SILO) != Headers.NONE)
                    result.TargetSilo = stream.ReadSiloAddress();

                return (HeadersContainer)result;
            }
        private static void VerifyUsingFallbackSerializer(object ob)
        {
            var writer = new BinaryTokenStreamWriter();
            SerializationManager.FallbackSerializer(ob, writer, ob.GetType());
            var bytes = writer.ToByteArray();

            byte[] defaultFormatterBytes;
            var formatter = new BinaryFormatter();
            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, ob);
                stream.Flush();
                defaultFormatterBytes = stream.ToArray();
            }

            var reader = new BinaryTokenStreamReader(bytes);
            var serToken = reader.ReadToken();
            Assert.Equal(SerializationTokenType.Fallback, serToken);
            var length = reader.ReadInt();
            Assert.Equal(length, defaultFormatterBytes.Length);
            var segment = new ArraySegment<byte>(bytes, reader.CurrentPosition, bytes.Length - reader.CurrentPosition);
            Assert.True(segment.SequenceEqual(defaultFormatterBytes));
        }
 /// <summary>
 /// Deserializes an event sequence token
 /// </summary>
 /// <param name="expected">The expected type.</param>
 /// <param name="reader">The binary stream to read from.</param>
 /// <returns></returns>
 public static object Deserialize(Type expected, BinaryTokenStreamReader reader)
 {
     var result = new EventSequenceTokenV2(reader.ReadLong(), reader.ReadInt());
     DeserializationContext.Current.RecordObject(result);
     return result;
 }
 /// <summary>
 /// Deserializes an event sequence token
 /// </summary>
 /// <param name="expected">The expected type.</param>
 /// <param name="reader">The binary stream to read from.</param>
 /// <returns></returns>
 public static object Deserialize(Type expected, BinaryTokenStreamReader reader)
 {
     var deserialized = new EventHubSequenceTokenV2(reader.ReadString(), reader.ReadLong(), reader.ReadInt());
     DeserializationContext.Current.RecordObject(deserialized);
     return deserialized;
 }