Exemplo n.º 1
0
        public ObjectSerializer(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            Type = type;
            var typeName = type.GetShortAssemblyQualifiedName();
            // ReSharper disable once PossibleNullReferenceException
            // ReSharper disable once AssignNullToNotNullAttribute
            var typeNameBytes = Encoding.UTF8.GetBytes(typeName);

            //precalculate the entire manifest for this serializer
            //this helps us to minimize calls to Stream.Write/WriteByte
            _manifest =
                new[] { ManifestFull }
            .Concat(BitConverter.GetBytes(typeNameBytes.Length))
            .Concat(typeNameBytes)
            .ToArray();         //serializer id 255 + assembly qualified name

            //initialize reader and writer with dummy handlers that wait until the serializer is fully initialized
            _writer = (stream, o, session) =>
            {
                SpinWait.SpinUntil(() => _isInitialized);
                WriteValue(stream, o, session);
            };

            _reader = (stream, session) =>
            {
                SpinWait.SpinUntil(() => _isInitialized);
                return(ReadValue(stream, session));
            };
        }
Exemplo n.º 2
0
        public override void Write(Writer writer, T[,] obj)
        {
            base.Write(writer, obj);
            var stream  = writer.Stream;
            int length  = obj.GetLength(0);
            int length2 = obj.GetLength(1);

            stream.WriteByte(TagList);
            if (length > 0)
            {
                ValueWriter.WriteInt(stream, length);
            }
            stream.WriteByte(TagOpenbrace);
            var serializer = Serializer <T> .Instance;

            for (int i = 0; i < length; ++i)
            {
                writer.AddReferenceCount(1);
                stream.WriteByte(TagList);
                if (length2 > 0)
                {
                    ValueWriter.WriteInt(stream, length2);
                }
                stream.WriteByte(TagOpenbrace);
                for (int j = 0; j < length2; ++j)
                {
                    serializer.Serialize(writer, obj[i, j]);
                }
                stream.WriteByte(TagClosebrace);
            }
            stream.WriteByte(TagClosebrace);
        }
Exemplo n.º 3
0
        public void WriteMessage(QueueBind message, IBufferWriter <byte> output)
        {
            ValueWriter writer = new ValueWriter(output);

            writer.WriteOctet(1);
            writer.WriteShortInt(_channelId);
            var reserved   = writer.Reserve(4);
            var checkpoint = writer.Written;

            FrameWriter.WriteMethodFrame(50, 20, ref writer);
            writer.WriteShortInt(0); //reserved-1
            writer.WriteShortStr(message.QueueName);
            writer.WriteShortStr(message.ExchangeName);
            writer.WriteShortStr(message.RoutingKey);
            writer.WriteBit(message.NoWait);
            writer.WriteTable(message.Arguments);
            var payloadSize = writer.Written - checkpoint;

            writer.WriteOctet(RabbitMQConstants.FrameEnd);

            Span <byte> span = stackalloc byte[4];

            BinaryPrimitives.WriteInt32BigEndian(span, payloadSize);
            reserved.Write(span);

            writer.Commit();
        }
Exemplo n.º 4
0
        public void WrittenValuesCanBeReadBackCorrectly()
        {
            var values = new[] { 4, 8, 1.5, 1.6, 1.7, 1.7, 2.3, 22.0 / 7.0, 653.2, 653.02 };

            var buffer = new BitBuffer();
            var writer = new ValueWriter(buffer);

            foreach (var value in values)
            {
                writer.AppendValue(value);
            }


            var reader = new ValueReader(buffer);

            var actual = new List <double>();

            while (reader.HasMoreValues)
            {
                actual.Add(reader.ReadNextValue());
            }

            actual.Count.Should().Be(values.Length);
            for (int i = 0; i < actual.Count; ++i)
            {
                actual[i].Should().Be(values[i]);
            }
        }
Exemplo n.º 5
0
        public override void Write(Writer writer, T obj)
        {
            if (!obj.CanRead || !obj.CanSeek)
            {
                throw new IOException("This stream can't support serialize.");
            }
            base.Write(writer, obj);
            var  stream = writer.Stream;
            long oldPos = obj.Position;

            obj.Position = 0;
            stream.WriteByte(TagBytes);
            int length = (int)obj.Length;

            if (length > 0)
            {
                ValueWriter.WriteInt(stream, length);
            }
            stream.WriteByte(TagQuote);
#if !NET35
            obj.CopyTo(stream);
#else
            byte[] array = new byte[length > 81920 ? 81920 : length];
            int    count;
            while ((count = obj.Read(array, 0, array.Length)) != 0)
            {
                stream.Write(array, 0, count);
            }
#endif
            stream.WriteByte(TagQuote);
            obj.Position = oldPos;
        }
        internal void WriteMessage(ref ContentHeader message, ref ValueWriter writer)
        {
            _bitCount = 0;
            _flagWord = 0;
            writer.WriteOctet(RabbitMQConstants.FrameHeader);
            writer.WriteShortInt(_channelId);
            var reserved   = writer.Reserve(4);
            var checkpoint = writer.Written;

            writer.WriteShortInt(message.ClassId);
            writer.WriteShortInt(message.Weight);
            writer.WriteLongLong(message.BodySize);

            WriteBitFlagsAndContinuation(ref message.Properties, ref writer);


            var payloadSize = writer.Written - checkpoint;

            writer.WriteOctet(RabbitMQConstants.FrameEnd);

            Span <byte> span = stackalloc byte[4];

            BinaryPrimitives.WriteInt32BigEndian(span, payloadSize);
            reserved.Write(span);

            writer.Commit();
        }
Exemplo n.º 7
0
        public override void Write(Writer writer, T obj)
        {
            if (!obj.CanRead)
            {
                throw new IOException("This stream can't support serialize.");
            }
            base.Write(writer, obj);
            var  stream = writer.Stream;
            long oldPos = 0;

            if (obj.CanSeek)
            {
                oldPos       = obj.Position;
                obj.Position = 0;
            }
            stream.WriteByte(TagBytes);
            int length = (int)obj.Length;

            if (length > 0)
            {
                ValueWriter.WriteInt(stream, length);
            }
            stream.WriteByte(TagQuote);
            obj.CopyTo(stream);
            stream.WriteByte(TagQuote);
            if (obj.CanSeek)
            {
                obj.Position = oldPos;
            }
        }
Exemplo n.º 8
0
 internal void WriteMessage(ReadOnlyMemory <byte> message, ref ValueWriter writer)
 {
     FrameWriter.WriteFrameHeader(RabbitMQConstants.FrameBody, _channelId, message.Length, ref writer);
     writer.WriteBytes(message.Span);
     writer.WriteOctet(RabbitMQConstants.FrameEnd);
     writer.Commit();
 }
Exemplo n.º 9
0
        public void WriteMessage(ContentHeader message, IBufferWriter <byte> output)
        {
            m_bitCount = 0;
            m_flagWord = 0;
            ValueWriter writer = new ValueWriter(output);

            writer.WriteOctet(Constants.FrameHeader);
            writer.WriteShortInt(_channelId);
            var reserved   = writer.Reserve(4);
            var checkpoint = writer.Written;

            writer.WriteShortInt(message.ClassId);
            writer.WriteShortInt(message.Weight);
            writer.WriteLongLong(message.BodySize);

            WriteBitFlagsAndContinuation(ref message, ref writer);


            var payloadSize = writer.Written - checkpoint;

            writer.WriteOctet(Constants.FrameEnd);

            Span <byte> span = stackalloc byte[4];

            BinaryPrimitives.WriteInt32BigEndian(span, payloadSize);
            reserved.Write(span);

            writer.Commit();
        }
Exemplo n.º 10
0
        public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
                                                        ConcurrentDictionary <Type, ValueSerializer> typeMapping)
        {
            var x = new ObjectSerializer(type);

            typeMapping.TryAdd(type, x);

            var elementType     = GetEnumerableType(type);
            var arrType         = elementType.MakeArrayType();
            var listModule      = type.Assembly.GetType("Microsoft.FSharp.Collections.ListModule");
            var ofArray         = listModule.GetMethod("OfArray");
            var ofArrayConcrete = ofArray.MakeGenericMethod(elementType);
            var ofArrayCompiled = CodeGenerator.CompileToDelegate(ofArrayConcrete, arrType);
            var toArray         = listModule.GetMethod("ToArray");
            var toArrayConcrete = toArray.MakeGenericMethod(elementType);
            var toArrayCompiled = CodeGenerator.CompileToDelegate(toArrayConcrete, type);

            ValueWriter writer = (stream, o, session) =>
            {
                var arr           = toArrayCompiled(o);
                var arrSerializer = serializer.GetSerializerByType(arrType);
                arrSerializer.WriteValue(stream, arr, session);
            };

            ValueReader reader = (stream, session) =>
            {
                var arrSerializer = serializer.GetSerializerByType(arrType);
                var items         = (Array)arrSerializer.ReadValue(stream, session);
                var res           = ofArrayCompiled(items);
                return(res);
            };

            x.Initialize(reader, writer);
            return(x);
        }
Exemplo n.º 11
0
        public ObjectSerializer(Type type)
        {
            if (type == null)
                throw new ArgumentNullException(nameof(type));

            Type = type;
            var typeName = type.GetShortAssemblyQualifiedName();
            // ReSharper disable once PossibleNullReferenceException
            // ReSharper disable once AssignNullToNotNullAttribute
            var typeNameBytes = Encoding.UTF8.GetBytes(typeName);

            //precalculate the entire manifest for this serializer
            //this helps us to minimize calls to Stream.Write/WriteByte 
            _manifest =
                new[] {ManifestFull}
                    .Concat(BitConverter.GetBytes(typeNameBytes.Length))
                    .Concat(typeNameBytes)
                    .ToArray(); //serializer id 255 + assembly qualified name

            //initialize reader and writer with dummy handlers that wait until the serializer is fully initialized
            _writer = (stream, o, session) =>
            {
                SpinWait.SpinUntil(() => _isInitialized);
                WriteValue(stream, o, session);
            };

            _reader = (stream, session) =>
            {
                SpinWait.SpinUntil(() => _isInitialized);
                return ReadValue(stream, session);
            };
        }
Exemplo n.º 12
0
        public void WriteMessage(QueueInfo message, IBufferWriter <byte> output)
        {
            ValueWriter writer = new ValueWriter(output);

            writer.WriteOctet(1);
            writer.WriteShortInt(ChannelId);
            var reseved    = writer.Reserve(4);
            var checkpoint = writer.Written;

            FrameWriter.WriteMethodFrame(50, 10, ref writer);
            writer.WriteShortInt(0); //reserved-1
            writer.WriteShortStr(message.Name);
            writer.WriteBit(message.Passive);
            writer.WriteBit(message.Durable);
            writer.WriteBit(message.Exclusive);
            writer.WriteBit(message.AutoDelete);
            writer.WriteBit(message.NoWait);
            writer.WriteTable(message.Arguments);
            var payloadSize = writer.Written - checkpoint;

            writer.WriteOctet(Constants.FrameEnd);

            Span <byte> span = stackalloc byte[4];

            BinaryPrimitives.WriteInt32BigEndian(span, payloadSize);
            reseved.Write(span);

            writer.Commit();
        }
        public void WriteMessage(ExchangeDeclare message, IBufferWriter <byte> output)
        {
            ValueWriter writer = new ValueWriter(output);

            writer.WriteOctet(1);
            writer.WriteShortInt(_channelId);
            var reserved   = writer.Reserve(4);
            var checkpoint = writer.Written;

            FrameWriter.WriteMethodFrame(40, 10, ref writer);
            writer.WriteShortInt(0); //reseved-1
            writer.WriteShortStr(message.Name);
            writer.WriteShortStr(message.Type);

            writer.WriteBit(message.Passive);
            writer.WriteBit(message.Durable);
            writer.WriteBit(message.AutoDelete);
            writer.WriteBit(message.Internal);
            writer.WriteBit(message.NoWait);
            writer.WriteTable(message.Arguments);
            var size = writer.Written - checkpoint;

            writer.WriteOctet(RabbitMQConstants.FrameEnd);

            Span <byte> sizeSpan = stackalloc byte[4];

            BinaryPrimitives.WriteInt32BigEndian(sizeSpan, size);
            reserved.Write(sizeSpan);

            writer.Commit();
        }
Exemplo n.º 14
0
        public void WriteMessage(QueueDelete message, IBufferWriter <byte> output)
        {
            ValueWriter writer = new ValueWriter(output);

            writer.WriteOctet(RabbitMQConstants.FrameMethod);
            writer.WriteShortInt(_channelId);
            var reserved   = writer.Reserve(4);
            var checkpoint = writer.Written;

            FrameWriter.WriteMethodFrame(50, 40, ref writer);
            writer.WriteShortInt(0); //reserved-1
            writer.WriteShortStr(message.Name);
            writer.WriteBit(message.IfUnused);
            writer.WriteBit(message.IfEmpty);
            writer.WriteBit(message.NoWait);
            writer.BitFlush();
            var payloadSize = writer.Written - checkpoint;

            writer.WriteOctet(RabbitMQConstants.FrameEnd);

            Span <byte> span = stackalloc byte[4];

            BinaryPrimitives.WriteInt32BigEndian(span, payloadSize);
            reserved.Write(span);

            writer.Commit();
        }
Exemplo n.º 15
0
        public void WriteMessage(bool _, IBufferWriter <byte> output)
        {
            ValueWriter writer = new ValueWriter(output);

            writer.Write(_heartbeatFrame);
            writer.Commit();
        }
Exemplo n.º 16
0
        public override void Write(Writer writer, StringBuilder obj)
        {
            base.Write(writer, obj);
            var stream = writer.Stream;

            stream.WriteByte(TagString);
            ValueWriter.Write(stream, obj.ToString());
        }
Exemplo n.º 17
0
        public override void Write(Writer writer, string obj)
        {
            base.Write(writer, obj);
            var stream = writer.Stream;

            stream.WriteByte(TagString);
            ValueWriter.Write(stream, obj);
        }
        public void WriteMessage(PublishPartialInfo message, IBufferWriter <byte> output)
        {
            var writer = new ValueWriter(output);

            _basicPublishWriter.WriteMessage(ref message.Info, ref writer);
            _contentHeaderWriter.WriteMessage(ref message.Header, ref writer);
            writer.Commit();
        }
Exemplo n.º 19
0
 private string WriteInt(int i)
 {
     using (var stream = new MemoryStream()) {
         ValueWriter.WriteInt(stream, i);
         var data = stream.GetArraySegment();
         return(Encoding.ASCII.GetString(data.Array, data.Offset, data.Count));
     }
 }
Exemplo n.º 20
0
        public void WriteMessage(byte[] message, IBufferWriter <byte> output)
        {
            ValueWriter writer = new ValueWriter(output);

            FrameWriter.WriteFrameHeader(Constants.FrameBody, _channelId, message.Length, ref writer);
            writer.WriteBytes(message);
            writer.WriteOctet(Constants.FrameEnd);
            writer.Commit();
        }
Exemplo n.º 21
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static <E extends Exception> void writeTo(ValueWriter<E> writer, double[] values) throws E
        public static void WriteTo <E>(ValueWriter <E> writer, double[] values) where E : Exception
        {
            writer.BeginArray(values.Length, ValueWriter_ArrayType.Double);
            foreach (double x in values)
            {
                writer.WriteFloatingPoint(x);
            }
            writer.EndArray();
        }
Exemplo n.º 22
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static <E extends Exception> void writeTo(ValueWriter<E> writer, String[] values) throws E
        public static void WriteTo <E>(ValueWriter <E> writer, string[] values) where E : Exception
        {
            writer.BeginArray(values.Length, ValueWriter_ArrayType.String);
            foreach (string x in values)
            {
                writer.WriteString(x);
            }
            writer.EndArray();
        }
Exemplo n.º 23
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static <E extends Exception> void writeTo(ValueWriter<E> writer, boolean[] values) throws E
        public static void WriteTo <E>(ValueWriter <E> writer, bool[] values) where E : Exception
        {
            writer.BeginArray(values.Length, ValueWriter_ArrayType.Boolean);
            foreach (bool x in values)
            {
                writer.WriteBoolean(x);
            }
            writer.EndArray();
        }
Exemplo n.º 24
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public <E extends Exception> void writeTo(ValueWriter<E> writer) throws E
        public override void WriteTo <E>(ValueWriter <E> writer) where E : Exception
        {
            writer.BeginArray(_value.Length, ValueWriter_ArrayType.Duration);
            foreach (DurationValue x in _value)
            {
                x.WriteTo(writer);
            }
            writer.EndArray();
        }
Exemplo n.º 25
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static <E extends Exception> void writeTo(ValueWriter<E> writer, long[] values) throws E
        public static void WriteTo <E>(ValueWriter <E> writer, long[] values) where E : Exception
        {
            writer.BeginArray(values.Length, ValueWriter_ArrayType.Long);
            foreach (long x in values)
            {
                writer.WriteInteger(x);
            }
            writer.EndArray();
        }
Exemplo n.º 26
0
 public void Write(Stream stream, DbEnvironment env)
 {
     Logger.Instance?.WriteLine($"-> {Type}: {Parameters.Length} parameters");
     stream.WriteByte((byte)Type);
     foreach (var parameter in Parameters)
     {
         ValueWriter.Write(parameter.Value, stream, parameter.Format, env.Encoding);
     }
 }
Exemplo n.º 27
0
        public void WriteMessage(NoPaylodMethodInfo message, IBufferWriter <byte> output)
        {
            var writer = new ValueWriter(output);

            FrameWriter.WriteFrameHeader(message.FrameType, message.Channel, 4, ref writer);
            FrameWriter.WriteMethodFrame(message.ClassId, message.MethodId, ref writer);
            writer.WriteOctet(RabbitMQConstants.FrameEnd);
            writer.Commit();
        }
Exemplo n.º 28
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static <E extends Exception> void writeTo(ValueWriter<E> writer, org.neo4j.graphdb.spatial.Point[] values) throws E
        public static void WriteTo <E>(ValueWriter <E> writer, Point[] values) where E : Exception
        {
            writer.BeginArray(values.Length, ValueWriter_ArrayType.Point);
            foreach (Point x in values)
            {
                PointValue value = Values.Point(x);
                writer.WritePoint(value.CoordinateReferenceSystem, value.Coordinate());
            }
            writer.EndArray();
        }
Exemplo n.º 29
0
 private static void WriteStringList(BinaryWriter writer, Dictionary <string, int> stringList,
                                     ValueWriter <string> writeString)
 {
     writer.Write(stringList.Count);
     foreach (string key in stringList.Keys)
     {
         writeString(writer, key);
         writer.Write(stringList[key]);
     }
 }
Exemplo n.º 30
0
        public void WriteMessage(RejectInfo message, IBufferWriter <byte> output)
        {
            ValueWriter writer = new ValueWriter(output);

            FrameWriter.WriteFrameHeader(Constants.FrameMethod, _channel, 13, ref writer);
            FrameWriter.WriteMethodFrame(60, 90, ref writer);
            writer.WriteLongLong(message.DeliveryTag);
            writer.WriteBit(message.Requeue);
            writer.WriteOctet(Constants.FrameEnd);
        }
Exemplo n.º 31
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: protected final <E extends Exception> void writeTo(ValueWriter<E> writer, ValueWriter_ArrayType type, java.time.temporal.Temporal[] values) throws E
        protected internal void WriteTo <E>(ValueWriter <E> writer, ValueWriter_ArrayType type, Temporal[] values) where E : Exception
        {
            writer.BeginArray(values.Length, type);
            foreach (Temporal x in values)
            {
                Value value = Values.TemporalValue(x);
                value.WriteTo(writer);
            }
            writer.EndArray();
        }
Exemplo n.º 32
0
 public ObjectAssembler(WiringContext wiringContext, ObjectAssemblerSettings settings = null)
 {
     this.wiringContext = wiringContext;
     typeConverterProvider = wiringContext.ConverterProvider;
     xamlTypeRepository = wiringContext.TypeContext;
     typeRepository = wiringContext.TypeContext;
     typeOperations = new TypeOperations(typeRepository.TypeFactory);
     startMemberWriter = new StartMemberWriter(this);
     getObjectWriter = new GetObjectWriter(this);
     startObjectWriter = new StartObjectWriter(this, settings?.RootInstance);
     valueWriter = new ValueWriter(this);
 }
Exemplo n.º 33
0
 public void Initialize(ValueReader reader, ValueWriter writer)
 {
     _reader = reader;
     _writer = writer;
     _isInitialized = true;
 }