Esempio n. 1
0
        public void PlainObjectsWithNoInheritance()
        {
            const PrefixStyle prefix     = PrefixStyle.Base128;
            const string      memberName = "Id";

            using (var ms = new MemoryStream())
            {
                var items = new List <PlainObject>();
                for (var i = 0; i < 10; i++)
                {
                    items.Add(PlainObject.WithId(i));
                }

                foreach (var item in items)
                {
                    _model.SerializeWithLengthPrefix(ms, item, prefix);
                }

                var builder = new ProjectionTypeBuilder(_model);

                var type = builder.GetTypeForProjection(typeof(PlainObject), typeof(PlainObject).GetMember(memberName));

                ms.Seek(0, SeekOrigin.Begin);
                var deserializedItems = _model.DeserializeItems(ms, type, prefix, 0, null).Cast <object>().ToArray();

                Assert.AreEqual(items.Count, deserializedItems.Length);

                var fi = (FieldInfo)type.GetMember(memberName)[0];
                for (var i = 0; i < items.Count; i++)
                {
                    Assert.AreEqual(items[i].Id, (int)fi.GetValue(deserializedItems[i]));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes this target.
        /// </summary>
        public override void Initialize()
        {
            _currentEntities.Clear();

            // write the mandatory header.
            _buffer.Seek(0, SeekOrigin.Begin);

            // create header block.
            var blockHeader = new HeaderBlock();

            blockHeader.required_features.Add("OsmSchema-V0.6");
            blockHeader.required_features.Add("DenseNodes");
            _runtimeTypeModel.Serialize(_buffer, blockHeader);
            var blockHeaderData = _buffer.ToArray();

            _buffer.SetLength(0);

            // create blob.
            var blob = new Blob();

            blob.raw = blockHeaderData;
            _runtimeTypeModel.Serialize(_buffer, blob);

            // create blobheader.
            var blobHeader = new BlobHeader();

            blobHeader.datasize  = (int)_buffer.Length;
            blobHeader.indexdata = null;
            blobHeader.type      = Encoder.OSMHeader;
            _runtimeTypeModel.SerializeWithLengthPrefix(_stream, blobHeader, _blobHeaderType, ProtoBuf.PrefixStyle.Fixed32BigEndian, 0);

            // flush to stream.
            _buffer.Seek(0, SeekOrigin.Begin);
            _buffer.CopyTo(_stream);
        }
Esempio n. 3
0
 public void SetUp()
 {
     _stream = new MemoryStream();
     for (var i = 0; i < RepetetionCount; i++)
     {
         _model.SerializeWithLengthPrefix(_stream, Employee.Create(2 * i, "Test" + i, "City" + i, "Street+i"),
                                          Prefix);
         _model.SerializeWithLengthPrefix(_stream,
                                          Employee.CreateManager(2 * i + 1, "Test" + i, "City" + i, "Street+i"), Prefix);
     }
     _stream.Seek(0, SeekOrigin.Begin);
 }
Esempio n. 4
0
        /// <summary>
        /// Initializes this target.
        /// </summary>
        public override void Initialize()
        {
            _currentEntities.Clear();

            // write the mandatory header.
            _buffer.Seek(0, SeekOrigin.Begin);

            // create header block.
            var blockHeader = new HeaderBlock();

            blockHeader.required_features.Add("OsmSchema-V0.6");
            blockHeader.required_features.Add("DenseNodes");
            _runtimeTypeModel.Serialize(_buffer, blockHeader);
            var blockHeaderData = _buffer.ToArray();

            _buffer.SetLength(0);

            // create blob.
            var blob = new Blob();

            if (_compress)
            {
                using (var target = new MemoryStream())
                {
                    using (var source = new MemoryStream(blockHeaderData))
                        using (var deflate = new DeflaterOutputStream(target))
                        {
                            source.CopyTo(deflate);
                        }
                    blob.zlib_data = target.ToArray();
                }
            }
            else
            {
                blob.raw = blockHeaderData;
            }

            _runtimeTypeModel.Serialize(_buffer, blob);

            // create blobheader.
            var blobHeader = new BlobHeader();

            blobHeader.datasize  = (int)_buffer.Length;
            blobHeader.indexdata = null;
            blobHeader.type      = Encoder.OSMHeader;
            _runtimeTypeModel.SerializeWithLengthPrefix(_stream, blobHeader, _blobHeaderType, ProtoBuf.PrefixStyle.Fixed32BigEndian, 0);

            // flush to stream.
            _buffer.Seek(0, SeekOrigin.Begin);
            _buffer.CopyTo(_stream);
        }
Esempio n. 5
0
        /// <summary>
        /// Writes a protocol-buffer representation of the given instance to the supplied stream,
        /// with a length-prefix. This is useful for socket programming,
        /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back
        /// from an ongoing stream.
        /// </summary>
        /// <typeparam name="T">The type being serialized.</typeparam>
        /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
        /// <param name="style">How to encode the length prefix.</param>
        /// <param name="destination">The destination stream to write to.</param>
        /// <param name="fieldNumber">The tag used as a prefix to each record (only used with base-128 style prefixes).</param>
        public static void SerializeWithLengthPrefix <T>(Stream destination, T instance, PrefixStyle style,
                                                         int fieldNumber)
        {
            RuntimeTypeModel model = RuntimeTypeModel.Default;

            model.SerializeWithLengthPrefix(destination, instance, model.MapType(typeof(T)), style, fieldNumber);
        }
Esempio n. 6
0
        public PerformanceTests()
        {
            _model = TypeModel.Create();
            _model.Add(typeof(Event), true);
            _stream = new MemoryStream();

            for (var i = 0; i < 1000 * 1000; i++)
            {
                var id   = Guid.NewGuid();
                var item = new OrderPlacedEvent(id,
                                                "Ordered by customer number" + i.ToString(CultureInfo.InvariantCulture),
                                                new[] { new OrderItem("Test product", i) });
                var shipment = new OrderShippedEvent(id, DateTime.Now);

                _model.SerializeWithLengthPrefix(_stream, item, typeof(Event), Prefix, 0);
                _model.SerializeWithLengthPrefix(_stream, shipment, typeof(Event), Prefix, 0);
            }
        }
Esempio n. 7
0
 public void SerializeWithLengthPrefix <T>(IEnumerable <T> content, string path) where T : new()
 {
     using (var fileStream = File.Open(path, FileMode.Create))
     {
         foreach (var item in content)
         {
             _model.SerializeWithLengthPrefix(fileStream, item, typeof(T), PrefixStyle.Base128, 1);
         }
     }
 }
Esempio n. 8
0
 public void WriteSpatialRecords(string path, IEnumerable <SpatialRecord> spatialRecords)
 {
     using (var fileStream = File.Open(path, FileMode.Create))
     {
         foreach (var spatialRecord in spatialRecords)
         {
             _model.SerializeWithLengthPrefix(fileStream, spatialRecord, typeof(SpatialRecord), PrefixStyle.Base128, 1);
         }
     }
 }
Esempio n. 9
0
            /// <summary>
            /// Writes a protocol-buffer representation of the given instance to the supplied stream,
            /// with a length-prefix. This is useful for socket programming,
            /// as DeserializeWithLengthPrefix/MergeWithLengthPrefix can be used to read the single object back
            /// from an ongoing stream.
            /// </summary>
            /// <param name="instance">The existing instance to be serialized (cannot be null).</param>
            /// <param name="style">How to encode the length prefix.</param>
            /// <param name="destination">The destination stream to write to.</param>
            /// <param name="fieldNumber">The tag used as a prefix to each record (only used with base-128 style prefixes).</param>
            public static void SerializeWithLengthPrefix(Stream destination, object instance, PrefixStyle style, int fieldNumber)
            {
                if (instance == null)
                {
                    throw new ArgumentNullException("instance");
                }
                RuntimeTypeModel model = RuntimeTypeModel.Default;

                model.SerializeWithLengthPrefix(destination, instance, model.MapType(instance.GetType()), style, fieldNumber);
            }
Esempio n. 10
0
 /// <summary>
 /// Serializes the object to string.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="data"></param>
 /// <returns></returns>
 public static byte[] Serialize <T>(T data)
 {
     try
     {
         using (MemoryStream outputStream = new MemoryStream())
         {
             TypeModel.SerializeWithLengthPrefix(outputStream, data, typeof(T), ProtoBuf.PrefixStyle.Base128, 0);
             return(outputStream.GetBuffer());
         }
     }
     catch (Exception e)
     {
         MasterLog.DebugWriteLine(e.Message);
         return(null);
     }
 }
Esempio n. 11
0
 static void TestClone(string fileName, int size, RuntimeTypeModel m)
 {
     using (var stream = new FileStream(fileName, FileMode.Create))
     {
         stream.SetLength(0);
         GC.GetTotalMemory(true);
         m.SerializeWithLengthPrefix(stream, new Wrapper()
         {
             Count = size
         }, typeof(Wrapper), PrefixStyle.Base128, 0);
         Assert.That(stream.Length, Is.AtLeast(size));
         Assert.That(stream.Length, Is.LessThan(size + 10000));
         stream.Flush(true);
         GC.GetTotalMemory(true);
         stream.Position = 0;
         var w = (Wrapper)m.DeserializeWithLengthPrefix(stream, null, typeof(Wrapper), PrefixStyle.Base128, 0);
         Assert.That(w.Count, Is.EqualTo(size));
         Assert.That(stream.Position, Is.EqualTo(stream.Length));
     }
 }
Esempio n. 12
0
        /// <summary>
        /// Serializes an object, or graph of objects with the given root to the provided stream.
        /// </summary>
        /// <param name="stream">The stream where the formatter puts the serialized data. This stream can
        /// reference a variety of backing stores (such as files, network, memory, and so on).</param>
        /// <param name="graph">The object, or root of the object graph, to serialize.
        /// All child objects of this root object are automatically serialized.</param>
        public void Serialize(Stream stream, object graph)
        {
            Ensure.NotNull(stream, "stream");
            Ensure.NotNull(graph, "graph");

            if (IncludeTypeName)
            {
                new BinaryWriter(stream, Encoding.UTF8)
                .Write(graph.GetType().AssemblyQualifiedName);
            }

            if (UseLengthPrefix)
            {
                _typeModel.SerializeWithLengthPrefix(stream, graph, graph.GetType(), PrefixStyle.Base128, 0);
            }
            else
            {
                _typeModel.Serialize(stream, graph);
            }
        }
Esempio n. 13
0
 public void Serialize(Stream stream, object value, Type type)
 {
     _runtime.SerializeWithLengthPrefix(stream, value, type, PrefixStyle.Fixed32, 0);
 }
Esempio n. 14
0
 public void Serialize(Stream stream, object o)
 {
     model.SerializeWithLengthPrefix(stream, o, o.GetType(), PrefixStyle.Base128, 0);
 }
Esempio n. 15
0
 public void Serialize(object value, Stream stream)
 {
     _runtime.SerializeWithLengthPrefix(stream, value, value.GetType(), ProtoBuf.PrefixStyle.Fixed32, 0);
 }
Esempio n. 16
0
 internal void Serialise(Stream stream, Type type, object obj)
 {
     Wrapped(() => _model.SerializeWithLengthPrefix(stream, obj, type, PrefixStyle.Base128, 0));
 }
Esempio n. 17
0
 public static void SerializeWithLengthPrefix <T>(this RuntimeTypeModel model, Stream destination, T instance, PrefixStyle style, int fieldNumber)
 {
     model.SerializeWithLengthPrefix(destination, instance, typeof(T), style, fieldNumber);
 }