/// <summary>
        /// Return a sorted set of type identifiers for all user types
        /// in a class hierarchy.
        /// </summary>
        /// <param name="o">The object to return type identifiers for</param>
        /// <param name="ctx">The POF context</param>
        /// <returns>
        /// A sorted enumeration of type identifiers for all user types
        /// in a class hierarchy.
        /// </returns>
        private IEnumerable <int> GetTypeIds(Object o, IPofContext ctx)
        {
            List <int> typeIds = new List <int>();

            Type type = o.GetType();

            while (type != null && ctx.IsUserType(type))
            {
                typeIds.Add(ctx.GetUserTypeIdentifier(type));
                type = type.BaseType;
            }

            if (o is IEvolvableObject)
            {
                EvolvableHolder evolvableHolder = ((IEvolvableObject)o).GetEvolvableHolder();
                if (!evolvableHolder.IsEmpty)
                {
                    foreach (int typeId in evolvableHolder.TypeIds)
                    {
                        typeIds.Add(typeId);
                    }
                }
            }

            typeIds.Sort();
            return(typeIds);
        }
            // ----- constructors -------------------------------------------

            /// <summary>
            /// Construct a SimplePofValue instance wrapping the supplied
            /// binary.
            /// </summary>
            /// <param name="valueParent">
            /// Parent value within the POF stream.
            /// </param>
            /// <param name="ctx">
            /// POF context to use when reading or writing properties.
            /// </param>
            /// <param name="of">
            /// Offset of this value from the beginning of POF stream.
            /// </param>
            /// <param name="nType">
            /// POF type identifier for this value.
            /// </param>
            /// <param name="nIndex">
            /// Index of this value within the parent sparse array.
            /// </param>
            public NilPofValue(IPofValue valueParent, IPofContext ctx,
                               int of, int nType, int nIndex)
                : base(valueParent, Binary.NO_BINARY, ctx, of, nType)
            {
                m_oValue = null;
                m_nIndex = nIndex;
            }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads a binary-encoded <b>IMessage</b> from the passed
        /// <see cref="DataReader"/> object.
        /// </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="reader">
        /// The <b>DataReader</b> containing the binary-encoded
        /// <b>IMessage</b>.
        /// </param>
        /// <returns>
        /// The <b>IMessage</b> object encoded in the given
        /// <b>DataReader</b>.
        /// </returns>
        /// <exception cref="IOException">
        /// If an error occurs reading or decoding the <b>IMessage</b>.
        /// </exception>
        public virtual IMessage Decode(IChannel channel, DataReader reader)
        {
            Debug.Assert(channel is IPofContext);

            IPofContext context   = (IPofContext)channel;
            int         typeId    = reader.ReadPackedInt32();
            int         versionId = reader.ReadPackedInt32();
            IPofReader  pofreader = new PofStreamReader.UserTypeReader(reader, context, typeId, versionId);
            IMessage    message   = channel.MessageFactory.CreateMessage(typeId);

            Debug.Assert(message is IPortableObject);

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

            if (isEvolvable)
            {
                evolvable             = (IEvolvable)message;
                evolvable.DataVersion = versionId;
            }

            // read the Message properties
            ((IPortableObject)message).ReadExternal(pofreader);

            // read the future properties
            Binary binFuture = pofreader.ReadRemainder();

            if (isEvolvable)
            {
                evolvable.FutureData = binFuture;
            }

            return(message);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Construct a ComplexPofValue instance wrapping the supplied binary.
 /// </summary>
 /// <param name="valueParent">
 /// Parent value within the POF stream.
 /// </param>
 /// <param name="binValue">
 /// Binary representation of this value.
 /// </param>
 /// <param name="ctx">
 /// POF context to use when reading or writing properties.
 /// </param>
 /// <param name="of">
 /// Offset of this value from the beginning of POF stream.
 /// </param>
 /// <param name="nType">
 /// POF type identifier for this value.
 /// </param>
 /// <param name="ofChildren">
 /// Offset of the first child element within this value.
 /// </param>
 public ComplexPofValue(IPofValue valueParent, Binary binValue,
                        IPofContext ctx, int of, int nType, int ofChildren)
     : base(valueParent, binValue, ctx, of, nType)
 {
     m_aChildren  = new LongSortedList();
     m_ofChildren = ofChildren;
 }
 public ModificationComponentFactory(IFileSystemProxy fileSystemProxy, IPofContext pofContext, SlotSourceFactory slotSourceFactory, IPofSerializer serializer)
 {
     this.fileSystemProxy   = fileSystemProxy;
     this.pofContext        = pofContext;
     this.slotSourceFactory = slotSourceFactory;
     this.serializer        = serializer;
 }
        /// <summary>
        /// Compute the expected pof type id based on the type.
        /// </summary>
        /// <param name="ctx">
        /// Pof context.
        /// </param>
        /// <returns>
        /// Pof type id or <see cref="PofConstants.T_UNKNOWN"/> if the type
        /// is <c>null</c>.
        /// </returns>
        protected int GetPofTypeId(IPofContext ctx)
        {
            Type type = m_type;

            return(m_type == null
                    ? m_typeId
                    : PofHelper.GetPofTypeId(type, ctx));
        }
        /// <summary>
        /// Parses POF-encoded binary and returns an instance of a
        /// <see cref="IPofValue"/> wrapper for it.
        /// </summary>
        /// <param name="valueParent">
        /// Parent POF value.
        /// </param>
        /// <param name="binValue">
        /// POF-encoded binary value.
        /// </param>
        /// <param name="ctx">
        /// POF context to use.
        /// </param>
        /// <param name="of">
        /// Offset of the parsed value from the beginning of the POF stream.
        /// </param>
        /// <returns>
        /// An IPofValue instance.
        /// </returns>
        internal static IPofValue ParseValue(IPofValue valueParent, Binary binValue,
                                             IPofContext ctx, int of)
        {
            DataReader reader = binValue.GetReader();
            int        nType  = reader.ReadPackedInt32();

            return(InstantiatePofValue(valueParent, nType, binValue, ctx, of, reader));
        }
        /// <summary>
        /// Parses POF-encoded binary and returns an instance of a
        /// <see cref="IPofValue"/> wrapper for it.
        /// </summary>
        /// <param name="valueParent">
        /// Parent POF value.
        /// </param>
        /// <param name="nType">
        /// Type identifier of this POF value.
        /// </param>
        /// <param name="binValue">
        /// POF-encoded binary value.
        /// </param>
        /// <param name="ctx">
        /// POF context to use.
        /// </param>
        /// <param name="of">
        /// Offset of the parsed value from the beginning of the POF stream.
        /// </param>
        /// <returns>
        /// An IPofValue instance.
        /// </returns>
        internal static IPofValue ParseUniformValue(IPofValue valueParent,
                                                    int nType, Binary binValue, IPofContext ctx, int of)
        {
            AbstractPofValue val = InstantiatePofValue(valueParent, nType,
                                                       binValue, ctx, of, binValue.GetReader());

            val.SetUniformEncoded();
            return(val);
        }
 /// <summary>
 /// Construct a PofValue instance wrapping the supplied binary.
 /// </summary>
 /// <param name="valueParent">
 /// Parent value within the POF stream.
 /// </param>
 /// <param name="binValue">
 /// Binary representation of this value.
 /// </param>
 /// <param name="ctx">
 /// POF context to use when reading or writing properties.
 /// </param>
 /// <param name="of">
 /// Offset of this value from the beginning of POF stream.
 /// </param>
 /// <param name="nType">
 /// POF type identifier for this value.
 /// </param>
 protected AbstractPofValue(IPofValue valueParent, Binary binValue,
                            IPofContext ctx, int of, int nType)
 {
     m_valueParent = valueParent;
     m_binValue    = binValue;
     m_ctx         = ctx;
     m_nType       = nType;
     m_of          = of;
 }
        /// <summary>
        /// Return the class associated with a specified type identifier, or null
        /// if the identifier is not defined in the current POF context.
        /// </summary>
        /// <param name="ctx">The POF context</param>
        /// <param name="nTypeId">The type identifier to lookup</param>
        /// <returns>
        /// </returns>
        private Type GetTypeForTypeId(IPofContext ctx, int nTypeId)

        {
            try
            {
                return(ctx.GetType(nTypeId));
            }
            catch (ArgumentException)
            {
                return(null);
            }
        }
Exemplo n.º 12
0
        public static void CheckConfiguration <TPortableObject>(IPofContext context, TPortableObject testObj)
            where TPortableObject : IPortableObject
        {
            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                serializer.Serialize(ms, testObj);
                ms.Position = 0;
                var deserialized = serializer.Deserialize <TPortableObject>(ms);
                NMockitoStatic.AssertEquals(testObj, deserialized);
            }
        }
        public void PortableExceptionConstructorTest()
        {
            IConnection conn = initiator.EnsureConnection();
            Channel     cacheServiceChannel = (Channel)conn.OpenChannel(CacheServiceProtocol.Instance,
                                                                        "CacheServiceProxy", null, null);

            EnsureCacheRequest ensureCacheRequest =
                (EnsureCacheRequest)cacheServiceChannel.MessageFactory.CreateMessage(EnsureCacheRequest.TYPE_ID);

            ensureCacheRequest.CacheName = "nonexisting";
            Assert.AreEqual(EnsureCacheRequest.TYPE_ID, ensureCacheRequest.TypeId);

            IStatus ensureCacheStatus = cacheServiceChannel.Send(ensureCacheRequest);

            Assert.IsInstanceOf(typeof(EnsureCacheRequest), ensureCacheRequest);
            Assert.AreEqual(EnsureCacheRequest.TYPE_ID, ensureCacheRequest.TypeId);
            try
            {
                ensureCacheStatus.WaitForResponse(-1);
            }
            catch (PortableException pe)
            {
                Assert.IsNotNull(pe);
                Assert.IsTrue(pe.Name.StartsWith("Portable("));
                Assert.IsNull(pe.InnerException);
                Assert.IsTrue(pe.Message.ToLower().IndexOf("no scheme") >= 0);

                string[] fullStackTrace = pe.FullStackTrace;
                string   stackTrace     = pe.StackTrace;
                Assert.IsTrue(fullStackTrace.Length > 0);
                foreach (string s in fullStackTrace)
                {
                    Assert.IsTrue(stackTrace.IndexOf(s) > 0);
                }
                Assert.IsTrue(stackTrace.IndexOf("process boundary") > 0);
                Assert.IsTrue(
                    pe.ToString().Equals(pe.Name + ": " + pe.Message));

                using (Stream stream = new MemoryStream())
                {
                    DataWriter  writer = new DataWriter(stream);
                    IPofContext ctx    = (IPofContext)cacheServiceChannel.Serializer;
                    ctx.Serialize(writer, pe);
                    stream.Position = 0;
                    DataReader        reader = new DataReader(stream);
                    PortableException result = (PortableException)ctx.Deserialize(reader);

                    Assert.AreEqual(pe.Name, result.Name);
                    Assert.AreEqual(pe.Message, result.Message);
                }
            }
        }
Exemplo n.º 14
0
        public void MergeContext(IPofContext pofContext)
        {
            var asPofContext = pofContext as PofContext;

            if (asPofContext != null)
            {
                MergeContext(asPofContext);
            }
            else
            {
                throw new InvalidOperationException("PofContext can only merge with other pof contexts");
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Validate that the supplied object is compatible with the
        /// specified type.
        /// </summary>
        /// <param name="o">
        /// The object.
        /// </param>
        /// <param name="typeId">
        /// The Pof type identifier; includes Pof intrinsics, Pof compact
        /// values, and user types.
        /// </param>
        /// <param name="ctx">
        /// The <see cref="IPofContext"/>.
        /// </param>
        /// <returns>
        /// The original object.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// If the specified type is a user type that is unknown to this
        /// <b>IPofContext</b> or there is no type mapping.
        /// </exception>
        /// <exception cref="InvalidCastException">
        /// If the specified object is not assignable to the specified type.
        /// </exception>
        public static object EnsureType(object o, int typeId, IPofContext ctx)
        {
            Type type = GetType(typeId, ctx);

            if (type == null)
            {
                throw new ArgumentException("Unknown or ambiguous type: " + typeId);
            }
            if (!type.IsAssignableFrom(o.GetType()))
            {
                throw new InvalidCastException(o.GetType().FullName +
                                               " is not assignable to " + type.FullName);
            }
            return(o);
        }
        /// <summary>
        /// Deserialize an enum instance from a POF stream by reading its
        /// value using the specified <see cref="IPofReader"/> object.
        /// </summary>
        /// <param name="reader">
        /// The <b>IPofReader</b> with which to read the object's state.
        /// </param>
        /// <returns>
        /// The deserialized enum instance.
        /// </returns>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        public object Deserialize(IPofReader reader)
        {
            IPofContext pofContext = reader.PofContext;
            Type        enumType   = pofContext.GetType(reader.UserTypeId);

            if (!enumType.IsEnum)
            {
                throw new ArgumentException(
                          "EnumPofSerializer can only be used to deserialize enum types.");
            }

            object enumValue = Enum.Parse(enumType, reader.ReadString(0));

            reader.RegisterIdentity(enumValue);
            reader.ReadRemainder();

            return(enumValue);
        }
        /// <summary>
        /// Creates a PofValue instance.
        /// </summary>
        /// <param name="valueParent">
        /// Parent POF value.
        /// </param>
        /// <param name="nType">
        /// Type identifier of this POF value.
        /// </param>
        /// <param name="binValue">
        /// POF-encoded binary value without the type identifier.
        /// </param>
        /// <param name="ctx">
        /// POF context to use.
        /// </param>
        /// <param name="of">
        /// Offset of the parsed value from the beginning of the POF stream.
        /// </param>
        /// <param name="reader">
        /// <see cref="DataReader"/> to read the value from.
        /// </param>
        /// <returns>
        /// A <see cref="IPofValue"/> instance.
        /// </returns>
        protected static AbstractPofValue InstantiatePofValue(
            IPofValue valueParent, int nType, Binary binValue,
            IPofContext ctx, int of, DataReader reader)
        {
            int         cSize;
            int         nElementType;
            int         nId;
            int         ofChildren;
            PofUserType value;
            IPofValue   valueRef;

            switch (nType)
            {
            case PofConstants.T_ARRAY:
                cSize      = reader.ReadPackedInt32();
                ofChildren = (int)reader.BaseStream.Position;
                return(new PofArray(valueParent, binValue, ctx, of, nType,
                                    ofChildren, cSize));

            case PofConstants.T_UNIFORM_ARRAY:
                nElementType = reader.ReadPackedInt32();
                cSize        = reader.ReadPackedInt32();
                ofChildren   = (int)reader.BaseStream.Position;
                return(new PofUniformArray(valueParent, binValue, ctx, of,
                                           nType, ofChildren, cSize, nElementType));

            case PofConstants.T_COLLECTION:
                cSize      = reader.ReadPackedInt32();
                ofChildren = (int)reader.BaseStream.Position;
                return(new PofCollection(valueParent, binValue, ctx, of,
                                         nType, ofChildren, cSize));

            case PofConstants.T_UNIFORM_COLLECTION:
                nElementType = reader.ReadPackedInt32();
                cSize        = reader.ReadPackedInt32();
                ofChildren   = (int)reader.BaseStream.Position;
                return(new PofUniformCollection(valueParent, binValue, ctx,
                                                of, nType, ofChildren, cSize, nElementType));

            case PofConstants.T_SPARSE_ARRAY:
                reader.ReadPackedInt32();                    // skip size
                ofChildren = (int)reader.BaseStream.Position;
                return(new PofSparseArray(valueParent, binValue, ctx, of,
                                          nType, ofChildren));

            case PofConstants.T_UNIFORM_SPARSE_ARRAY:
                nElementType = reader.ReadPackedInt32();
                reader.ReadPackedInt32();                    // skip size
                ofChildren = (int)reader.BaseStream.Position;
                return(new PofUniformSparseArray(valueParent, binValue, ctx,
                                                 of, nType, ofChildren, nElementType));

            case PofConstants.T_REFERENCE:
                nId        = reader.ReadPackedInt32();
                ofChildren = (int)reader.BaseStream.Position;
                value      = new PofUserType(valueParent, binValue, ctx, of,
                                             nType, ofChildren, 0);
                valueRef = value.LookupIdentity(nId);
                if (valueRef != null)
                {
                    value.SetValue(valueRef.GetValue());
                }
                return(value);

            default:
                nId = -1;
                if (nType == PofConstants.T_IDENTITY)
                {
                    nId   = reader.ReadPackedInt32();
                    nType = reader.ReadPackedInt32();
                    if (valueParent == null)
                    {
                        of = (int)reader.BaseStream.Position;
                    }
                }

                if (nType >= 0)
                {
                    int nVersionId = reader.ReadPackedInt32();
                    ofChildren = (int)reader.BaseStream.Position;

                    value = new PofUserType(valueParent, binValue, ctx, of,
                                            nType, ofChildren, nVersionId);
                    if (nId > -1)
                    {
                        value.RegisterIdentity(nId, value);
                    }
                    return(value);
                }
                return(new SimplePofValue(valueParent, binValue, ctx, of, nType));
            }
        }
 /// <summary>
 /// Construct a PofUniformCollection instance wrapping the supplied binary.
 /// </summary>
 /// <param name="valueParent">
 /// Parent value within the POF stream.
 /// </param>
 /// <param name="binValue">
 /// Binary representation of this value.
 /// </param>
 /// <param name="ctx">
 /// POF context to use when reading or writing properties.
 /// </param>
 /// <param name="of">
 /// Offset of this value from the beginning of POF stream.
 /// </param>
 /// <param name="nType">
 /// POF type identifier for this value.
 /// </param>
 /// <param name="ofChildren">
 /// Offset of the first child element within this value.
 /// </param>
 /// <param name="cElements">
 /// The size of this collection.
 /// </param>
 /// <param name="nElementType">
 /// A POF type identifier for this value's elements.
 /// </param>
 public PofUniformCollection(IPofValue valueParent, Binary binValue, IPofContext ctx,
                             int of, int nType, int ofChildren, int cElements, int nElementType)
     : base(valueParent, binValue, ctx, of, nType, ofChildren, cElements)
 {
     UniformElementType = nElementType;
 }
Exemplo n.º 19
0
 public PofSerializer(IPofContext context)
 {
     this.context = context;
 }
 /// <summary>
 /// Construct a SimplePofValue instance wrapping the supplied binary.
 /// </summary>
 /// <param name="valueParent">
 /// Parent value within the POF stream.
 /// </param>
 /// <param name="binValue">
 /// Binary representation of this value.
 /// </param>
 /// <param name="ctx">
 /// POF context to use when reading or writing properties.
 /// </param>
 /// <param name="of">
 /// Offset of this value from the beginning of POF stream.
 /// </param>
 /// <param name="nType">
 /// POF type identifier for this value.
 /// </param>
 public SimplePofValue(IPofValue valueParent, Binary binValue,
                       IPofContext ctx, int of, int nType)
     : base(valueParent, binValue, ctx, of, nType)
 {
 }
 /// <summary>
 /// Construct a PofSparseArray instance wrapping the supplied binary.
 /// </summary>
 /// <param name="valueParent">
 /// Parent value within the POF stream.
 /// </param>
 /// <param name="binValue">
 /// Binary representation of this value.
 /// </param>
 /// <param name="ctx">
 /// POF context to use when reading or writing properties.
 /// </param>
 /// <param name="of">
 /// Offset of this value from the beginning of POF stream.
 /// </param>
 /// <param name="nType">
 /// POF type identifier for this value.
 /// </param>
 /// <param name="ofChildren">
 /// Offset of the first child element within this value.
 /// </param>
 public PofSparseArray(IPofValue valueParent, Binary binValue, IPofContext ctx,
                       int of, int nType, int ofChildren)
     : base(valueParent, binValue, ctx, of, nType, ofChildren)
 {
 }
Exemplo n.º 22
0
 public PofWriter(IPofContext context, ISlotDestination destination)
 {
     this.context     = context;
     this.destination = destination;
 }
Exemplo n.º 23
0
 /// <summary>
 /// Construct a PofCollection instance wrapping the supplied binary.
 /// </summary>
 /// <param name="valueParent">
 /// Parent value within the POF stream.
 /// </param>
 /// <param name="binValue">
 /// Binary representation of this value.
 /// </param>
 /// <param name="ctx">
 /// POF context to use when reading or writing properties.
 /// </param>
 /// <param name="of">
 /// Offset of this value from the beginning of POF stream.
 /// </param>
 /// <param name="nType">
 /// POF type identifier for this value.
 /// </param>
 /// <param name="ofChildren">
 /// Offset of the first child element within this value.
 /// </param>
 /// <param name="cElements">
 /// The size of this collection.
 /// </param>
 public PofCollection(IPofValue valueParent, Binary binValue, IPofContext ctx,
                      int of, int nType, int ofChildren, int cElements)
     : base(valueParent, binValue, ctx, of, nType, ofChildren, cElements)
 {
 }
 /// <summary>
 /// Construct a PofArray instance wrapping the supplied binary.
 /// </summary>
 /// <param name="valueParent">
 /// Parent value within the POF stream.
 /// </param>
 /// <param name="binValue">
 /// Binary representation of this value.
 /// </param>
 /// <param name="ctx">
 /// POF context to use when reading or writing properties.
 /// </param>
 /// <param name="of">
 /// Offset of this value from the beginning of POF stream.
 /// </param>
 /// <param name="nType">
 /// POF type identifier for this value.
 /// </param>
 /// <param name="ofChildren">
 /// Offset of the first child element within this value.
 /// </param>
 /// <param name="cElements">
 /// The length of this array.
 /// </param>
 public PofArray(IPofValue valueParent, Binary binValue, IPofContext ctx,
                 int of, int nType, int ofChildren, int cElements)
     : base(valueParent, binValue, ctx, of, nType, ofChildren)
 {
     m_cElements = cElements;
 }
Exemplo n.º 25
0
 public void SetUp()
 {
     ctx    = new SimplePofContext();
     stream = new MemoryStream();
     la     = new LongSortedList();
 }
        /// <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="pofWriter">
        /// 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 pofWriter, Object o)
        {
            if (!(o is IPortableObject))
            {
                throw new IOException("Class [" + o.GetType() +
                                      "] does not implement a IPortableObject interface");
            }

            IPortableObject  po         = (IPortableObject)o;
            bool             fEvolvable = o is IEvolvableObject;
            IEvolvableObject et         = fEvolvable ? (IEvolvableObject)o : null;

            try
            {
                CacheFactory.Log("Serializing " + o.GetType(), CacheFactory.LogLevel.Max);

                IPofContext       ctx     = pofWriter.PofContext;
                IEnumerable <int> typeIds = GetTypeIds(o, ctx);

                foreach (int typeId in typeIds)
                {
                    IEvolvable e = null;
                    if (fEvolvable)
                    {
                        e = et.GetEvolvable(typeId);
                    }

                    IPofWriter writer = pofWriter.CreateNestedPofWriter(typeId, typeId);
                    if (fEvolvable)
                    {
                        writer.VersionId = Math.Max(e.DataVersion, e.ImplVersion);
                    }

                    Type type = GetTypeForTypeId(ctx, typeId);
                    if (type != null)
                    {
                        po.WriteExternal(writer);
                    }

                    writer.WriteRemainder(fEvolvable ? e.FutureData : null);
                }

                pofWriter.WriteRemainder(null);
            }
            catch (Exception e)
            {
                String sClass = null;
                try
                {
                    sClass = pofWriter.PofContext.GetTypeName(m_nTypeId);
                }
                catch (Exception)
                {
                }

                String sActual = null;
                try
                {
                    sActual = o.GetType().FullName;
                }
                catch (Exception)
                {
                }

                throw new IOException(
                          "An exception occurred writing a IPortableObject"
                          + " user type to a POF stream: type-id=" + m_nTypeId
                          + (sClass == null ? "" : ", class-name=" + sClass)
                          + (sActual == null ? "" : ", actual class-name=" + sActual)
                          + ", exception=" + e, e);
            }
        }
Exemplo n.º 27
0
        /// <summary>
        /// Determine the type associated with the given type identifier.
        /// </summary>
        /// <param name="typeId">
        /// The Pof type identifier; includes Pof intrinsics, Pof compact
        /// values, and user types.
        /// </param>
        /// <param name="ctx">
        /// The <see cref="IPofContext"/>.
        /// </param>
        /// <returns>
        /// The type associated with the specified type identifier or
        /// <c>null</c> for types with no mapping.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// If the specified type is a user type that is unknown to this
        /// <b>IPofContext</b>.
        /// </exception>
        public static Type GetType(int typeId, IPofContext ctx)
        {
            if (typeId >= 0)
            {
                return(ctx.GetType(typeId));
            }

            switch (typeId)
            {
            case PofConstants.T_INT16:
                return(typeof(short));

            case PofConstants.T_INT32:
            case PofConstants.V_INT_NEG_1:
            case PofConstants.V_INT_0:
            case PofConstants.V_INT_1:
            case PofConstants.V_INT_2:
            case PofConstants.V_INT_3:
            case PofConstants.V_INT_4:
            case PofConstants.V_INT_5:
            case PofConstants.V_INT_6:
            case PofConstants.V_INT_7:
            case PofConstants.V_INT_8:
            case PofConstants.V_INT_9:
            case PofConstants.V_INT_10:
            case PofConstants.V_INT_11:
            case PofConstants.V_INT_12:
            case PofConstants.V_INT_13:
            case PofConstants.V_INT_14:
            case PofConstants.V_INT_15:
            case PofConstants.V_INT_16:
            case PofConstants.V_INT_17:
            case PofConstants.V_INT_18:
            case PofConstants.V_INT_19:
            case PofConstants.V_INT_20:
            case PofConstants.V_INT_21:
            case PofConstants.V_INT_22:
                return(typeof(int));

            case PofConstants.T_INT64:
                return(typeof(long));

            case PofConstants.T_INT128:
                throw new NotSupportedException("T_INT128 type is not supported.");

            case PofConstants.T_FLOAT32:
                return(typeof(float));

            case PofConstants.T_FLOAT64:
                return(typeof(double));

            case PofConstants.T_FLOAT128:
                throw new NotSupportedException("T_FLOAT128 type is not supported.");

            case PofConstants.V_FP_POS_INFINITY:
                return(typeof(double));

            case PofConstants.V_FP_NEG_INFINITY:
                return(typeof(double));

            case PofConstants.V_FP_NAN:
                return(typeof(double));

            case PofConstants.T_DECIMAL32:
                return(typeof(decimal));

            case PofConstants.T_DECIMAL64:
                throw new NotSupportedException("T_DECIMAL64 type is not supported.");

            case PofConstants.T_DECIMAL128:
                throw new NotSupportedException("T_DECIMAL128 type is not supported.");

            case PofConstants.T_BOOLEAN:
            case PofConstants.V_BOOLEAN_FALSE:
            case PofConstants.V_BOOLEAN_TRUE:
                return(typeof(bool));

            case PofConstants.T_OCTET:
                return(typeof(byte));

            case PofConstants.T_OCTET_STRING:
                return(typeof(Binary));

            case PofConstants.T_CHAR:
                return(typeof(char));

            case PofConstants.T_CHAR_STRING:
            case PofConstants.V_STRING_ZERO_LENGTH:
                return(typeof(string));

            case PofConstants.T_DATE:
                return(typeof(DateTime));

            case PofConstants.T_TIME:
                return(typeof(RawTime));

            case PofConstants.T_DATETIME:
                return(typeof(DateTime));

            case PofConstants.T_YEAR_MONTH_INTERVAL:
                return(typeof(RawYearMonthInterval));

            case PofConstants.T_TIME_INTERVAL:
            case PofConstants.T_DAY_TIME_INTERVAL:
                return(typeof(TimeSpan));

            case PofConstants.T_COLLECTION:
            case PofConstants.T_UNIFORM_COLLECTION:
            case PofConstants.V_COLLECTION_EMPTY:
                return(typeof(ICollection));

            case PofConstants.T_MAP:
            case PofConstants.T_UNIFORM_KEYS_MAP:
            case PofConstants.T_UNIFORM_MAP:
                return(typeof(IDictionary));

            case PofConstants.T_SPARSE_ARRAY:
                return(typeof(ILongArray));

            case PofConstants.T_ARRAY:
                return(typeof(object[]));

            case PofConstants.T_UNIFORM_ARRAY:
            case PofConstants.T_UNIFORM_SPARSE_ARRAY:
            case PofConstants.V_REFERENCE_NULL:
                // ambiguous - could be either an array or SparseArray
                return(null);

            case PofConstants.T_IDENTITY:
            case PofConstants.T_REFERENCE:
                throw new ArgumentException(typeId + " has no " +
                                            "mapping to a class");

            default:
                throw new ArgumentException(typeId + " is an " +
                                            "invalid type");
            }
        }
Exemplo n.º 28
0
 /// <summary>
 /// Construct a PofUserType instance wrapping the supplied binary.
 /// </summary>
 /// <param name="valueParent">
 /// Parent value within the POF stream.
 /// </param>
 /// <param name="binValue">
 /// Binary representation of this value.
 /// </param>
 /// <param name="ctx">
 /// POF context to use when reading or writing properties.
 /// </param>
 /// <param name="of">
 /// Offset of this value from the beginning of POF stream.
 /// </param>
 /// <param name="nType">
 /// POF type identifier for this value.
 /// </param>
 /// <param name="ofChildren">
 /// Offset of the first child element within this value.
 /// </param>
 /// <param name="nVersion">
 /// Data version of this value.
 /// </param>
 public PofUserType(IPofValue valueParent, Binary binValue,
                    IPofContext ctx, int of, int nType, int ofChildren, int nVersion)
     : base(valueParent, binValue, ctx, of, nType, ofChildren)
 {
     m_nVersion = nVersion;
 }
Exemplo n.º 29
0
 public PofReader(IPofContext context, ISlotSource slots)
 {
     this.context = context;
     this.slots   = slots;
 }
 /// <summary>
 /// Parses POF-encoded binary and returns an instance of a
 /// <see cref="IPofValue"/> wrapper for it.
 /// </summary>
 /// <param name="binValue">
 /// POF-encoded binary value.
 /// </param>
 /// <param name="ctx">
 /// POF context to use.
 /// </param>
 /// <returns>
 /// An IPofValue instance.
 /// </returns>
 public static IPofValue Parse(Binary binValue, IPofContext ctx)
 {
     return(ParseValue(null, binValue, ctx, 0));
 }