Пример #1
0
        public static void Serialize(Message instance, UnsafeBufferWrapper wrapper)
        {
            wrapper.Write3(ref instance.Header);
            wrapper.Write(instance.Id);
            wrapper.Write(instance.PartCount);

            for (var i = 0; i < instance.PartCount; i++)
            {
                wrapper.Write3(ref instance.Parts[i]);
            }
        }
Пример #2
0
        public static void Serialize(Message instance, UnsafeBufferWrapper wrapper)
        {
            *(Header*)wrapper.Cursor = instance.Header;
            wrapper.Cursor += sizeof(Header);
            wrapper.Write(instance.Id);
            wrapper.Write(instance.PartCount);

            for (var i = 0; i < instance.PartCount; i++)
            {
                *(MessagePart*)wrapper.Cursor = instance.Parts[i];
                wrapper.Cursor += sizeof(MessagePart);
            }
        }
Пример #3
0
 public void Should_read_and_write_decimals()
 {
     var buffer = new byte[1024];
     using (var wrapper = new UnsafeBufferWrapper(buffer))
     {
         const decimal value = 42.42m;
         wrapper.Write(value);
         Assert.AreEqual(wrapper.Position, sizeof (decimal));
         wrapper.ResetCursor();
         var readValue = wrapper.ReadDecimal();
         Assert.AreEqual(value, readValue);
     }
 }
Пример #4
0
 public void Should_read_and_write_custom_structs()
 {
     var buffer = new byte[1024];
     using (var wrapper = new UnsafeBufferWrapper(buffer))
     {
         var value = new CustomStruct {Field1 = 42, Field2 = TimeSpan.FromHours(42), Field3 = DateTime.UtcNow};
         wrapper.Write(ref value);
         Assert.AreEqual(wrapper.Position, sizeof(CustomStruct));
         wrapper.ResetCursor();
         var readValue = wrapper.Read<CustomStruct>();
         Assert.AreEqual(value, readValue);
     }
 }
Пример #5
0
 public void Should_read_and_write_chars()
 {
     var buffer = new byte[1024];
     using (var wrapper = new UnsafeBufferWrapper(buffer))
     {
         const char value = 'x';
         wrapper.Write(value);
         Assert.AreEqual(wrapper.Position, sizeof (char));
         wrapper.ResetCursor();
         var readValue = wrapper.ReadChar();
         Assert.AreEqual(value, readValue);
     }
 }
Пример #6
0
 public void Should_read_and_write_bytes()
 {
     var buffer = new byte[1024];
     using (var wrapper = new UnsafeBufferWrapper(buffer))
     {
         const byte value = (byte) 42;
         wrapper.Write(value);
         Assert.AreEqual(wrapper.Position, sizeof (byte));
         wrapper.ResetCursor();
         var readValue = wrapper.ReadByte();
         Assert.AreEqual(value, readValue);
     }
 }
Пример #7
0
 public void Should_read_and_write_ushorts()
 {
     var buffer = new byte[1024];
     using (var wrapper = new UnsafeBufferWrapper(buffer))
     {
         const ushort value = 42;
         wrapper.Write(value);
         Assert.AreEqual(wrapper.Position, sizeof (ushort));
         wrapper.ResetCursor();
         var readValue = wrapper.ReadUInt16();
         Assert.AreEqual(value, readValue);
     }
 }
Пример #8
0
 public void Should_read_and_write_structs()
 {
     var buffer = new byte[1024];
     using (var wrapper = new UnsafeBufferWrapper(buffer))
     {
         var value = Guid.NewGuid();
         wrapper.Write(ref value);
         Assert.AreEqual(wrapper.Position, sizeof (Guid));
         wrapper.ResetCursor();
         var readValue = wrapper.Read<Guid>();
         Assert.AreEqual(value, readValue);
     }
 }
Пример #9
0
 public void Should_read_and_write_longs()
 {
     var buffer = new byte[1024];
     using (var wrapper = new UnsafeBufferWrapper(buffer))
     {
         const long value = 42L;
         wrapper.Write(value);
         Assert.AreEqual(wrapper.Position, sizeof (long));
         wrapper.ResetCursor();
         var readValue = wrapper.ReadInt64();
         Assert.AreEqual(value, readValue);
     }
 }
Пример #10
0
 public void Should_read_and_write_floats()
 {
     var buffer = new byte[1024];
     using (var wrapper = new UnsafeBufferWrapper(buffer))
     {
         const float value = 42.42f;
         wrapper.Write(value);
         Assert.AreEqual(wrapper.Position, sizeof (float));
         wrapper.ResetCursor();
         var readValue = wrapper.ReadSingle();
         Assert.AreEqual(value, readValue);
     }
 }
Пример #11
0
 public void Should_read_and_write_enums()
 {
     var buffer = new byte[1024];
     using (var wrapper = new UnsafeBufferWrapper(buffer))
     {
         var value = ConsoleColor.Cyan;
         wrapper.Write(ref value);
         Assert.AreEqual(wrapper.Position, sizeof (ConsoleColor));
         wrapper.ResetCursor();
         var readValue = wrapper.Read<ConsoleColor>();
         Assert.AreEqual(value, readValue);
     }
 }