예제 #1
0
        public void CanWriteAndReadRosTime()
        {
            using (var s = new MemoryStream())
            {
                var writer = new RosBinaryWriter(s);
                var reader = new RosBinaryReader(s);

                var writtenTime = new DateTime(2021, 03, 15, 18, 55, 12, 7, DateTimeKind.Unspecified)
                                  .ToRosTime();

                // Writing should convert to UTC.
                writer.WriteBuiltInType(typeof(RosTime), writtenTime);

                // Read back what we wrote.
                s.Position = 0;
                var readTime = (RosTime)reader.ReadRosTime();

                // Expected behaviour is that time in ROS-Messages is UTC.
                // Max difference is 0.001 since only milliseconds are preserved and ROS uses nanoseconds.
                readTime.Seconds.Should().Be(writtenTime.Seconds);
                readTime.Nanoseconds.Should().Be(writtenTime.Nanoseconds);
                readTime.DateTime.Kind.Should().Be(DateTimeKind.Utc);
                readTime.DateTime.Should().BeCloseTo(writtenTime.DateTime, TimeSpan.FromSeconds(0.001));
            }
        }
        private static object DeserializeActionResult(SerializationContext context, RosBinaryReader reader, IRosMessageTypeInfo messageTypeInfo, RosMessageSerializer serializer)
        {
            var header     = Deserialize <Header>(context, reader);
            var goalStatus = Deserialize <GoalStatus>(context, reader);
            var partialMessageDeserializer = PartialMessageDeserializer.CreateFromStream(context.Stream, serializer);

            return(new ActionResult(header, goalStatus, partialMessageDeserializer));
        }
예제 #3
0
        public void CanWriteAndReadString()
        {
            using (var s = new MemoryStream())
            {
                var writer = new RosBinaryWriter(s);
                var reader = new RosBinaryReader(s);

                writer.Write("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxASDF");

                s.Position = 0;

                reader.ReadString().Should().Be("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxASDF");
            }
        }
        private static T Deserialize <T>(SerializationContext context, RosBinaryReader reader)
        {
            var typeInfo  = context.MessageTypeRegistry.GetOrCreateMessageTypeInfo(typeof(T));
            var formatter = context.MessageFormatters.FindFormatterFor(typeInfo);

            if (formatter == null)
            {
                throw new NotSupportedException($"No formatter for message {typeInfo} found.");
            }

            var header = formatter.Deserialize(context, reader, typeInfo);

            return((T)header);
        }
예제 #5
0
        public void CanWriteAndReadTimeSpan()
        {
            using (var s = new MemoryStream())
            {
                var writer          = new RosBinaryWriter(s);
                var reader          = new RosBinaryReader(s);
                var writtenTimeSpan = TimeSpan.FromSeconds(123456789.123);

                writer.WriteBuiltInType(typeof(TimeSpan), writtenTimeSpan);

                s.Position = 0;
                var readTime = (TimeSpan)reader.ReadBuiltInType(typeof(TimeSpan));

                Assert.Equal(writtenTimeSpan, readTime);
            }
        }
예제 #6
0
        public void CanWriteAndReadInt()
        {
            using (var s = new MemoryStream())
            {
                var writer = new RosBinaryWriter(s);
                var reader = new RosBinaryReader(s);

                writer.Write((int)4);
                writer.Write((int)5);
                writer.Write((int)6);

                s.Position = 0;

                reader.ReadInt32().Should().Be(4);
                reader.ReadInt32().Should().Be(5);
                reader.ReadInt32().Should().Be(6);
            }
        }
예제 #7
0
        public void CanWriteAndReadDateTime()
        {
            using (var s = new MemoryStream())
            {
                var writer      = new RosBinaryWriter(s);
                var reader      = new RosBinaryReader(s);
                var writtenTime = DateTime.Now;

                // Writing should convert to UTC.
                writer.WriteBuiltInType(typeof(DateTime), writtenTime);

                // Read back what we wrote.
                s.Position = 0;
                var readTime = (DateTime)reader.ReadBuiltInType(typeof(DateTime));

                // Expected behaviour is that time in ROS-Messages is UTC.
                // Max difference is 0.001 since only milliseconds are preserved and ROS uses nanoseconds.
                Assert.Equal(writtenTime.ToUniversalTime(), readTime, TimeSpan.FromSeconds(0.001));
            }
        }
예제 #8
0
        public void CanWriteAndReadBuiltInTypes2(string rosType, object writeValue, object expectedValue)
        {
            var primitiveRosType = RosType.Parse(rosType);

            primitiveRosType.IsBuiltIn.Should().BeTrue();
            primitiveRosType.IsArray.Should().BeFalse();

            using (var s = new MemoryStream())
            {
                var writer = new RosBinaryWriter(s);
                var reader = new RosBinaryReader(s);

                writer.WriteBuiltInType(primitiveRosType, writeValue);

                s.Position = 0;

                var readValue = reader.ReadBuiltInType(primitiveRosType, expectedValue.GetType());

                readValue.Should().BeEquivalentTo(expectedValue);
                readValue.GetType().Should().Be(expectedValue.GetType());
            }
        }
        private static ActionGoal DeserializeActionGoal(SerializationContext context, RosBinaryReader reader, IRosMessageTypeInfo messageTypeInfo, RosMessageSerializer serializer)
        {
            var header = Deserialize <Header>(context, reader);
            var goalId = Deserialize <GoalID>(context, reader);
            var partialMessageDeserializer = PartialMessageDeserializer.CreateFromStream(context.Stream, serializer);

            return(new ActionGoal(header, goalId, partialMessageDeserializer));
        }
        public object Deserialize(SerializationContext context, RosBinaryReader reader, IRosMessageTypeInfo messageTypeInfo)
        {
            var deserializer = Deserializers[messageTypeInfo.Type];

            return(deserializer(context, reader, messageTypeInfo, Serializer));
        }