Exemplo n.º 1
0
        static void Main()
        {
            // The Example type internally uses instances generic schemas Generic1 and Generic2
            var src = new Example
            {
                Field = { Field = new Generic2<int> { Field = 13 } }
            };

            // We can also instantiate generic schema in the C# program
            var src1 = new Generic1<Example> { Field = src };
            var src2 = new Generic2<double> {Field = 3.14};

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);
            Serialize.To(writer, src1);
            Serialize.To(writer, src2);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Example>.From(reader);
            Debug.Assert(Comparer.Equal(src, dst));

            var dst1 = Deserialize<Generic1<Example>>.From(reader);
            Debug.Assert(Comparer.Equal(src1, dst1));

            var dst2 = Deserialize<Generic2<double>>.From(reader);
            Debug.Assert(Comparer.Equal(src2, dst2));
        }
Exemplo n.º 2
0
        static void Main()
        {
            // Get runtime schemas for two "versions" of Example schema. 
            // When using untagged protocols the consumer must have access to runtime schema of the data.
            var schema1 = Schema<Example1>.RuntimeSchema;
            var schema2 = Schema<Example2>.RuntimeSchema;

            // Create and cache deserializers for objects of type Example2 from payloads using the two schemas
            var deserializers = new Dictionary<string, Deserializer<SimpleBinaryReader<InputBuffer>>>
            {
                {"Example1", new Deserializer<SimpleBinaryReader<InputBuffer>>(typeof(Example2), schema1)},
                {"Example2", new Deserializer<SimpleBinaryReader<InputBuffer>>(typeof(Example2), schema2)}
            };

            // Create payload serializing an instance of Example1
            var src = new Example1
            {
                Enabled = true,
                Name = "Foo"
            };

            var output = new OutputBuffer();
            var writer = new SimpleBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new SimpleBinaryReader<InputBuffer>(input);

            // Use the precreated deserializer to deserialize an instance of Example2 schema for Example1
            var dst = deserializers["Example1"].Deserialize<Example2>(reader);
            Debug.Assert(src.Enabled == dst.Enabled);
            Debug.Assert(src.Name == dst.Name);
        }
Exemplo n.º 3
0
        public void Varint()
        {
            IntTest<ushort> test16 = (value) =>
            {
                var output = new OutputBuffer();
                output.WriteVarUInt16(value);
                var input = new InputBuffer(output.Data);
                Assert.AreEqual(value, input.ReadVarUInt16());
            };

            IntTest<uint> test32 = (value) =>
            {
                var output = new OutputBuffer();
                output.WriteVarUInt32(value);
                var input = new InputBuffer(output.Data);
                Assert.AreEqual(value, input.ReadVarUInt32());
            };

            IntTest<ulong> test64 = (value) =>
            {
                var output = new OutputBuffer();
                output.WriteVarUInt64(value);
                var input = new InputBuffer(output.Data);
                Assert.AreEqual(value, input.ReadVarUInt64());
            };

            test16(ushort.MinValue);
            test16(ushort.MaxValue);
            test32(uint.MinValue);
            test32(uint.MaxValue);
            test64(ulong.MinValue);
            test64(ulong.MaxValue);
        }
Exemplo n.º 4
0
        static void Main()
        {
            var data = Enumerable.Range(0, 255).Select(i => (byte)i).ToArray();
            var src = new Example
            {
                ListOfBlobs =
                {
                    new ArraySegment<byte>(data, 0, 10), 
                    new ArraySegment<byte>(data, 10, 10)
                },

                NullableBlob = new ArraySegment<byte>(data, 20, 10),
                UninitializeBlob = new ArraySegment<byte>(data, 30, 70)
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Example>.From(reader);
            Debug.Assert(Comparer.Equal(src, dst));
        }
 public override object Deserialize(string serialized)
 {
     Initialize();
     var bytes = Convert.FromBase64String(serialized);
     var input = new InputBuffer(bytes);
     var reader = new FastBinaryReader<InputBuffer>(input);
     return _deserializer.Deserialize(reader);
 }
Exemplo n.º 6
0
        public void MakeProtocolErrorFrame_JustErrorCode_MakesAFrame()
        {
            var frame = EpoxyConnection.MakeProtocolErrorFrame(MeaninglessErrorCode, null, LoggerTests.BlackHole);
            Assert.NotNull(frame);
            Assert.AreEqual(1, frame.Framelets.Count);
            Assert.AreEqual(FrameletType.ProtocolError, frame.Framelets[0].Type);

            var inputBuffer = new InputBuffer(frame.Framelets[0].Contents);
            var fastBinaryReader = new FastBinaryReader<InputBuffer>(inputBuffer, version: 1);
            var error = Deserialize<ProtocolError>.From(fastBinaryReader);
            Assert.AreEqual(MeaninglessErrorCode, error.error_code);
            Assert.Null(error.details);
        }
        private static object DeserializeCompactBinary(
            ushort version,
            byte[] data,
            Deserializer<CompactBinaryReader<InputBuffer>> deserializer)
        {
            var inputStream = new InputBuffer(data);

            var reader = new CompactBinaryReader<InputBuffer>(inputStream, version);

            var outputObject = deserializer.Deserialize(reader);

            return outputObject;
        }
Exemplo n.º 8
0
        static void Main()
        {
            var src = new Example
            {
                id = new Guid("{DC37ECC5-9E39-49C9-931B-51138D648262}")
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Example>.From(reader);
            Debug.Assert(dst.id == src.id);
        }
Exemplo n.º 9
0
        static void Main()
        {
            var src = new Example
            {
                Name = "FooBar",
                Constants = {  3.14, 6.28 }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Example>.From(reader);
            Debug.Assert(Comparer.Equal(src, dst));
        }
Exemplo n.º 10
0
        static void Main()
        {
            var src = new Example
            {
                Price = 9999999999999999999999999999M,
                Numbers = { 79228162514264337593543950335M }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Example>.From(reader);
            Debug.Assert(Comparer.Equal(src, dst));
        }
Exemplo n.º 11
0
        static void Main()
        {
            var src = new Example
            {
                Now = DateTime.Now,
                Dates = { new DateTime(2017, 1, 29) }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Example>.From(reader);
            Debug.Assert(Comparer.Equal(src, dst));
        }
Exemplo n.º 12
0
        static void Main()
        {
            var src = new Example
            {
                Name = "foo",
                Constants = { 3.14, 6.28 }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Marshal.To(writer, src);

            var input = new InputBuffer(output.Data);

            // We don't need to specify protocol for unmarshaling, 
            // it is determined from information stored in the payload.
            var dst = Unmarshal<Example>.From(input);
            Debug.Assert(Comparer.Equal(src, dst));
        }
Exemplo n.º 13
0
        static void Main()
        {
            var src = new Example
            {
                id_str = new Guid("{DC37ECC5-9E39-49C9-931B-51138D648262}"),
                id_bin = new Guid("{0F5F6768-1608-4D5C-A11D-B176D0D792B5}"),
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Example>.From(reader);
            Debug.Assert(dst.id_str == src.id_str);
            Debug.Assert(dst.id_bin == src.id_bin);
        }
Exemplo n.º 14
0
        static void Main()
        {
            var src = new Message
                          {
                              Header = new Header { Origin = "contoso.com", Destination = "fabrikam.com" },
                              Priority = Priority.Normal,
                              MessagePayload = 42
                          };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Message>.From(reader);
            Debug.Assert(Comparer.Equal(src, dst));
        }
Exemplo n.º 15
0
        static void Main()
        {
            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            // Get runtime schema for type Example and serialize SchemaDef
            Serialize.To(writer, Schema<Example>.RuntimeSchema.SchemaDef);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var schemaDef = Deserialize<SchemaDef>.From(reader);
            var schema = new RuntimeSchema(schemaDef);

            Debug.Assert(schema.IsStruct);
            Debug.Assert(schema.StructDef.metadata.qualified_name == "Examples.Example");
            Debug.Assert(schema.StructDef.metadata.attributes["StructAttribute"] == "Value of the attribute");
            Debug.Assert(schema.StructDef.fields[0].metadata.attributes["FieldAttribute"] == "Value of the attribute");
            Debug.Assert(schema.StructDef.fields[0].type.key.id == BondDataType.BT_UINT32);
            Debug.Assert(schema.SchemaDef.structs[1].fields[0].metadata.default_value.string_value == "this is a string");
        }
Exemplo n.º 16
0
        static void Main()
        {
            var src = new Example
            {
                Widgets =
                {
                    new Widget { Name = "Konfabulator", Number = 3.14 }
                }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var xmlString = new StringBuilder();
            var xmlWriter = new SimpleXmlWriter(XmlWriter.Create(xmlString, new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                Indent = true
            }));
            Transcode<Example>.FromTo(reader, xmlWriter);
            xmlWriter.Flush();

            Debug.Assert(xmlString.ToString() == 
@"<Example>
  <Widgets>
    <Item>
      <Widget>
        <Name>Konfabulator</Name>
        <Number>3.14</Number>
      </Widget>
    </Item>
  </Widgets>
</Example>");
        }
Exemplo n.º 17
0
        static void Main()
        {
            var src = new Example
            {
                Name = "FooBar",
                Constants = {  3.14, 6.28 }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            // The first calls to Serialize.To and Deserialize<T>.From can take
            // a relatively long time because they generate the de/serializer 
            // for a given type and protocol.
            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Example>.From(reader);
            Debug.Assert(Comparer.Equal(src, dst));
        }
Exemplo n.º 18
0
        static void Main()
        {
            var src = new Example
            {
                Name = "FooBar",
                Constants = { 3.14, 6.28 }
            };

            // Create de/serializer for the Example class and Compact Binary protocol.
            // This may take relatively long time so usually the objects should be cached and reused.
            var exampleSerializer = new Serializer<CompactBinaryWriter<OutputBuffer>>(typeof(Example));
            var exampleDeserializer = new Deserializer<CompactBinaryReader<InputBuffer>>(typeof(Example));

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            exampleSerializer.Serialize(src, writer);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = exampleDeserializer.Deserialize<Example>(reader);
            Debug.Assert(Comparer.Equal(src, dst));
        }
Exemplo n.º 19
0
		public iCalLexer(InputBuffer ib)		 : this(new LexerSharedInputState(ib))
		{
		}
Exemplo n.º 20
0
		public DebuggingInputBuffer(InputBuffer buffer)
		{
			this.buffer = buffer;
			inputBufferEventSupport = new InputBufferEventSupport(this);
		}
Exemplo n.º 21
0
        internal static ClassifyState TransitionExpectConfig(
            ClassifyState state, Frame frame, ref ProtocolErrorCode? errorCode, ref FrameDisposition disposition,
            Logger logger)
        {
            Debug.Assert(state == ClassifyState.ExpectConfig);
            Debug.Assert(frame != null);

            if (frame.Count == 0 || frame.Framelets[0].Type != FrameletType.EpoxyConfig)
            {
                return ClassifyState.InternalStateError;
            }

            if (frame.Count != 1)
            {
                logger.Site().Error("Config frame had trailing framelets.");
                errorCode = ProtocolErrorCode.MALFORMED_DATA;
                return ClassifyState.MalformedFrame;
            }

            var framelet = frame.Framelets[0];

            var inputBuffer = new InputBuffer(framelet.Contents);
            var fastBinaryReader = new FastBinaryReader<InputBuffer>(inputBuffer, version: 1);

            // We don't currently do anything with the config aside from try to deserialize it.
            EpoxyConfig config;
            switch (configDeserializer.TryDeserialize(fastBinaryReader, out config))
            {
                case Deserialize.Result.Success:
                    break;

                default:
                    logger.Site().Error("Didn't get a valid {0}.", nameof(EpoxyConfig));
                    errorCode = ProtocolErrorCode.MALFORMED_DATA;
                    return ClassifyState.MalformedFrame;
            }

            disposition = FrameDisposition.ProcessConfig;
            return ClassifyState.ClassifiedValidFrame;
        }
Exemplo n.º 22
0
        static void Main()
        {
            var circle = new Circle
            {
                Type = Type.Circle,
                Radius = 3.14
            };

            var rectangle = new Rectangle
            {
                Type = Type.Rectange,
                Width = 10,
                Height = 5.5
            };

            var src = new Polymorphic
            {
                Shapes = 
                {
                    new Bonded<Circle>(circle),
                    new Bonded<Rectangle>(rectangle)
                }
            };

            var output = new OutputBuffer();
            var writer = new CompactBinaryWriter<OutputBuffer>(output);

            Serialize.To(writer, src);

            var input = new InputBuffer(output.Data);
            var reader = new CompactBinaryReader<InputBuffer>(input);

            var dst = Deserialize<Polymorphic>.From(reader);

            var deserializers = new Dictionary<Type, Deserializer<CompactBinaryReader<InputBuffer>>>
            {
                {Type.Circle, new Deserializer<CompactBinaryReader<InputBuffer>>(typeof(Circle))},
                {Type.Rectange, new Deserializer<CompactBinaryReader<InputBuffer>>(typeof(Rectangle))}
            };
            
            foreach (var item in dst.Shapes)
            {
                // Deserialize item as Shape and extract object type
                var type = item.Deserialize().Type;
                
                // Select one of the precreated deserializers based on the item type
                var shape = deserializers[type].Deserialize(item);
                
                if (shape.GetType() == typeof(Circle))
                {
                    Debug.Assert(Comparer.Equal(circle, shape as Circle));
                }

                if (shape.GetType() == typeof(Rectangle))
                {
                    Debug.Assert(Comparer.Equal(rectangle, shape as Rectangle));
                }

                // Alternatively the generic method IBonded<T>.Deserialize<U> can be used
                if (type == Type.Circle)
                {
                    var c = item.Deserialize<Circle>();
                    Debug.Assert(Comparer.Equal(circle, c));
                }

                if (type == Type.Rectange)
                {
                    var r = item.Deserialize<Rectangle>();
                    Debug.Assert(Comparer.Equal(rectangle, r));
                }
            }
        }
Exemplo n.º 23
0
		public SurveyorLexer(InputBuffer ib)		 : this(new LexerSharedInputState(ib))
		{
		}
Exemplo n.º 24
0
        internal static ClassifyState TransitionExpectEpoxyHeaders(
            ClassifyState state, Frame frame, ref EpoxyHeaders headers, ref ProtocolErrorCode? errorCode, Logger logger)
        {
            Debug.Assert(state == ClassifyState.ExpectEpoxyHeaders);
            Debug.Assert(frame != null);

            if (frame.Count == 0 || frame.Framelets[0].Type != FrameletType.EpoxyHeaders)
            {
                return ClassifyState.InternalStateError;
            }

            var framelet = frame.Framelets[0];
            var inputBuffer = new InputBuffer(framelet.Contents);
            var fastBinaryReader = new FastBinaryReader<InputBuffer>(inputBuffer, version: 1);
            switch (headersDeserializer.TryDeserialize(fastBinaryReader, out headers))
            {
                case Deserialize.Result.Success:
                    break;

                default:
                    logger.Site().Error("Didn't get a valid {0}.", nameof(EpoxyHeaders));
                    errorCode = ProtocolErrorCode.MALFORMED_DATA;
                    return ClassifyState.MalformedFrame;
            }

            logger.Site().Debug("Deserialized {0} with conversation ID {1} and message type {2}.",
                nameof(EpoxyHeaders), headers.conversation_id, headers.message_type);
            return ClassifyState.ExpectOptionalLayerData;
        }
Exemplo n.º 25
0
		public DebuggingCharScanner(InputBuffer cb) : base(cb)
		{
			InitBlock();
		}
Exemplo n.º 26
0
        internal static ClassifyState TransitionExpectProtocolError(
            ClassifyState state, Frame frame, ref ProtocolError error, ref FrameDisposition disposition, Logger logger)
        {
            if (state != ClassifyState.ExpectProtocolError || frame == null || frame.Count == 0
                || frame.Framelets[0].Type != FrameletType.ProtocolError)
            {
                return ClassifyState.InternalStateError;
            }

            if (frame.Count > 1)
            {
                logger.Site().Error("Protocol error frame had trailing framelets.");
                return ClassifyState.ErrorInErrorFrame;
            }

            var framelet = frame.Framelets[0];

            var inputBuffer = new InputBuffer(framelet.Contents);
            var fastBinaryReader = new FastBinaryReader<InputBuffer>(inputBuffer, version: 1);
            switch (errorDeserializer.TryDeserialize(fastBinaryReader, out error))
            {
                case Deserialize.Result.Success:
                    break;

                default:
                    logger.Site().Error("Didn't get a valid {0}.", nameof(ProtocolError));
                    return ClassifyState.ErrorInErrorFrame;
            }

            logger.Site().Debug("Deserialized {0} with code {1}.", nameof(ProtocolError), error.error_code);
            disposition = FrameDisposition.HandleProtocolError;
            return ClassifyState.ClassifiedValidFrame;
        }