diff --git a/QuickLink/Messages/MessageReader.cs b/QuickLink/Messages/MessageReader.cs
index 732ccfa..859856c 100644
--- a/QuickLink/Messages/MessageReader.cs
+++ b/QuickLink/Messages/MessageReader.cs
@@ -35,6 +35,26 @@ private void EnsureCanReadLength(int length)
throw new InvalidOperationException("Cannot read beyond the end of the buffer");
}
+ ///
+ /// Reads a boolean from the buffer.
+ ///
+ /// The boolean read from the buffer
+ public bool ReadBool()
+ {
+ EnsureCanReadLength(1);
+ return _buffer[_offset++] == 1;
+ }
+
+ ///
+ /// Reads a byte from the buffer.
+ ///
+ /// The byte read from the buffer
+ public byte ReadByte()
+ {
+ EnsureCanReadLength(1);
+ return _buffer[_offset++];
+ }
+
///
/// Reads a 16-bit signed integer from the buffer.
///
@@ -59,6 +79,30 @@ public int ReadInt32()
return value;
}
+ ///
+ /// Reads a 32-bit signed float from the buffer.
+ ///
+ /// The float read from the buffer.
+ public float ReadFloat()
+ {
+ EnsureCanReadLength(4);
+ float value = BitConverter.ToSingle(_buffer, _offset);
+ _offset += 4;
+ return value;
+ }
+
+ ///
+ /// Reads a 32-bit signed double from the buffer.
+ ///
+ /// The double read from the buffer.
+ public double ReadDouble()
+ {
+ EnsureCanReadLength(8);
+ double value = BitConverter.ToDouble(_buffer, _offset);
+ _offset += 8;
+ return value;
+ }
+
///
/// Reads a string from the buffer.
///
@@ -72,6 +116,19 @@ public string ReadString()
return value;
}
+ ///
+ /// Writes the specified amount of bytes to the provided buffer.
+ ///
+ /// The buffer to write the bytes to.
+ /// The offset at which to write the bytes in the output buffer.
+ /// The amount of bytes to write to the buffer.
+ public void ReadBytes(byte[] buffer, int offset, int length)
+ {
+ EnsureCanReadLength(length);
+ Array.Copy(_buffer, _offset, buffer, offset, length);
+ _offset += length;
+ }
+
///
/// Sets the current position in the buffer to the specified offset.
///
diff --git a/QuickLink/Messages/MessageWriter.cs b/QuickLink/Messages/MessageWriter.cs
index 509c607..c918698 100644
--- a/QuickLink/Messages/MessageWriter.cs
+++ b/QuickLink/Messages/MessageWriter.cs
@@ -23,6 +23,24 @@ public MessageWriter(MessageType type)
WriteInt16(type.Id);
}
+ ///
+ /// Writes a boolean to the underlying memory stream
+ ///
+ /// The value to write
+ public void WriteBool(bool value)
+ {
+ WriteByte((byte)(value ? 1 : 0));
+ }
+
+ ///
+ /// Writes a byte to the underlying memory stream
+ ///
+ /// The value to write
+ public void WriteByte(byte value)
+ {
+ _memoryStream.WriteByte(value);
+ }
+
///
/// Writes a 16-bit signed integer to the underlying memory stream.
///
@@ -43,6 +61,26 @@ public void WriteInt32(int value)
_memoryStream.Write(buffer, 0, buffer.Length);
}
+ ///
+ /// Writes a 32-bit signed float to the underlying memory stream.
+ ///
+ /// The value to write.
+ public void WriteFloat(float value)
+ {
+ byte[] buffer = BitConverter.GetBytes(value);
+ _memoryStream.Write(buffer, 0, buffer.Length);
+ }
+
+ ///
+ /// Writes a 32-bit signed double to the underlying memory stream.
+ ///
+ /// The value to write.
+ public void WriteDouble(double value)
+ {
+ byte[] buffer = BitConverter.GetBytes(value);
+ _memoryStream.Write(buffer, 0, buffer.Length);
+ }
+
///
/// Writes a string to the underlying memory stream.
///
diff --git a/QuickLinkTests/MessageTests.cs b/QuickLinkTests/MessageTests.cs
index 9280489..63e6a54 100644
--- a/QuickLinkTests/MessageTests.cs
+++ b/QuickLinkTests/MessageTests.cs
@@ -55,4 +55,41 @@ public void MessageTypesWithSameNameAreEqualAndAttachProperly()
Assert.Equal(2, message1FiredCount);
Assert.Equal(2, message2FiredCount);
}
+
+ [Fact(DisplayName = "Messages can properly write and read different types of data.")]
+ public void MessagesCanWriteAndReadDifferentDataTypes()
+ {
+ using (MessageWriter writer = new MessageWriter(MessageType1))
+ {
+ byte[] testBytes = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21};
+
+ writer.WriteBool(true);
+ writer.WriteBool(false);
+ writer.WriteByte(0x01);
+ writer.WriteInt16(0x0203);
+ writer.WriteInt32(0x04050607);
+ writer.WriteFloat(1.23f);
+ writer.WriteDouble(1.23);
+ writer.WriteString("Hello, world!");
+ writer.WriteBytes(testBytes, 0, testBytes.Length);
+
+ byte[] resultBytes = new byte[testBytes.Length];
+
+ MessageReader reader = writer.ToReader();
+ Assert.True(reader.ReadBool());
+ Assert.False(reader.ReadBool());
+ Assert.Equal(0x01, reader.ReadByte());
+ Assert.Equal(0x0203, reader.ReadInt16());
+ Assert.Equal(0x04050607, reader.ReadInt32());
+ Assert.Equal(1.23f, reader.ReadFloat());
+ Assert.Equal(1.23, reader.ReadDouble());
+ Assert.Equal("Hello, world!", reader.ReadString());
+ reader.ReadBytes(resultBytes, 0, resultBytes.Length);
+
+ for (int i = 0; i < testBytes.Length; i++)
+ {
+ Assert.Equal(testBytes[i], resultBytes[i]);
+ }
+ }
+ }
}
\ No newline at end of file