예제 #1
1
        private void Check(TypeModel model, SerializationContext ctx, int magicNumber, string caption)
        {
            try
            {
                CanHazFactory orig = new CanHazFactory {Foo = 123, Bar = 456}, clone;
                using(var ms = new MemoryStream())
                {
                    model.Serialize(ms, orig, ctx);
                    ms.Position = 0;
                    clone = (CanHazFactory) model.Deserialize(ms, null, typeof(CanHazFactory), ctx);
                }

                Assert.AreNotSame(orig, clone);

                Assert.AreEqual(123, orig.Foo, caption);
                Assert.AreEqual(456, orig.Bar, caption);
                Assert.AreEqual(0, orig.MagicNumber, caption);

                Assert.AreEqual(123, clone.Foo, caption);
                Assert.AreEqual(456, clone.Bar, caption);
                Assert.AreEqual(magicNumber, clone.MagicNumber, caption);

            } catch
            {
                Debug.WriteLine(caption);
                throw;
            }

        }
예제 #2
0
 public void TestExternal()
 {
     var model = RuntimeTypeModel.Create();
     model.AutoCompile = false;
     model.Add(typeof(CanHazFactory), true).SetFactory(typeof(TypeFactory).GetMethod("ExternalFactory"));
     var ctx = new SerializationContext {Context = 12345};
     Check(model, ctx, 12345, "Runtime");
     model.CompileInPlace();
     Check(model, ctx, 12345, "CompileInPlace");
     Check(model.Compile(), ctx, 12345, "Compile");
 }
예제 #3
0
 static public int get_Context(IntPtr l)
 {
     try {
         ProtoBuf.SerializationContext self = (ProtoBuf.SerializationContext)checkSelf(l);
         pushValue(l, true);
         pushValue(l, self.Context);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
예제 #4
0
 /// <summary>
 /// Creates a new writer against a stream
 /// </summary>
 /// <param name="dest">The destination stream</param>
 /// <param name="model">The model to use for serialization; this can be null, but this will impair the ability to serialize sub-objects</param>
 /// <param name="context">Additional context about this serialization operation</param>
 public ProtoWriter(Stream dest, TypeModel model, SerializationContext context)
 {
     if (dest == null) throw new ArgumentNullException("dest");
     if (!dest.CanWrite) throw new ArgumentException("Cannot write to stream", "dest");
     //if (model == null) throw new ArgumentNullException("model");
     this.dest = dest;
     this.ioBuffer = BufferPool.GetBuffer();
     this.model = model;
     this.wireType = WireType.None;
     if (context == null) { context = SerializationContext.Default; }
     else { context.Freeze(); }
     this.context = context;
 }
 public SerializationContext(Stream stream, SerializationContext parent)
 {
     this.stream = stream;
     if (parent == null)
     {
         workspace = new byte[InitialBufferLength];
         ioBuffer = new byte[IO_BUFFER_SIZE];
     }
     else
     {
         ReadFrom(parent);
     }
 }
예제 #6
0
 static public int constructor(IntPtr l)
 {
     try {
         ProtoBuf.SerializationContext o;
         o = new ProtoBuf.SerializationContext();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
예제 #7
0
 static public int set_Context(IntPtr l)
 {
     try {
         ProtoBuf.SerializationContext self = (ProtoBuf.SerializationContext)checkSelf(l);
         System.Object v;
         checkType(l, 2, out v);
         self.Context = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 public int Serialize(IExtension extension)
 {
     if (extension == null) return 0;
     Stream stream = extension.BeginAppend();
     try
     {
         SerializationContext ctx = new SerializationContext(stream, null);
         int len = Serialize(ctx);
         ctx.Flush();
         extension.EndAppend(stream, true);
         return len;
     }
     catch
     {
         extension.EndAppend(stream, false);
         throw;
     }
 }
 public override int Serialize(SerializationContext context)
 {
     return WritePrefix(context) + context.EncodeInt32(wireValue);
 }
 protected int WritePrefix(SerializationContext context)
 {
     return prefix == 0 ? 0 : context.EncodeUInt32(prefix);
 }
 public abstract int Serialize(SerializationContext context);
예제 #12
0
 private void TraceChangeOrigin(SerializationContext context)
 {
     long newPos = stream.Position, oldPos = context.stream.Position;
     if(oldPos != newPos)
     {
         Debug.WriteLine(new string('!', stackDepth) +
             string.Format(" re-based: {0} now {1}", oldPos, newPos),
             SerializationContext.DebugCategory);
     }
 }
예제 #13
0
 private static void Init(ProtoReader reader, Stream source, TypeModel model, SerializationContext context, int length)
 {
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     if (!source.CanRead)
     {
         throw new ArgumentException("Cannot read from stream", "source");
     }
     reader.source = source;
     reader.ioBuffer = BufferPool.GetBuffer();
     reader.model = model;
     bool flag = length >= 0;
     reader.isFixedLength = flag;
     reader.dataRemaining = ((!flag) ? 0 : length);
     if (context == null)
     {
         context = SerializationContext.Default;
     }
     else
     {
         context.Freeze();
     }
     reader.context = context;
     reader.position = (reader.available = (reader.depth = (reader.fieldNumber = (reader.ioIndex = 0))));
     reader.blockEnd = 2147483647;
     reader.internStrings = true;
     reader.wireType = WireType.None;
     reader.trapCount = 1u;
     if (reader.netCache == null)
     {
         reader.netCache = new NetObjectCache();
     }
 }
예제 #14
0
        public void ReadFrom(SerializationContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            this.workspace = context.workspace;
            this.objectStack = context.objectStack;
            this.stackDepth = context.stackDepth;
            this.streamState = context.streamState;
            this.peekedValue = context.peekedValue;
            this.ioBuffer = context.ioBuffer;
            this.ioBufferIndex = context.ioBufferIndex;
            this.inputStreamAvailable = context.inputStreamAvailable;
            this.ioBufferEffectiveSize = context.ioBufferEffectiveSize;
            this.stringCache = context.stringCache;
            TraceChangeOrigin(context); // note ConditionalAttribute

            // IMPORTANT: don't copy the group stack; we want to
            // validate that the group-stack is empty when finding the end of a stream
        }
예제 #15
0
        /// <summary>
        /// All this does is call GetExtendedValuesTyped with the correct type for "instance";
        /// this ensures that we don't get issues with subclasses declaring conflicting types -
        /// the caller must respect the fields defined for the type they pass in.
        /// </summary>
        internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag)
        {
#if FEAT_IKVM
            throw new NotSupportedException();
#else
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }
            if (tag <= 0)
            {
                throw new ArgumentOutOfRangeException("tag");
            }
            IExtension extn = instance.GetExtensionObject(false);

            if (extn == null)
            {
#if FX11
                return(new object[0]);
#else
                yield break;
#endif
            }

#if FX11
            BasicList result = new BasicList();
#endif
            Stream stream = extn.BeginQuery();
            object value  = null;
            try {
                SerializationContext ctx = new SerializationContext();
                using (ProtoReader reader = new ProtoReader(stream, model, ctx))
                {
                    while (model.TryDeserializeAuxiliaryType(reader, format, tag, type, ref value, true, false, false, false) && value != null)
                    {
                        if (!singleton)
                        {
#if FX11
                            result.Add(value);
#else
                            yield return(value);
#endif
                            value = null; // fresh item each time
                        }
                    }
                }
                if (singleton && value != null)
                {
#if FX11
                    result.Add(value);
#else
                    yield return(value);
#endif
                }
#if FX11
                object[] resultArr = new object[result.Count];
                result.CopyTo(resultArr, 0);
                return(resultArr);
#endif
            } finally {
                extn.EndQuery(stream);
            }
#endif
        }
예제 #16
0
 public ProtoReader(Stream source, TypeModel model, SerializationContext context, int length)
 {
     ProtoReader.Init(this, source, model, context, length);
 }
예제 #17
0
 internal static ProtoReader Create(Stream source, TypeModel model, SerializationContext context, int len)
 {
     ProtoReader recycled = ProtoReader.GetRecycled();
     if (recycled == null)
     {
         return new ProtoReader(source, model, context, len);
     }
     ProtoReader.Init(recycled, source, model, context, len);
     return recycled;
 }
예제 #18
0
 public static CanHazFactory ExternalFactory(SerializationContext ctx)
 {
     return new CanHazFactory { MagicNumber = (int)ctx.Context };
 }
예제 #19
0
 static SerializationContext()
 {
     @default = new SerializationContext();
     @default.Freeze();
 }
예제 #20
0
 static SerializationContext()
 {
     @default = new SerializationContext();
     @default.Freeze();
 }