Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include more basic data types for writing and reading messages. #3

Merged
merged 6 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions QuickLink/Messages/MessageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ private void EnsureCanReadLength(int length)
throw new InvalidOperationException("Cannot read beyond the end of the buffer");
}

/// <summary>
/// Reads a boolean from the buffer.
/// </summary>
/// <returns>The boolean read from the buffer</returns>
public bool ReadBool()
{
EnsureCanReadLength(1);
return _buffer[_offset++] == 1;
}

/// <summary>
/// Reads a byte from the buffer.
/// </summary>
/// <returns>The byte read from the buffer</returns>
public byte ReadByte()
{
EnsureCanReadLength(1);
return _buffer[_offset++];
}

/// <summary>
/// Reads a 16-bit signed integer from the buffer.
/// </summary>
Expand All @@ -59,6 +79,30 @@ public int ReadInt32()
return value;
}

/// <summary>
/// Reads a 32-bit signed float from the buffer.
/// </summary>
/// <returns>The float read from the buffer.</returns>
public float ReadFloat()
{
EnsureCanReadLength(4);
float value = BitConverter.ToSingle(_buffer, _offset);
_offset += 4;
return value;
}

/// <summary>
/// Reads a 32-bit signed double from the buffer.
/// </summary>
/// <returns>The double read from the buffer.</returns>
public double ReadDouble()
{
EnsureCanReadLength(8);
double value = BitConverter.ToDouble(_buffer, _offset);
_offset += 8;
return value;
}

/// <summary>
/// Reads a string from the buffer.
/// </summary>
Expand All @@ -72,6 +116,19 @@ public string ReadString()
return value;
}

/// <summary>
/// Writes the specified amount of bytes to the provided buffer.
/// </summary>
/// <param name="buffer">The buffer to write the bytes to.</param>
/// <param name="offset">The offset at which to write the bytes in the output buffer.</param>
/// <param name="length">The amount of bytes to write to the buffer.</param>
public void ReadBytes(byte[] buffer, int offset, int length)
{
EnsureCanReadLength(length);
Array.Copy(_buffer, _offset, buffer, offset, length);
_offset += length;
}

/// <summary>
/// Sets the current position in the buffer to the specified offset.
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions QuickLink/Messages/MessageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ public MessageWriter(MessageType type)
WriteInt16(type.Id);
}

/// <summary>
/// Writes a boolean to the underlying memory stream
/// </summary>
/// <param name="value">The value to write</param>
public void WriteBool(bool value)
{
WriteByte((byte)(value ? 1 : 0));
}

/// <summary>
/// Writes a byte to the underlying memory stream
/// </summary>
/// <param name="value">The value to write</param>
public void WriteByte(byte value)
{
_memoryStream.WriteByte(value);
}

/// <summary>
/// Writes a 16-bit signed integer to the underlying memory stream.
/// </summary>
Expand All @@ -43,6 +61,26 @@ public void WriteInt32(int value)
_memoryStream.Write(buffer, 0, buffer.Length);
}

/// <summary>
/// Writes a 32-bit signed float to the underlying memory stream.
/// </summary>
/// <param name="value">The value to write.</param>
public void WriteFloat(float value)
{
byte[] buffer = BitConverter.GetBytes(value);
_memoryStream.Write(buffer, 0, buffer.Length);
}

/// <summary>
/// Writes a 32-bit signed double to the underlying memory stream.
/// </summary>
/// <param name="value">The value to write.</param>
public void WriteDouble(double value)
{
byte[] buffer = BitConverter.GetBytes(value);
_memoryStream.Write(buffer, 0, buffer.Length);
}

/// <summary>
/// Writes a string to the underlying memory stream.
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions QuickLinkTests/MessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}
}