コード例 #1
0
        /// <summary>
        /// Encode and write a binary representation of the given
        /// <b>IMessage</b> to the given <see cref="DataWriter"/>.
        /// </summary>
        /// <remarks>
        /// Using the passed <see cref="IChannel"/>, the Codec has access to
        /// both the <see cref="IMessageFactory"/> for the <b>IChannel</b>
        /// and the underlying <see cref="IConnection"/>.
        /// </remarks>
        /// <param name="channel">
        /// The <b>IChannel</b> object through which the binary-encoded
        /// <b>IMessage</b> was passed.
        /// </param>
        /// <param name="message">
        /// The <b>IMessage</b> to encode.
        /// </param>
        /// <param name="writer">
        /// The <b>DataWriter</b> to write the binary representation of the
        /// <b>IMessage</b> to.
        /// </param>
        /// <exception cref="IOException">
        /// If an error occurs encoding or writing the <b>IMessage</b>.
        /// </exception>
        /// <seealso cref="ICodec.Encode"/>
        public virtual void Encode(IChannel channel, IMessage message, DataWriter writer)
        {
            Debug.Assert(channel is IPofContext);
            Debug.Assert(message is IPortableObject);

            IPofContext     context   = (IPofContext)channel;
            PofStreamWriter pofwriter = new PofStreamWriter.UserTypeWriter(writer, context, message.TypeId, 0);

            ISerializer serializer = channel.Serializer;

            if (serializer is ConfigurablePofContext)
            {
                ConfigurablePofContext pofCtx = (ConfigurablePofContext)serializer;
                if (pofCtx.IsReferenceEnabled)
                {
                    pofwriter.EnableReference();
                }
            }

            // set the version identifier
            bool       isEvolvable = message is IEvolvable;
            IEvolvable evolvable   = null;

            if (isEvolvable)
            {
                evolvable           = (IEvolvable)message;
                pofwriter.VersionId = Math.Max(evolvable.DataVersion, evolvable.ImplVersion);
            }

            // write the Message properties
            ((IPortableObject)message).WriteExternal(pofwriter);

            // write the future properties
            pofwriter.WriteRemainder(isEvolvable ? evolvable.FutureData : null);
        }
コード例 #2
0
        public void TestVersionIdNegativeVelueException()
        {
            SimplePofContext ctx    = new SimplePofContext();
            Stream           stream = new MemoryStream();
            IPofWriter       writer = new PofStreamWriter.UserTypeWriter(new DataWriter(stream), ctx, 1, -1);

            writer.VersionId = -1;
        }
コード例 #3
0
        public void TestUserTypeConstuctor()
        {
            SimplePofContext ctx    = new SimplePofContext();
            Stream           stream = new MemoryStream();
            IPofWriter       writer = new PofStreamWriter.UserTypeWriter(new DataWriter(stream),
                                                                         ctx, 1, -1);

            Assert.IsTrue(writer != null);
            Assert.AreEqual(1, writer.UserTypeId);
            Assert.AreEqual(0, writer.VersionId);
        }
コード例 #4
0
        public void TestBeginPropertyWithExcepton1()
        {
            SimplePofContext ctx = new SimplePofContext();

            ctx.RegisterUserType(1, typeof(PortablePerson), new PortableObjectSerializer(1));
            PortablePerson zoja   = new PortablePerson("Zoja", new DateTime(1982, 11, 11, 7, 15, 30));
            Stream         stream = new MemoryStream();
            IPofWriter     writer = new PofStreamWriter.UserTypeWriter(new DataWriter(stream), ctx, 1, -1);

            writer.WriteObject(-1, zoja);
        }
コード例 #5
0
            /// <summary>
            /// Serialize a user type instance to a POF stream by writing its
            /// state using the specified <see cref="IPofWriter"/> object.
            /// </summary>
            /// <remarks>
            /// An implementation of <b>IPofSerializer</b> is required to follow
            /// the following steps in sequence for writing out an object of a
            /// user type:
            /// <list type="number">
            /// <item>
            /// <description>
            /// If the object is evolvable, the implementation must set the
            /// version by calling <see cref="IPofWriter.VersionId"/>.
            /// </description>
            /// </item>
            /// <item>
            /// <description>
            /// The implementation may write any combination of the properties of
            /// the user type by using the "write" methods of the
            /// <b>IPofWriter</b>, but it must do so in the order of the property
            /// indexes.
            /// </description>
            /// </item>
            /// <item>
            /// <description>
            /// After all desired properties of the user type have been written,
            /// the implementation must terminate the writing of the user type by
            /// calling <see cref="IPofWriter.WriteRemainder"/>.
            /// </description>
            /// </item>
            /// </list>
            /// </remarks>
            /// <param name="writer">
            /// The <b>IPofWriter</b> with which to write the object's state.
            /// </param>
            /// <param name="o">
            /// The object to serialize.
            /// </param>
            /// <exception cref="IOException">
            /// If an I/O error occurs.
            /// </exception>
            public void Serialize(IPofWriter writer, Object o)
            {
                var             buffer         = new BinaryMemoryStream(1024 * 8);
                PofStreamWriter userTypeWriter = new PofStreamWriter.UserTypeWriter(
                    new DataWriter(buffer), m_pofContext, TYPE_PORTABLE, -1);

                // COH-5065: due to the complexity of maintaining references
                // in future data, we won't support them for IEvolvable objects
                if (m_pofContext.IsReferenceEnabled && !(o is IEvolvable))
                {
                    userTypeWriter.EnableReference();
                }

                m_serializer.Serialize(userTypeWriter, o);

                String typeName = o.GetType().AssemblyQualifiedName;

                writer.WriteString(0, typeName);
                writer.WriteBinary(1, buffer.ToBinary());
                writer.WriteRemainder(null);

                Register(typeName);
            }