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]); } }
public static void Deserialize(Message instance, UnsafeBufferWrapper wrapper) { instance.Header = wrapper.Read<Header>(); instance.Id = wrapper.ReadInt32(); instance.PartCount = wrapper.ReadInt32(); for (var i = 0; i < instance.PartCount; i++) { instance.Parts[i] = wrapper.Read<MessagePart>(); } }
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); } }
public static void Deserialize(Message instance, UnsafeBufferWrapper wrapper) { instance.Header = *(Header*)wrapper.Cursor; wrapper.Cursor += sizeof(Header); instance.Id = wrapper.ReadInt32(); instance.PartCount = wrapper.ReadInt32(); for (var i = 0; i < instance.PartCount; i++) { instance.Parts[i] = *(MessagePart*)wrapper.Cursor; wrapper.Cursor += sizeof(MessagePart); } }
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); } }
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); } }
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); } }
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); } }
protected override void DoRun(int batchCount, int batchSize, int messagePartCount) { using (var wrapper = new UnsafeBufferWrapper(new byte[1024 * 100])) { var message = CreateMessage(messagePartCount); for (var i = 0; i < batchCount; i++) { wrapper.ResetCursor(); for (var j = 0; j < batchSize; j++) { UnsafeBufferWrapperSerializer2.Serialize(message, wrapper); } } } }
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); } }
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); } }
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); } }
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); } }
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); } }