Exemplo n.º 1
0
        public static bool RunDeserializer(DeserializerDlg <Word> desWord, DeserializerDlg <Simple> desSimple)
        {
            Serializer.Setup(desWord);
            Serializer.Setup(desSimple);

            var expected = new Simple
            {
                Identifier = 150,
                Sentence   = new[] { new Word {
                                         Value = "hello"
                                     }, new Word {
                                         Value = "there"
                                     } }
            };

            Memory <byte> buffer = new byte[19];

            // 4 bytes
            BinaryPrimitives.WriteInt32BigEndian(buffer.Span, expected.Identifier);
            // 4+=1 byte for word count
            buffer.Span.Slice(4)[0] = 2;
            // 5+=2 bytes for the 1st word length
            BinaryPrimitives.WriteInt16BigEndian(buffer.Span.Slice(5), 5);
            // 7+=5 bytes for the 1st word
            Encoding.UTF8.GetBytes(expected.Sentence[0].Value, buffer.Span.Slice(7));
            // 12+=2 bytes for the 2nd word length
            BinaryPrimitives.WriteInt16BigEndian(buffer.Span.Slice(12), 5);
            // 14+=5 bytes for the 2nd word
            Encoding.UTF8.GetBytes(expected.Sentence[1].Value, buffer.Span.Slice(14));

            var deserialized = new Simple();
            var input        = new ReadOnlySequence <byte>(buffer);

            return(Serializer.TryDeserialize(ref input, deserialized, out var bytesRead));
        }
Exemplo n.º 2
0
 public static void Setup <T>(DeserializerDlg <T> des) =>
 SerializerStorage <T> .TryDeserialize = des;
Exemplo n.º 3
0
        public void Should_Deserialize_Simple_via_manual_CSharp_code()
        {
            DeserializerDlg <Word> dlgWord =
                /*DeserializerDlg<Word>*/ (ref ReadOnlySequence <Byte> input, Word value, out Int64 bytesRead) =>
            {
                SequenceReader <Byte> reader;
                String wordValue;

                reader = new SequenceReader <Byte>(input);
                if (ReaderExtensions.TryReadValue <String>(
                        ref reader,
                        out wordValue) == false)
                {
                    bytesRead = reader.Consumed;
                    return(false);
                }

                value.Value = wordValue;
                bytesRead   = reader.Consumed;
                return(true);
            };

            DeserializerDlg <Simple> dlgSimple =
                /*DeserializerDlg<Simple>*/ (ref ReadOnlySequence <Byte> input, Simple value, out Int64 bytesRead) =>
            {
                SequenceReader <Byte> reader;
                Int32  identifier;
                Word[] content;
                Byte   contentLength;

                reader = new SequenceReader <Byte>(input);
                if (ReaderExtensions.TryReadValue <Int32>(
                        ref reader,
                        out identifier) == false)
                {
                    bytesRead = reader.Consumed;
                    return(false);
                }

                if (ReaderExtensions.TryReadValue <Byte>(
                        ref reader,
                        out contentLength) == false)
                {
                    bytesRead = reader.Consumed;
                    return(false);
                }

                if (Serializer.TryDeserializeValues <Word>(
                        ref reader,
                        (Int32)contentLength,
                        out content) == false)
                {
                    bytesRead = reader.Consumed;
                    return(false);
                }

                value.Identifier = identifier;
                value.Sentence   = content;
                bytesRead        = reader.Consumed;
                return(true);
            };

            Serializer.Setup(dlgWord);
            Serializer.Setup(dlgSimple);

            RunDeserializer();
        }
Exemplo n.º 4
0
        public static void CreateLightExpression_and_CompileFast(
            out DeserializerDlg <Word> desWord,
            out DeserializerDlg <Simple> desSimple)
        {
            var reader    = Variable(typeof(SequenceReader <byte>), "reader");
            var bytesRead = Parameter(typeof(long).MakeByRefType(), "bytesRead");
            var input     = Parameter(typeof(ReadOnlySequence <byte>).MakeByRefType(), "input");

            var createReader = Assign(reader,
                                      New(typeof(SequenceReader <byte>).GetConstructor(new[] { typeof(ReadOnlySequence <byte>) }), input));

            var returnTarget = Label(typeof(bool));
            var returnLabel  = Label(returnTarget, Constant(default(bool)));
            var returnFalse  =
                Block(
                    Assign(bytesRead,
                           Property(reader,
                                    typeof(SequenceReader <byte>).GetProperty(nameof(SequenceReader <byte> .Consumed)))),
                    Block(Return(returnTarget, Constant(false), typeof(bool)), returnLabel));

            var returnTrue =
                Block(
                    Assign(bytesRead,
                           Property(reader,
                                    typeof(SequenceReader <byte>).GetProperty(nameof(SequenceReader <byte> .Consumed)))),
                    Block(Return(returnTarget, Constant(true), typeof(bool)), returnLabel));

            var valueWord    = Parameter(typeof(Word), "value");
            var wordValueVar = Variable(typeof(string), "wordValue");
            var expr0        = Lambda <DeserializerDlg <Word> >(
                Block(new[] { reader, wordValueVar },
                      createReader,
                      IfThen(
                          NotEqual(Call(_tryRead.MakeGenericMethod(typeof(string)), reader, wordValueVar), Constant(true)),
                          returnFalse),
                      Assign(Property(valueWord, nameof(Word.Value)), wordValueVar),
                      returnTrue),
                input, valueWord, bytesRead);

            desWord = expr0.CompileFast();

            var valueSimple   = Parameter(typeof(Simple), "value");
            var identifierVar = Variable(typeof(int), "identifier");
            var contentVar    = Variable(typeof(Word[]), "content");
            var contentLenVar = Variable(typeof(byte), "contentLength");

            var expr1 = Lambda <DeserializerDlg <Simple> >(
                Block(new[] { reader, identifierVar, contentVar, contentLenVar },
                      createReader,
                      IfThen(
                          NotEqual(Call(_tryRead.MakeGenericMethod(typeof(int)), reader, identifierVar), Constant(true)),
                          returnFalse),
                      IfThen(
                          NotEqual(Call(_tryRead.MakeGenericMethod(typeof(byte)), reader, contentLenVar), Constant(true)),
                          returnFalse),
                      IfThen(
                          NotEqual(Call(_tryDeserialize.MakeGenericMethod(typeof(Word)), reader, Convert(contentLenVar, typeof(int)), contentVar), Constant(true)),
                          returnFalse),
                      Assign(Property(valueSimple, nameof(Simple.Identifier)), identifierVar),
                      Assign(Property(valueSimple, nameof(Simple.Sentence)), contentVar),
                      returnTrue),
                input, valueSimple, bytesRead);

            desSimple = expr1.CompileFast();
        }