Order methods alphabetically; add Read/Write for double/float

This commit is contained in:
Oliver Booth 2023-01-22 14:41:54 +00:00
parent 553f7235f1
commit 6548da76ad
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 136 additions and 90 deletions

View File

@ -1,5 +1,6 @@
using System.Net;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
namespace TcpDotNet.Protocol;
@ -35,73 +36,6 @@ 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()
{
return IPAddress.NetworkToHostOrder(base.ReadInt16());
}
/// <inheritdoc />
public override int ReadInt32()
{
return IPAddress.NetworkToHostOrder(base.ReadInt32());
}
/// <inheritdoc />
public override long ReadInt64()
{
return IPAddress.NetworkToHostOrder(base.ReadInt64());
}
/// <inheritdoc />
[CLSCompliant(false)]
public override ushort ReadUInt16()
{
return (ushort) IPAddress.NetworkToHostOrder((short) base.ReadUInt16());
}
/// <inheritdoc />
[CLSCompliant(false)]
public override uint ReadUInt32()
{
return (uint) IPAddress.NetworkToHostOrder((int) base.ReadUInt32());
}
/// <inheritdoc />
[CLSCompliant(false)]
public override ulong ReadUInt64()
{
return (ulong) IPAddress.NetworkToHostOrder((long) base.ReadUInt64());
}
/// <summary>
/// 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>
public Quaternion ReadQuaternion()
{
float x = ReadSingle();
float y = ReadSingle();
float z = ReadSingle();
float w = ReadSingle();
return new Quaternion(x, y, z, w);
}
/// <summary>
/// Reads in a 32-bit integer in compressed format.
/// </summary>
@ -188,6 +122,97 @@ public sealed class ProtocolReader : BinaryReader
return (long) result;
}
/// <inheritdoc />
public override double ReadDouble()
{
Span<byte> buffer = stackalloc byte[8];
int read = Read(buffer);
if (read != buffer.Length) throw new EndOfStreamException();
if (BitConverter.IsLittleEndian) buffer.Reverse();
MemoryMarshal.TryRead(buffer, out double value);
return value;
}
/// <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()
{
return IPAddress.NetworkToHostOrder(base.ReadInt16());
}
/// <inheritdoc />
public override int ReadInt32()
{
return IPAddress.NetworkToHostOrder(base.ReadInt32());
}
/// <inheritdoc />
public override long ReadInt64()
{
return IPAddress.NetworkToHostOrder(base.ReadInt64());
}
/// <summary>
/// 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>
public Quaternion ReadQuaternion()
{
float x = ReadSingle();
float y = ReadSingle();
float z = ReadSingle();
float w = ReadSingle();
return new Quaternion(x, y, z, w);
}
/// <inheritdoc />
public override float ReadSingle()
{
Span<byte> buffer = stackalloc byte[4];
int read = Read(buffer);
if (read != buffer.Length) throw new EndOfStreamException();
if (BitConverter.IsLittleEndian) buffer.Reverse();
MemoryMarshal.TryRead(buffer, out float value);
return value;
}
/// <inheritdoc />
[CLSCompliant(false)]
public override ushort ReadUInt16()
{
return (ushort) IPAddress.NetworkToHostOrder((short) base.ReadUInt16());
}
/// <inheritdoc />
[CLSCompliant(false)]
public override uint ReadUInt32()
{
return (uint) IPAddress.NetworkToHostOrder((int) base.ReadUInt32());
}
/// <inheritdoc />
[CLSCompliant(false)]
public override ulong ReadUInt64()
{
return (ulong) IPAddress.NetworkToHostOrder((long) base.ReadUInt64());
}
/// <summary>
/// Reads a <see cref="Vector2" /> value from the current stream and advances the current position of the stream by eight
/// bytes.

View File

@ -1,5 +1,6 @@
using System.Net;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
namespace TcpDotNet.Protocol;
@ -33,6 +34,27 @@ public sealed class ProtocolWriter : BinaryWriter
{
}
/// <inheritdoc />
public override void Write(double value)
{
Span<byte> buffer = stackalloc byte[8];
MemoryMarshal.TryWrite(buffer, ref value);
if (BitConverter.IsLittleEndian) buffer.Reverse();
Write(buffer);
}
/// <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);
}
/// <inheritdoc />
public override void Write(short value)
{
@ -51,6 +73,28 @@ public sealed class ProtocolWriter : BinaryWriter
base.Write(IPAddress.HostToNetworkOrder(value));
}
/// <summary>
/// Writes a <see cref="Quaternion" /> value to the current stream and advances the stream position by sixteen bytes.
/// </summary>
/// <param name="value">The <see cref="Quaternion" /> value to write.</param>
public void Write(Quaternion value)
{
Write(value.X);
Write(value.Y);
Write(value.Z);
Write(value.W);
}
/// <inheritdoc />
public override void Write(float value)
{
Span<byte> buffer = stackalloc byte[4];
MemoryMarshal.TryWrite(buffer, ref value);
if (BitConverter.IsLittleEndian) buffer.Reverse();
Write(buffer);
}
/// <inheritdoc />
[CLSCompliant(false)]
public override void Write(ushort value)
@ -72,29 +116,6 @@ 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>
/// <param name="value">The <see cref="Quaternion" /> value to write.</param>
public void Write(Quaternion value)
{
Write(value.X);
Write(value.Y);
Write(value.Z);
Write(value.W);
}
/// <summary>
/// Writes a <see cref="Vector2" /> value to the current stream and advances the stream position by eight bytes.
/// </summary>