public void Serialize(object item, BinaryTokenStreamWriter writer, Type expectedType)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (item == null)
            {
                writer.WriteNull();
                return;
            }

            var formatter = new BinaryFormatter();
            byte[] bytes;
            using (var memoryStream = new MemoryStream())
            {
                formatter.Serialize(memoryStream, item);
                memoryStream.Flush();
                bytes = memoryStream.ToArray();
            }
            
            writer.Write(bytes.Length);
            writer.Write(bytes);
        }
 private static void Serialize(object input, BinaryTokenStreamWriter stream, Type expected)
 {
     SerializeCounter++;
     var obj = input as ClassWithCustomSerializer;
     stream.Write(obj.IntProperty);
     stream.Write(obj.StringProperty);
 }
        /// <summary>
        /// Serialize the event sequence token.
        /// </summary>
        /// <param name="untypedInput">The object to serialize.</param>
        /// <param name="writer">The writer to write the binary stream to.</param>
        /// <param name="expected">The expected type.</param>
        public static void Serialize(object untypedInput, BinaryTokenStreamWriter writer, Type expected)
        {
            var typed = untypedInput as EventSequenceTokenV2;
            if (typed == null)
            {
                writer.WriteNull();
                return;
            }

            writer.Write(typed.SequenceNumber);
            writer.Write(typed.EventIndex);
        }
        internal static void Serialize(object obj, BinaryTokenStreamWriter stream, Type unused)
        {
            var envelope = (ResponseEnvelope)obj;

            if (envelope.Result == null)
            {
                stream.Write(ResultToken.Null);
                return;
            }

            stream.Write(ResultToken.Some);
            MessageEnvelope.Serializer.Serialize(envelope.Result, stream);
        }
Exemple #5
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).");
            }
        }
        /// <summary>
        /// Serializes an object to a binary stream
        /// </summary>
        /// <param name="item">The object to serialize</param>
        /// <param name="writer">The <see cref="BinaryTokenStreamWriter"/></param>
        /// <param name="expectedType">The type the deserializer should expect</param>
        public void Serialize(object item, BinaryTokenStreamWriter writer, Type expectedType)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (item == null)
            {
                // Special handling for null value. 
                // Since in this ProtobufSerializer we are usualy writing the data lengh as 4 bytes
                // we also have to write the Null object as 4 bytes lengh of zero.
                writer.Write(0);
                return;
            }

            IMessage iMessage = item as IMessage;
            if (iMessage == null)
            {
                throw new ArgumentException("The provided item for serialization in not an instance of " + typeof(IMessage), "item");
            }
            // The way we write the data is potentially in-efficinet, 
            // since we are first writing to ProtoBuff's internal CodedOutputStream
            // and then take its internal byte[] and write it into out own BinaryTokenStreamWriter.
            // Writing byte[] to BinaryTokenStreamWriter may sometimes copy the byte[] and sometimes just append ass ArraySegment without copy.
            // In the former case it will be a secodnd copy.
            // It would be more effecient to write directly into BinaryTokenStreamWriter
            // but protobuff does not currently support writing directly into a given arbitary stream
            // (it does support System.IO.Steam but BinaryTokenStreamWriter is not compatible with System.IO.Steam).
            // Alternatively, we could force to always append to BinaryTokenStreamWriter, but that could create a lot of small ArraySegments.
            // The plan is to ask the ProtoBuff team to add support for some "InputStream" interface, like Bond does.
            byte[] outBytes = iMessage.ToByteArray();
            writer.Write(outBytes.Length);
            writer.Write(outBytes);
        }
 private void WriteType(Type actualType, Type expectedType, BinaryTokenStreamWriter writer)
 {
     if (actualType == expectedType)
     {
         writer.Write((byte)SerializationTokenType.ExpectedType);
     }
     else
     {
         writer.Write((byte)SerializationTokenType.NamedType);
         writer.Write(actualType.AssemblyQualifiedName);
     }
 }
        /// <summary>
        /// Serializes an object to a binary stream
        /// </summary>
        /// <param name="item">The object to serialize</param>
        /// <param name="writer">The <see cref="BinaryTokenStreamWriter"/></param>
        /// <param name="expectedType">The type the deserializer should expect</param>
        public void Serialize(object item, BinaryTokenStreamWriter writer, Type expectedType)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (item == null)
            {
                writer.WriteNull();
                return;
            }

            var str = JsonConvert.SerializeObject(item, expectedType, settings);

            writer.Write(str);
        }
        /// <summary>
        /// Serializes an object to a binary stream
        /// </summary>
        /// <param name="item">The object to serialize</param>
        /// <param name="writer">The <see cref="BinaryTokenStreamWriter"/></param>
        /// <param name="expectedType">The type the deserializer should expect</param>
        public void Serialize(object item, BinaryTokenStreamWriter writer, Type expectedType)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (item == null)
            {
                writer.WriteNull();
                return;
            }

            var str = JsonConvert.SerializeObject(item, expectedType, defaultSettings);
            writer.Write(str);
        }
 public static void Serializer(object untypedInput, BinaryTokenStreamWriter stream, Type expected)
 {
     byte[] buffer = Serialize(untypedInput);
     stream.Write(buffer.Length);
     stream.Write(buffer);
 }
Exemple #11
0
            public static void Serializer(System.Object untypedInput,  BinaryTokenStreamWriter stream, System.Type expected)
            {
                HeadersContainer input = (HeadersContainer)untypedInput;
                var headers = input.GetHeadersMask();
                stream.Write((int)headers);
                if ((headers & Headers.CACHE_INVALIDATION_HEADER) != Headers.NONE)
                {
                    var count = input.CacheInvalidationHeader.Count;
                    stream.Write(input.CacheInvalidationHeader.Count);
                    for (int i = 0; i < count; i++)
                    {
                        WriteObj(stream, typeof(ActivationAddress), input.CacheInvalidationHeader[i]);
                    }
                }

                if ((headers & Headers.CATEGORY) != Headers.NONE)
                {
                    stream.Write((byte)input.Category);
                }

                if ((headers & Headers.DEBUG_CONTEXT) != Headers.NONE)
                    stream.Write(input.DebugContext);

                if ((headers & Headers.DIRECTION) != Headers.NONE)
                    stream.Write((byte)input.Direction.Value);

                if ((headers & Headers.EXPIRATION) != Headers.NONE)
                    stream.Write(input.Expiration.Value);

                if ((headers & Headers.FORWARD_COUNT) != Headers.NONE)
                    stream.Write(input.ForwardCount);

                if ((headers & Headers.GENERIC_GRAIN_TYPE) != Headers.NONE)
                    stream.Write(input.GenericGrainType);

                if ((headers & Headers.CORRELATION_ID) != Headers.NONE)
                    stream.Write(input.Id);

                if ((headers & Headers.ALWAYS_INTERLEAVE) != Headers.NONE)
                    stream.Write(input.IsAlwaysInterleave);

                if ((headers & Headers.IS_NEW_PLACEMENT) != Headers.NONE)
                    stream.Write(input.IsNewPlacement);

                if ((headers & Headers.READ_ONLY) != Headers.NONE)
                    stream.Write(input.IsReadOnly);

                if ((headers & Headers.IS_UNORDERED) != Headers.NONE)
                    stream.Write(input.IsUnordered);

                if ((headers & Headers.NEW_GRAIN_TYPE) != Headers.NONE)
                    stream.Write(input.NewGrainType);

                if ((headers & Headers.REJECTION_INFO) != Headers.NONE)
                    stream.Write(input.RejectionInfo);

                if ((headers & Headers.REJECTION_TYPE) != Headers.NONE)
                    stream.Write((byte)input.RejectionType);

                if ((headers & Headers.REQUEST_CONTEXT) != Headers.NONE)
                {
                    var requestData = input.RequestContextData;
                    var count = requestData.Count;
                    stream.Write(count);
                    foreach (var d in requestData)
                    {
                        stream.Write(d.Key);
                        SerializationManager.SerializeInner(d.Value, stream, typeof(object));
                    }
                }

                if ((headers & Headers.RESEND_COUNT) != Headers.NONE)
                    stream.Write(input.ResendCount);

                if ((headers & Headers.RESULT) != Headers.NONE)
                    stream.Write((byte)input.Result);

                if ((headers & Headers.SENDING_ACTIVATION) != Headers.NONE)
                {
                    stream.Write(input.SendingActivation);
                }

                if ((headers & Headers.SENDING_GRAIN) != Headers.NONE)
                {
                    stream.Write(input.SendingGrain);
                }

                if ((headers & Headers.SENDING_SILO) != Headers.NONE)
                {
                    stream.Write(input.SendingSilo);
                }

                if ((headers & Headers.TARGET_ACTIVATION) != Headers.NONE)
                {
                    stream.Write(input.TargetActivation);
                }

                if ((headers & Headers.TARGET_GRAIN) != Headers.NONE)
                {
                    stream.Write(input.TargetGrain);
                }

                if ((headers & Headers.TARGET_OBSERVER) != Headers.NONE)
                {
                    WriteObj(stream, typeof(GuidId), input.TargetObserverId);
                }

                if ((headers & Headers.TARGET_SILO) != Headers.NONE)
                {
                    stream.Write(input.TargetSilo);
                }
            }
 internal static void SerializeGrainCancellationToken(object obj, BinaryTokenStreamWriter stream, Type expected)
 {
     var ctw = (GrainCancellationToken)obj;
     var canceled = ctw.CancellationToken.IsCancellationRequested;
     stream.Write(canceled);
     stream.Write(ctw.Id);
 }
 internal static void Serialize(object obj, BinaryTokenStreamWriter stream, Type unused)
 {
     var envelope = (RequestEnvelope) obj;
     stream.Write(envelope.Target);
     MessageEnvelope.Serializer.Serialize(envelope.Message, stream);
 }
 public void Serialize(object item, BinaryTokenStreamWriter writer, Type expectedType)
 {
     var typed = (SimpleType)item;
     writer.Write(typed.Number);
 }
Exemple #15
0
 public static void WriteTypeKey(TypeKey key, BinaryTokenStreamWriter writer)
 {
     writer.Write(key.HashCode);
     writer.Write((ushort)key.TypeName.Length);
     writer.Write(key.TypeName);
 }