diff --git a/TcpDotNet/Protocol/ProtocolReader.cs b/TcpDotNet/Protocol/ProtocolReader.cs index 78b51da..810613d 100644 --- a/TcpDotNet/Protocol/ProtocolReader.cs +++ b/TcpDotNet/Protocol/ProtocolReader.cs @@ -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 { } - /// - /// Reads a value from the current stream and advances the current position of the stream by sixteen - /// bytes. - /// - /// A value. - /// The end of the stream is reached. - public Guid ReadGuid() - { - Span buffer = stackalloc byte[16]; - int read = Read(buffer); - if (read != 16) throw new EndOfStreamException(); - return new Guid(buffer); - } - - /// - public override short ReadInt16() - { - return IPAddress.NetworkToHostOrder(base.ReadInt16()); - } - - /// - public override int ReadInt32() - { - return IPAddress.NetworkToHostOrder(base.ReadInt32()); - } - - /// - public override long ReadInt64() - { - return IPAddress.NetworkToHostOrder(base.ReadInt64()); - } - - /// - [CLSCompliant(false)] - public override ushort ReadUInt16() - { - return (ushort) IPAddress.NetworkToHostOrder((short) base.ReadUInt16()); - } - - /// - [CLSCompliant(false)] - public override uint ReadUInt32() - { - return (uint) IPAddress.NetworkToHostOrder((int) base.ReadUInt32()); - } - - /// - [CLSCompliant(false)] - public override ulong ReadUInt64() - { - return (ulong) IPAddress.NetworkToHostOrder((long) base.ReadUInt64()); - } - - /// - /// Reads a value from the current stream and advances the current position of the stream by - /// sixteen bytes. - /// - /// A value read from the current stream. - public Quaternion ReadQuaternion() - { - float x = ReadSingle(); - float y = ReadSingle(); - float z = ReadSingle(); - float w = ReadSingle(); - return new Quaternion(x, y, z, w); - } - /// /// Reads in a 32-bit integer in compressed format. /// @@ -188,6 +122,97 @@ public sealed class ProtocolReader : BinaryReader return (long) result; } + /// + public override double ReadDouble() + { + Span 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; + } + + /// + /// Reads a value from the current stream and advances the current position of the stream by sixteen + /// bytes. + /// + /// A value. + /// The end of the stream is reached. + public Guid ReadGuid() + { + Span buffer = stackalloc byte[16]; + int read = Read(buffer); + if (read != 16) throw new EndOfStreamException(); + return new Guid(buffer); + } + + /// + public override short ReadInt16() + { + return IPAddress.NetworkToHostOrder(base.ReadInt16()); + } + + /// + public override int ReadInt32() + { + return IPAddress.NetworkToHostOrder(base.ReadInt32()); + } + + /// + public override long ReadInt64() + { + return IPAddress.NetworkToHostOrder(base.ReadInt64()); + } + + /// + /// Reads a value from the current stream and advances the current position of the stream by + /// sixteen bytes. + /// + /// A value read from the current stream. + public Quaternion ReadQuaternion() + { + float x = ReadSingle(); + float y = ReadSingle(); + float z = ReadSingle(); + float w = ReadSingle(); + return new Quaternion(x, y, z, w); + } + + /// + public override float ReadSingle() + { + Span 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; + } + + /// + [CLSCompliant(false)] + public override ushort ReadUInt16() + { + return (ushort) IPAddress.NetworkToHostOrder((short) base.ReadUInt16()); + } + + /// + [CLSCompliant(false)] + public override uint ReadUInt32() + { + return (uint) IPAddress.NetworkToHostOrder((int) base.ReadUInt32()); + } + + /// + [CLSCompliant(false)] + public override ulong ReadUInt64() + { + return (ulong) IPAddress.NetworkToHostOrder((long) base.ReadUInt64()); + } + /// /// Reads a value from the current stream and advances the current position of the stream by eight /// bytes. diff --git a/TcpDotNet/Protocol/ProtocolWriter.cs b/TcpDotNet/Protocol/ProtocolWriter.cs index ae55a0e..76280fe 100644 --- a/TcpDotNet/Protocol/ProtocolWriter.cs +++ b/TcpDotNet/Protocol/ProtocolWriter.cs @@ -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 { } + /// + public override void Write(double value) + { + Span buffer = stackalloc byte[8]; + MemoryMarshal.TryWrite(buffer, ref value); + + if (BitConverter.IsLittleEndian) buffer.Reverse(); + Write(buffer); + } + + /// + /// Writes a value to the current stream and advances the stream position by sixteen bytes. + /// + /// The value to write. + public void Write(Guid guid) + { + Span buffer = stackalloc byte[16]; + guid.TryWriteBytes(buffer); + Write(buffer); + } + /// public override void Write(short value) { @@ -51,6 +73,28 @@ public sealed class ProtocolWriter : BinaryWriter base.Write(IPAddress.HostToNetworkOrder(value)); } + /// + /// Writes a value to the current stream and advances the stream position by sixteen bytes. + /// + /// The value to write. + public void Write(Quaternion value) + { + Write(value.X); + Write(value.Y); + Write(value.Z); + Write(value.W); + } + + /// + public override void Write(float value) + { + Span buffer = stackalloc byte[4]; + MemoryMarshal.TryWrite(buffer, ref value); + + if (BitConverter.IsLittleEndian) buffer.Reverse(); + Write(buffer); + } + /// [CLSCompliant(false)] public override void Write(ushort value) @@ -72,29 +116,6 @@ public sealed class ProtocolWriter : BinaryWriter base.Write((ulong) IPAddress.HostToNetworkOrder((long) value)); } - /// - /// Writes a value to the current stream and advances the stream position by sixteen bytes. - /// - /// The value to write. - public void Write(Guid guid) - { - Span buffer = stackalloc byte[16]; - guid.TryWriteBytes(buffer); - Write(buffer); - } - - /// - /// Writes a value to the current stream and advances the stream position by sixteen bytes. - /// - /// The value to write. - public void Write(Quaternion value) - { - Write(value.X); - Write(value.Y); - Write(value.Z); - Write(value.W); - } - /// /// Writes a value to the current stream and advances the stream position by eight bytes. ///