Esempio n. 1
0
        public void UniqueKeySerializationShouldReproduceAnIdenticalObject()
        {
            {
                var expected = UniqueKey.NewKey(Guid.NewGuid());
                BinaryTokenStreamWriter writer = new BinaryTokenStreamWriter();
                writer.Write(expected);
                BinaryTokenStreamReader reader = new BinaryTokenStreamReader(writer.ToBytes());
                var actual = reader.ReadUniqueKey();
                Assert.AreEqual(expected, actual, "UniqueKey.Serialize() and UniqueKey.Deserialize() failed to reproduce an identical object (case #1).");
            }

            {
                var kx = random.Next().ToString(CultureInfo.InvariantCulture);
                var expected = UniqueKey.NewKey(Guid.NewGuid(), category: UniqueKey.Category.KeyExtGrain, keyExt: kx);
                BinaryTokenStreamWriter writer = new BinaryTokenStreamWriter();
                writer.Write(expected);
                BinaryTokenStreamReader reader = new BinaryTokenStreamReader(writer.ToBytes());
                var actual = reader.ReadUniqueKey();
                Assert.AreEqual(expected, actual, "UniqueKey.Serialize() and UniqueKey.Deserialize() failed to reproduce an identical object (case #2).");
            }

            {
                var kx = random.Next().ToString(CultureInfo.InvariantCulture) + new String('*', 400);
                var expected = UniqueKey.NewKey(Guid.NewGuid(), category: UniqueKey.Category.KeyExtGrain, keyExt: kx);
                BinaryTokenStreamWriter writer = new BinaryTokenStreamWriter();
                writer.Write(expected);
                BinaryTokenStreamReader reader = new BinaryTokenStreamReader(writer.ToBytes());
                var actual = reader.ReadUniqueKey();
                Assert.AreEqual(expected, actual, "UniqueKey.Serialize() and UniqueKey.Deserialize() failed to reproduce an identical object (case #3).");
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Returns a new nested context which begins at the specified position.
 /// </summary>
 /// <param name="context"></param>
 /// <param name="position"></param>
 /// <param name="reader"></param>
 /// <returns></returns>
 public static IDeserializationContext CreateNestedContext(
     this IDeserializationContext context,
     int position,
     BinaryTokenStreamReader reader)
 {
     return(new DeserializationContext.NestedDeserializationContext(context, position, reader));
 }
Esempio n. 3
0
        public void ILSerializer_AllowCopiedFieldsToDifferFromSerializedFields()
        {
            var input = new FieldTest
            {
                One = 1,
                Two = 2,
                Three = 3
            };

            var generator = new ILSerializerGenerator();
            var serializers = generator.GenerateSerializer(input.GetType(), f => f.Name != "One", f => f.Name != "Three");
            var copy = (FieldTest)serializers.DeepCopy(input);
            Assert.Equal(1, copy.One);
            Assert.Equal(2, copy.Two);
            Assert.Equal(0, copy.Three);

            var writer = new BinaryTokenStreamWriter();
            serializers.Serialize(input, writer, input.GetType());
            var reader = new BinaryTokenStreamReader(writer.ToByteArray());
            var deserialized = (FieldTest)serializers.Deserialize(input.GetType(), reader);

            Assert.Equal(0, deserialized.One);
            Assert.Equal(2, deserialized.Two);
            Assert.Equal(3, deserialized.Three);
        }
Esempio n. 4
0
        /// <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("reader");
            }

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

            var typeHandle   = expectedType.TypeHandle;
            var deserializer = GetDeserializer(typeHandle);

            if (deserializer == null)
            {
                LogWarning(3, "no deserializer found for type {0}", expectedType.FullName);
                throw new ArgumentOutOfRangeException("no deserializer provided for the selected type", "expectedType");
            }

            var inputStream = InputStream.Create(reader);
            var bondReader  = new BondBinaryReader(inputStream);

            return(deserializer.Deserialize(bondReader));
        }
        /// <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);
        }
Esempio n. 6
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));
        }
 public static object Deserializer(System.Type expected, Orleans.Serialization.BinaryTokenStreamReader stream)
 {
     Test.Interfaces.PersonalAttributes result = new Test.Interfaces.PersonalAttributes();
     result.FirstName = ((String)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(String), stream)));
     result.Gender    = ((GenderType)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(GenderType), stream)));
     result.LastName  = ((String)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(String), stream)));
     return(result);
 }
Esempio n. 8
0
 private static object Deserialize(Type expected, BinaryTokenStreamReader stream)
 {
     DeserializeCounter++;
     var result = new ClassWithCustomSerializer();
     result.IntProperty = stream.ReadInt();
     result.StringProperty = stream.ReadString();
     return result;
 }
 /// <summary>
 /// Tries to deserialize an item.
 /// </summary>
 /// <param name="reader">The reader used for binary deserialization</param>
 /// <param name="expectedType">The type that should be deserialzied</param>
 /// <returns>The deserialized object</returns>
 public object Deserialize(Type expectedType, BinaryTokenStreamReader reader)
 {
     var token = reader.ReadToken();
     if (token == SerializationTokenType.Null) return null;
     var actualType = this.ReadType(token, reader, expectedType);
     var methods = this.serializers.Get(actualType);
     var deserializer = methods.Deserialize;
     return deserializer(expectedType, reader);
 }
Esempio n. 10
0
        internal static InputStream Create(BinaryTokenStreamReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            return(new InputStream(reader));
        }
Esempio n. 11
0
        public static void VerifyUsingFallbackSerializer(object ob)
        {
            var writer = new BinaryTokenStreamWriter();
            SerializationManager.FallbackSerializer(ob, writer, ob.GetType());
            var bytes = writer.ToByteArray();

            var reader = new BinaryTokenStreamReader(bytes);
            var serToken = reader.ReadToken();
            Assert.Equal(SerializationTokenType.Fallback, serToken);
        }
Esempio n. 12
0
 public static object Deserializer(System.Type expected, Orleans.Serialization.BinaryTokenStreamReader stream)
 {
     GPSTracker.Common.DeviceMessage result = new GPSTracker.Common.DeviceMessage();
     result.DeviceId  = ((Guid)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(Guid), stream)));
     result.Latitude  = ((Double)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(Double), stream)));
     result.Longitude = ((Double)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(Double), stream)));
     result.MessageId = ((Int32)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(Int32), stream)));
     result.Timestamp = ((DateTime)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(DateTime), stream)));
     return(result);
 }
Esempio n. 13
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)));
        }
Esempio n. 14
0
        /// <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 (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var str = reader.ReadString();
            return JsonConvert.DeserializeObject(str, expectedType, settings);
        }
Esempio n. 15
0
        /// <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 (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var str = reader.ReadString();

            return(JsonConvert.DeserializeObject(str, expectedType, defaultSettings));
        }
Esempio n. 16
0
 public static object Deserializer(System.Type expected, Orleans.Serialization.BinaryTokenStreamReader stream)
 {
     GPSTracker.Common.Device result = new GPSTracker.Common.Device();
     result.Description = ((String)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(String), stream)));
     result.DeviceId    = ((Guid)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(Guid), stream)));
     result.Direction   = ((Double)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(Double), stream)));
     result.Lat         = ((Double)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(Double), stream)));
     result.Lon         = ((Double)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(Double), stream)));
     result.Speed       = ((Double)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(Double), stream)));
     return(result);
 }
 public void EventSequenceTokenV2_Serialize_IfNotNull()
 {
     var writer = new BinaryTokenStreamWriter();
     var token = new EventSequenceTokenV2(long.MaxValue, int.MaxValue);
     EventSequenceTokenV2.Serialize(token, writer, null);
     var reader = new BinaryTokenStreamReader(writer.ToByteArray());
     var deserialized = EventSequenceTokenV2.Deserialize(typeof(EventSequenceTokenV2), reader) as EventSequenceTokenV2;
     Assert.NotNull(deserialized);
     Assert.NotSame(token, deserialized);
     Assert.Equal(token.EventIndex, deserialized.EventIndex);
     Assert.Equal(token.SequenceNumber, deserialized.SequenceNumber);
 }
Esempio n. 18
0
        internal static object Deserialize(Type unused, BinaryTokenStreamReader stream)
        {
            byte resultToken = stream.ReadByte();

            if (resultToken == ResultToken.Null)
                return new ResponseEnvelope(null);

            if (resultToken != ResultToken.Some)
                throw new NotSupportedException();

            var result = MessageEnvelope.Serializer.Deserialize(stream);
            return new ResponseEnvelope(result);
        }
Esempio n. 19
0
        internal static object Reserialize(IMessageSerializer serializer, object message)
        {
            if (serializer == null)
                return message;

            var writer = new BinaryTokenStreamWriter();
            serializer.Serialize(message, writer);

            var bytes = writer.ToByteArray();
            var reader = new BinaryTokenStreamReader(bytes);

            return serializer.Deserialize(reader);
        }
Esempio n. 20
0
        /// <summary>
        /// Tries to deserialize an item.
        /// </summary>
        /// <param name="expectedType">The type that should be deserialzied</param>
        /// <param name="reader">The reader used for binary deserialization</param>
        /// <returns>The deserialized object</returns>
        public object Deserialize(Type expectedType, BinaryTokenStreamReader reader)
        {
            var token = reader.ReadToken();

            if (token == SerializationTokenType.Null)
            {
                return(null);
            }
            var actualType = this.ReadType(token, reader, expectedType);

            return(this.serializers.GetOrAdd(actualType, this.generateSerializer)
                   .Methods.Deserialize(expectedType, reader));
        }
        private Type ReadType(SerializationTokenType token, BinaryTokenStreamReader reader, Type expectedType)
        {
            switch (token)
            {
            case SerializationTokenType.ExpectedType:
                return(expectedType);

            case SerializationTokenType.NamedType:
                return(Type.GetType(reader.ReadString(), throwOnError: true));

            default:
                throw new NotSupportedException($"{nameof(SerializationTokenType)} of {token} is not supported.");
            }
        }
        /// <summary>
        /// Tries to deserialize an item.
        /// </summary>
        /// <param name="reader">The reader used for binary deserialization</param>
        /// <param name="expectedType">The type that should be deserialzied</param>
        /// <returns>The deserialized object</returns>
        public object Deserialize(Type expectedType, BinaryTokenStreamReader reader)
        {
            var token = reader.ReadToken();

            if (token == SerializationTokenType.Null)
            {
                return(null);
            }
            var actualType   = this.ReadType(token, reader, expectedType);
            var methods      = this.serializers.Get(actualType);
            var deserializer = methods.Deserialize;

            return(deserializer(expectedType, reader));
        }
Esempio n. 23
0
        public Type ReadType(SerializationTokenType token, BinaryTokenStreamReader reader, Type expectedType)
        {
            switch (token)
            {
            case SerializationTokenType.ExpectedType:
                return(expectedType);

            case SerializationTokenType.NamedType:
                return(this.ReadNamedType(reader));

            default:
                throw new NotSupportedException($"{nameof(SerializationTokenType)} of {token} is not supported.");
            }
        }
        public void AzureQueueBatchContainerV2_Serialize_IfNotNull()
        {
            var container = CreateAzureQueueBatchContainer();
            var writer = new BinaryTokenStreamWriter();
            AzureQueueBatchContainerV2.Serialize(container, writer, null);
            var reader = new BinaryTokenStreamReader(writer.ToByteArray());
            var deserialized = AzureQueueBatchContainerV2.Deserialize(typeof(AzureQueueBatchContainer), reader) as AzureQueueBatchContainerV2;
            ValidateIdenticalQueueBatchContainerButNotSame(container, deserialized);

            writer = new BinaryTokenStreamWriter();
            SerializationManager.Serialize(container, writer);
            reader = new BinaryTokenStreamReader(writer.ToByteArray());
            deserialized = SerializationManager.Deserialize<AzureQueueBatchContainerV2>(reader);
            ValidateIdenticalQueueBatchContainerButNotSame(container, deserialized);
        }
        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;
        }
Esempio n. 26
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);
        }
        public void EventSequenceTokenV2_DeepCopy_IfNotNull()
        {
            var token = new EventSequenceTokenV2(long.MaxValue, int.MaxValue);
            var copy = EventSequenceTokenV2.DeepCopy(token) as EventSequenceToken;
            Assert.NotNull(copy);
            Assert.NotSame(token, copy);
            Assert.Equal(token.EventIndex, copy.EventIndex);
            Assert.Equal(token.SequenceNumber, copy.SequenceNumber);

            var writer = new BinaryTokenStreamWriter();
            SerializationManager.Serialize(token, writer);
            var bytes = writer.ToByteArray();

            var reader = new BinaryTokenStreamReader(bytes);
            copy = SerializationManager.Deserialize(reader) as EventSequenceToken;
            Assert.NotNull(copy);
            Assert.NotSame(token, copy);
            Assert.Equal(token.EventIndex, copy.EventIndex);
            Assert.Equal(token.SequenceNumber, copy.SequenceNumber);
        }
Esempio n. 28
0
 /// <summary>
 /// Initializes a new <see cref="NestedDeserializationContext"/> instance.
 /// </summary>
 /// <param name="parent"></param>
 /// <param name="position">The position, relative to the outer-most context, at which this context begins.</param>
 /// <param name="reader"></param>
 public NestedDeserializationContext(IDeserializationContext parent, int position, BinaryTokenStreamReader reader)
 {
     this.position     = position;
     this.parent       = parent;
     this.StreamReader = reader;
 }
        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));
        }
Esempio n. 30
0
 public static object Deserializer(System.Type expected, Orleans.Serialization.BinaryTokenStreamReader stream)
 {
     Orleans.PingPong.Message result = new Orleans.PingPong.Message();
     return(result);
 }
Esempio n. 31
0
 /// <summary>
 /// Called from generated code.
 /// </summary>
 /// <param name="stream">Stream to recover / repopulate this grain state object from.</param>
 public void DeserializeFrom(BinaryTokenStreamReader stream)
 {
     var values = (Dictionary<string, object>)SerializationManager.DeserializeInner(wireFormatType, stream);
     this.SetAllInternal(values);
 }
Esempio n. 32
0
 /// <summary>
 /// Deserializes event data properties
 /// </summary>
 /// <param name="bytes"></param>
 /// <returns></returns>
 public static IDictionary<string, object> DeserializeProperties(this ArraySegment<byte> bytes)
 {
     var stream = new BinaryTokenStreamReader(bytes);
     return SerializationManager.Deserialize<List<KeyValuePair<string, object>>>(stream).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
 }
 public static object _Deserializer(System.Type expected, Orleans.Serialization.BinaryTokenStreamReader stream)
 {
     return(PersonReference.Cast(((Orleans.GrainReference)(GrainReference.DeserializeGrainReference(expected, stream)))));
 }
Esempio n. 34
0
 private static object Deserialize(Type expected, BinaryTokenStreamReader stream)
 {
     DeserializeWasCalled = true;
     return null;
 }
 public static object Deserializer(System.Type expected, Orleans.Serialization.BinaryTokenStreamReader stream)
 {
     Frontend.Library.Models.GameServerStartParams result = new Frontend.Library.Models.GameServerStartParams();
     result.GameServerID = ((GameServerID)(Orleans.Serialization.SerializationManager.DeserializeInner(typeof(GameServerID), stream)));
     return(result);
 }
Esempio n. 36
0
 public object Deserialize(Type expectedType, BinaryTokenStreamReader reader)
 {
     DeserializeCalled = true;
     return null;
 }
 public static object Deserializer(Type expected, BinaryTokenStreamReader stream) =>
     Deserialize(stream.ReadBytes(stream.ReadInt()));
Esempio n. 38
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;
            }
Esempio n. 39
0
        public Type ReadNamedType(BinaryTokenStreamReader reader)
        {
            var key = ReadTypeKey(reader);

            return(this.GetTypeFromTypeKey(key, throwOnError: true));
        }
Esempio n. 40
0
 object IMessageSerializer.Deserialize(BinaryTokenStreamReader stream)
 {
     var bytes = (byte[]) SerializationManager.DeserializeInner(typeof(byte[]), stream);
     var json  = Encoding.Default.GetString(bytes);
     return JsonConvert.DeserializeObject(json, settings);
 }
Esempio n. 41
0
        /// <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;
        }
Esempio n. 42
0
 private InputStream(BinaryTokenStreamReader reader)
 {
     this.reader = reader;
 }
Esempio n. 43
0
 public object Deserialize(Type expectedType, BinaryTokenStreamReader reader)
 {
     DeserializeCalled = true;
     return new FakeSerialized { SomeData = "fake deserialization" };
 }
 private static StreamSequenceToken TokenFromBytes(byte[] bytes)
 {
     var stream = new BinaryTokenStreamReader(bytes);
     return SerializationManager.Deserialize<StreamSequenceToken>(stream);
 }
 public static object Deserializer(Type expected, BinaryTokenStreamReader stream)
 {
     var str = (string)(SerializationManager.Deserialize(typeof(string), stream));
     return JObject.Parse(str);
 }
Esempio n. 46
0
        public void Serialize_CustomSerializer()
        {
            var original = new ClassWithCustomSerializer() { IntProperty = -3, StringProperty = "Goodbye" };
            var writeStream = new BinaryTokenStreamWriter();
            SerializationManager.Serialize(original, writeStream);
            Assert.AreEqual(1, ClassWithCustomSerializer.SerializeCounter, "Custom serializer was not called");

            var readStream = new BinaryTokenStreamReader(writeStream.ToBytes());
            var obj = SerializationManager.Deserialize(readStream);
            Assert.AreEqual(1, ClassWithCustomSerializer.DeserializeCounter, "Custom deserializer was not called");
        }
 public static object Deserialize(Type expected, BinaryTokenStreamReader stream)
 {
     var str = (string)SerializationManager.Deserialize(typeof(string), stream);
     return JsonConvert.DeserializeObject(str, expected);
 }
Esempio n. 48
0
 private static object DeserializeBody(List<ArraySegment<byte>> bytes)
 {
     if (bytes == null)
     {
         return null;
     }
     try
     {
         var stream = new BinaryTokenStreamReader(bytes);
         return SerializationManager.Deserialize(stream);
     }
     catch (Exception ex)
     {
         logger.Error(ErrorCode.Messaging_UnableToDeserializeBody, "Exception deserializing message body", ex);
         throw;
     }
 }
Esempio n. 49
0
 private static object ReadObj(Type t, BinaryTokenStreamReader stream)
 {
     var des = SerializationManager.GetDeserializer(t);
     return des.Invoke(t, stream);
 }
Esempio n. 50
0
 private static bool ReadBool(BinaryTokenStreamReader stream)
 {
     return stream.ReadByte() == (byte) SerializationTokenType.True;
 }
Esempio n. 51
0
 // Initializes body and header but does not take ownership of byte.
 // Caller must clean up bytes
 public Message(List<ArraySegment<byte>> header, List<ArraySegment<byte>> body, bool deserializeBody = false)
 {
     var input = new BinaryTokenStreamReader(header);
     Headers = SerializationManager.DeserializeMessageHeaders(input);
     if (deserializeBody)
     {
         bodyObject = DeserializeBody(body);
     }
     else
     {
         bodyBytes = body;
     }
 }