Add support for reading/writing System.Guid

This commit is contained in:
Oliver Booth 2022-05-19 10:21:00 +01:00
parent 96ca5d778b
commit a87e186c58
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 27 additions and 2 deletions

View File

@ -35,6 +35,20 @@ public sealed class ProtocolReader : BinaryReader
{
}
/// <summary>
/// Reads a <see cref="Guid" /> value from the current stream and advances the current position of the stream by sixteen
/// bytes.
/// </summary>
/// <returns>A <see cref="Guid" /> value.</returns>
/// <exception cref="EndOfStreamException">The end of the stream is reached.</exception>
public Guid ReadGuid()
{
Span<byte> buffer = stackalloc byte[16];
int read = Read(buffer);
if (read != 16) throw new EndOfStreamException();
return new Guid(buffer);
}
/// <inheritdoc />
public override short ReadInt16()
{
@ -75,7 +89,7 @@ public sealed class ProtocolReader : BinaryReader
}
/// <summary>
/// Reads a <see cref="Quaternion" /> value from the current stream and advances the current position of the stream by
/// Reads a <see cref="Quaternion" /> value from the current stream and advances the current position of the stream by
/// sixteen bytes.
/// </summary>
/// <returns>A <see cref="Quaternion" /> value read from the current stream.</returns>

View File

@ -55,7 +55,7 @@ public sealed class ProtocolWriter : BinaryWriter
[CLSCompliant(false)]
public override void Write(ushort value)
{
base.Write((ushort)IPAddress.HostToNetworkOrder((short)value));
base.Write((ushort) IPAddress.HostToNetworkOrder((short) value));
}
/// <inheritdoc />
@ -72,6 +72,17 @@ public sealed class ProtocolWriter : BinaryWriter
base.Write((ulong) IPAddress.HostToNetworkOrder((long) value));
}
/// <summary>
/// Writes a <see cref="Guid" /> value to the current stream and advances the stream position by sixteen bytes.
/// </summary>
/// <param name="guid">The <see cref="Guid" /> value to write.</param>
public void Write(Guid guid)
{
Span<byte> buffer = stackalloc byte[16];
guid.TryWriteBytes(buffer);
Write(buffer);
}
/// <summary>
/// Writes a <see cref="Quaternion" /> value to the current stream and advances the stream position by sixteen bytes.
/// </summary>