public static ulong PeekUInt64(this IBitBuffer buffer, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(ulong)]; buffer.PeekBits(tmp, bitCount, tmp.Length * 8); return(BinaryPrimitives.ReadUInt64LittleEndian(tmp)); }
/// <summary> /// Reads the specified number of bits into an <see cref="int"/> without advancing the read position. /// </summary> public static int PeekInt32(this IBitBuffer buffer, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(int)]; buffer.PeekBits(tmp, bitCount, tmp.Length * 8); return(BinaryPrimitives.ReadInt32LittleEndian(tmp)); }
/// <summary> /// Reads the specified number of bits, clamped between one and <paramref name="maxBitCount"/>, into the given buffer. /// </summary> /// <param name="destination">The destination span.</param> /// <param name="bitCount">The number of bits to read.</param> /// <param name="maxBitCount">The maximum amount of bits to read.</param> public static bool TryReadBits(this IBitBuffer buffer, Span <byte> destination, int bitCount, int maxBitCount) { Debug.Assert(bitCount >= 1); Debug.Assert(bitCount <= maxBitCount); return(buffer.TryReadBits(destination, bitCount)); }
public static void SetLengthByPosition(this IBitBuffer buffer) { if (buffer.BitLength < buffer.BitPosition) { buffer.BitLength = buffer.BitPosition; } }
/// <summary> /// Reads bytes into the given span. /// </summary> /// <param name="destination">The destination span.</param> /// <exception cref="EndOfMessageException"></exception> public static void Read(this IBitBuffer buffer, Span <byte> destination) { if (!buffer.TryRead(destination)) { throw new EndOfMessageException(); } }
/// <summary> /// </summary> /// <exception cref="EndOfMessageException"></exception> public static byte[] Read(this IBitBuffer buffer, int count) { var data = new byte[count]; buffer.Read(data); return(data); }
/// <summary> /// Reads all fields with the specified binding of the object in alphabetical order using reflection. /// </summary> public static void ReadAllFields( this IBitBuffer buffer, object target, BindingFlags flags = DefaultBindingFlags) { if (target == null) { throw new ArgumentNullException(nameof(target)); } Type type = target.GetType(); FieldInfo[] fields = type.GetFields(flags); SortMembers(fields); var readParams = new[] { buffer }; foreach (FieldInfo fi in fields) { // find read method MethodInfo?readMethod; if (fi.FieldType.IsEnum) { readMethod = EnumReadMethod; } else if (!ReadMethods.TryGetValue(fi.FieldType, out readMethod)) { throw new LidgrenException("Failed to find read method for type " + fi.FieldType); } // read and set value var value = readMethod.Invoke(null, readParams); fi.SetValue(target, value); } }
/// <summary> /// Reads a 32-bit <see cref="int"/> written by <see cref="Write(int)"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static int ReadInt32(this IBitBuffer buffer) { Span <byte> tmp = stackalloc byte[sizeof(int)]; buffer.Read(tmp); return(BinaryPrimitives.ReadInt32LittleEndian(tmp)); }
public static void EnsureEnoughBitCapacity(this IBitBuffer buffer, int bitCount, int maxBitCount) { Debug.Assert(bitCount >= 1); Debug.Assert(bitCount <= maxBitCount); buffer.EnsureBitCapacity(buffer.BitLength + bitCount); }
/// <summary> /// Reads a <see cref="long"/> stored in 1 to 64 bits, written by <see cref="Write(long, int)"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static long ReadInt64(this IBitBuffer buffer, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(long)]; if (bitCount == tmp.Length * 8) { buffer.Read(tmp); return(BinaryPrimitives.ReadInt64LittleEndian(tmp)); } tmp.Clear(); buffer.ReadBits(tmp, bitCount, tmp.Length * 8); long value = BinaryPrimitives.ReadInt64LittleEndian(tmp); long signBit = 1 << (bitCount - 1); if ((value & signBit) == 0) { return(value); // positive } // negative unchecked { ulong mask = ((ulong)-1) >> (65 - bitCount); ulong nValue = ((ulong)value & mask) + 1; return(-(long)nValue); } }
/// <summary> /// Reads the specified number of bits into the given buffer. /// </summary> /// <param name="destination">The destination span.</param> /// <param name="bitCount">The number of bits to read.</param> /// <exception cref="EndOfMessageException"></exception> public static void ReadBits(this IBitBuffer buffer, Span <byte> destination, int bitCount) { if (!buffer.TryReadBits(destination, bitCount)) { throw new EndOfMessageException(); } }
public static ulong ReadUInt64(this IBitBuffer buffer) { Span <byte> tmp = stackalloc byte[sizeof(ulong)]; buffer.Read(tmp); return(BinaryPrimitives.ReadUInt64LittleEndian(tmp)); }
public static void Write(this IBitBuffer buffer, uint value) { Span <byte> tmp = stackalloc byte[sizeof(uint)]; BinaryPrimitives.WriteUInt32LittleEndian(tmp, value); buffer.Write(tmp); }
/// <summary> /// Writes a 64-bit <see cref="long"/>. /// </summary> public static void Write(this IBitBuffer buffer, long value) { Span <byte> tmp = stackalloc byte[sizeof(long)]; BinaryPrimitives.WriteInt64LittleEndian(tmp, value); buffer.Write(tmp); }
/// <summary> /// Writes all fields with specified binding in alphabetical order using reflection. /// </summary> public static void WriteAllFields( this IBitBuffer buffer, object?source, BindingFlags flags = DefaultBindingFlags) { if (source == null) { return; } Type type = source.GetType(); FieldInfo[] fields = type.GetFields(flags); SortMembers(fields); foreach (FieldInfo field in fields) { // find the appropriate Write method MethodInfo?writeMethod; if (field.FieldType.IsEnum) { writeMethod = EnumWriteMethod; } else if (!WriteMethods.TryGetValue(field.FieldType, out writeMethod)) { throw new LidgrenException("Failed to find write method for type " + field.FieldType); } // get and write value var value = field.GetValue(source); writeMethod.Invoke(null, new[] { buffer, value }); } }
public static ushort ReadUInt16(this IBitBuffer buffer) { Span <byte> tmp = stackalloc byte[sizeof(ushort)]; buffer.Read(tmp); return(BinaryPrimitives.ReadUInt16LittleEndian(tmp)); }
public static void Write(this IBitBuffer buffer, ulong value, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(ulong)]; BinaryPrimitives.WriteUInt64LittleEndian(tmp, value); buffer.Write(tmp, bitCount); }
/// <summary> /// Reads the specified number of bits, clamped between one and <paramref name="maxBitCount"/>, into the given buffer. /// </summary> /// <param name="destination">The destination span.</param> /// <param name="bitCount">The number of bits to read.</param> /// <param name="maxBitCount">The maximum amount of bits to read.</param> /// <exception cref="EndOfMessageException"></exception> public static void ReadBits(this IBitBuffer buffer, Span <byte> destination, int bitCount, int maxBitCount) { Debug.Assert(bitCount >= 1); Debug.Assert(bitCount <= maxBitCount); buffer.ReadBits(destination, bitCount); }
/// <summary> /// Reads a <see cref="int"/> stored in 1 to 32 bits, written by <see cref="Write(int, int)"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static int ReadInt32(this IBitBuffer buffer, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(int)]; if (bitCount == tmp.Length * 8) { buffer.Read(tmp); return(BinaryPrimitives.ReadInt32LittleEndian(tmp)); } tmp.Clear(); buffer.ReadBits(tmp, bitCount, tmp.Length * 8); int value = BinaryPrimitives.ReadInt32LittleEndian(tmp); int signBit = 1 << (bitCount - 1); if ((value & signBit) == 0) { return(value); // positive } // negative unchecked { uint mask = ((uint)-1) >> (33 - bitCount); uint nValue = ((uint)value & mask) + 1; return(-(int)nValue); } }
/// <summary> /// Reads a 32-bit <see cref="int"/> written by <see cref="WriteRanged"/>. /// </summary> /// <param name="min">The minimum value used when writing the value</param> /// <param name="max">The maximum value used when writing the value</param> /// <returns>A signed integer value larger or equal to MIN and smaller or equal to MAX</returns> /// <exception cref="EndOfMessageException"></exception> public static int ReadRangedInt32(this IBitBuffer buffer, int min, int max) { uint range = (uint)(max - min); int numBits = NetBitWriter.BitsForValue(range); uint rvalue = buffer.ReadUInt32(numBits); return((int)(min + rvalue)); }
public static sbyte ReadSByte(this IBitBuffer buffer) { if (!buffer.ReadSByte(out sbyte value)) { throw new EndOfMessageException(); } return(value); }
/// <summary> /// Reads 1 to 8 bits into a <see cref="byte"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static byte ReadByte(this IBitBuffer buffer, int bitCount) { if (!buffer.ReadByte(bitCount, out byte value)) { throw new EndOfMessageException(); } return(value); }
/// <summary> /// Reads an 8-bit <see cref="bool"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static bool ReadBool(this IBitBuffer buffer) { if (!buffer.ReadBool(out bool result)) { throw new EndOfMessageException(); } return(result); }
public static uint ReadUInt32(this IBitBuffer buffer, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(uint)]; tmp.Clear(); buffer.ReadBits(tmp, bitCount, tmp.Length * 8); return(BinaryPrimitives.ReadUInt32LittleEndian(tmp)); }
/// <summary> /// Reads the bytes of a <typeparamref name="T"/> value from the buffer. /// </summary> public static bool TryRead <T>(this IBitBuffer buffer, out T value) where T : unmanaged { Unsafe.SkipInit(out value); Span <T> span = MemoryMarshal.CreateSpan(ref value, 1); Span <byte> bytes = MemoryMarshal.AsBytes(span); return(buffer.TryRead(bytes)); }
public static void IncrementBitPosition(this IBitBuffer buffer, int bitCount) { buffer.BitPosition += bitCount; if (buffer.BitLength < buffer.BitPosition) { buffer.BitLength = buffer.BitPosition; } }
/// <summary> /// Reads the specified number of bits into a <see cref="byte"/> without advancing the read position. /// </summary> public static byte PeekByte(this IBitBuffer buffer, int bitCount) { if (!buffer.HasEnoughBits(bitCount)) { throw new EndOfMessageException(); } return(NetBitWriter.ReadByte(buffer.GetBuffer(), buffer.BitPosition, bitCount)); }
/// <summary> /// Reads the bytes of a <typeparamref name="T"/> value from the buffer. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static T Read <T>(this IBitBuffer buffer) where T : unmanaged { if (!buffer.TryRead(out T value)) { throw new EndOfMessageException(); } return(value); }
public static sbyte PeekSByte(this IBitBuffer buffer) { if (!buffer.HasEnoughBits(8)) { throw new EndOfMessageException(); } return((sbyte)NetBitWriter.ReadByte(buffer.GetBuffer(), buffer.BitPosition, 8)); }
/// <summary> /// Reads a 1-bit <see cref="bool"/> without advancing the read position. /// </summary> public static bool PeekBool(this IBitBuffer buffer) { if (!buffer.HasEnoughBits(1)) { throw new EndOfMessageException(); } return(NetBitWriter.ReadByte(buffer.GetBuffer(), buffer.BitPosition, 1) > 0); }