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 /> /// <inheritdoc />
public override short ReadInt16() public override short ReadInt16()
{ {

View File

@ -72,6 +72,17 @@ public sealed class ProtocolWriter : BinaryWriter
base.Write((ulong) IPAddress.HostToNetworkOrder((long) value)); 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> /// <summary>
/// Writes a <see cref="Quaternion" /> value to the current stream and advances the stream position by sixteen bytes. /// Writes a <see cref="Quaternion" /> value to the current stream and advances the stream position by sixteen bytes.
/// </summary> /// </summary>