diff --git a/Lidgren.Network/NetIncomingMessage.Peek.cs b/Lidgren.Network/NetIncomingMessage.Peek.cs
deleted file mode 100644
index bb0e43c..0000000
--- a/Lidgren.Network/NetIncomingMessage.Peek.cs
+++ /dev/null
@@ -1,314 +0,0 @@
-/* Copyright (c) 2010 Michael Lidgren
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software
-and associated documentation files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
-the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or
-substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-*/
-using System;
-using System.Diagnostics;
-using System.Net;
-
-namespace Lidgren.Network
-{
- public partial class NetIncomingMessage
- {
- ///
- /// Returns the internal data buffer, don't modify
- ///
- public byte[] PeekDataBuffer()
- {
- return m_data;
- }
-
- //
- // 1 bit
- //
- ///
- /// Reads a 1-bit Boolean without advancing the read pointer
- ///
- public bool PeekBoolean()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 1, c_readOverflowError);
- byte retval = NetBitWriter.ReadByte(m_data, 1, m_readPosition);
- return (retval > 0 ? true : false);
- }
-
- //
- // 8 bit
- //
- ///
- /// Reads a Byte without advancing the read pointer
- ///
- public byte PeekByte()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 8, c_readOverflowError);
- byte retval = NetBitWriter.ReadByte(m_data, 8, m_readPosition);
- return retval;
- }
-
- ///
- /// Reads an SByte without advancing the read pointer
- ///
- [CLSCompliant(false)]
- public sbyte PeekSByte()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 8, c_readOverflowError);
- byte retval = NetBitWriter.ReadByte(m_data, 8, m_readPosition);
- return (sbyte)retval;
- }
-
- ///
- /// Reads the specified number of bits into a Byte without advancing the read pointer
- ///
- public byte PeekByte(int numberOfBits)
- {
- byte retval = NetBitWriter.ReadByte(m_data, numberOfBits, m_readPosition);
- return retval;
- }
-
- ///
- /// Reads the specified number of bytes without advancing the read pointer
- ///
- public byte[] PeekBytes(int numberOfBytes)
- {
- NetException.Assert(m_bitLength - m_readPosition >= (numberOfBytes * 8), c_readOverflowError);
-
- byte[] retval = new byte[numberOfBytes];
- NetBitWriter.ReadBytes(m_data, numberOfBytes, m_readPosition, retval, 0);
- return retval;
- }
-
- ///
- /// Reads the specified number of bytes without advancing the read pointer
- ///
- public void PeekBytes(byte[] into, int offset, int numberOfBytes)
- {
- NetException.Assert(m_bitLength - m_readPosition >= (numberOfBytes * 8), c_readOverflowError);
- NetException.Assert(offset + numberOfBytes <= into.Length);
-
- NetBitWriter.ReadBytes(m_data, numberOfBytes, m_readPosition, into, offset);
- return;
- }
-
- //
- // 16 bit
- //
- ///
- /// Reads an Int16 without advancing the read pointer
- ///
- public Int16 PeekInt16()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
- uint retval = NetBitWriter.ReadUInt32(m_data, 16, m_readPosition);
- return (short)retval;
- }
-
- ///
- /// Reads a UInt16 without advancing the read pointer
- ///
- [CLSCompliant(false)]
- public UInt16 PeekUInt16()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
- uint retval = NetBitWriter.ReadUInt32(m_data, 16, m_readPosition);
- return (ushort)retval;
- }
-
- //
- // 32 bit
- //
- ///
- /// Reads an Int32 without advancing the read pointer
- ///
- public Int32 PeekInt32()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 32, c_readOverflowError);
- uint retval = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- return (Int32)retval;
- }
-
- ///
- /// Reads the specified number of bits into an Int32 without advancing the read pointer
- ///
- public Int32 PeekInt32(int numberOfBits)
- {
- NetException.Assert((numberOfBits > 0 && numberOfBits <= 32), "ReadInt() can only read between 1 and 32 bits");
- NetException.Assert(m_bitLength - m_readPosition >= numberOfBits, c_readOverflowError);
-
- uint retval = NetBitWriter.ReadUInt32(m_data, numberOfBits, m_readPosition);
-
- if (numberOfBits == 32)
- return (int)retval;
-
- int signBit = 1 << (numberOfBits - 1);
- if ((retval & signBit) == 0)
- return (int)retval; // positive
-
- // negative
- unchecked
- {
- uint mask = ((uint)-1) >> (33 - numberOfBits);
- uint tmp = (retval & mask) + 1;
- return -((int)tmp);
- }
- }
-
- ///
- /// Reads a UInt32 without advancing the read pointer
- ///
- [CLSCompliant(false)]
- public UInt32 PeekUInt32()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 32, c_readOverflowError);
- uint retval = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- return retval;
- }
-
- ///
- /// Reads the specified number of bits into a UInt32 without advancing the read pointer
- ///
- [CLSCompliant(false)]
- public UInt32 PeekUInt32(int numberOfBits)
- {
- NetException.Assert((numberOfBits > 0 && numberOfBits <= 32), "ReadUInt() can only read between 1 and 32 bits");
- //NetException.Assert(m_bitLength - m_readBitPtr >= numberOfBits, "tried to read past buffer size");
-
- UInt32 retval = NetBitWriter.ReadUInt32(m_data, numberOfBits, m_readPosition);
- return retval;
- }
-
- //
- // 64 bit
- //
- ///
- /// Reads a UInt64 without advancing the read pointer
- ///
- [CLSCompliant(false)]
- public UInt64 PeekUInt64()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 64, c_readOverflowError);
-
- ulong low = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- ulong high = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition + 32);
-
- ulong retval = low + (high << 32);
-
- return retval;
- }
-
- ///
- /// Reads an Int64 without advancing the read pointer
- ///
- public Int64 PeekInt64()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 64, c_readOverflowError);
- unchecked
- {
- ulong retval = PeekUInt64();
- long longRetval = (long)retval;
- return longRetval;
- }
- }
-
- ///
- /// Reads the specified number of bits into an UInt64 without advancing the read pointer
- ///
- [CLSCompliant(false)]
- public UInt64 PeekUInt64(int numberOfBits)
- {
- NetException.Assert((numberOfBits > 0 && numberOfBits <= 64), "ReadUInt() can only read between 1 and 64 bits");
- NetException.Assert(m_bitLength - m_readPosition >= numberOfBits, c_readOverflowError);
-
- ulong retval;
- if (numberOfBits <= 32)
- {
- retval = (ulong)NetBitWriter.ReadUInt32(m_data, numberOfBits, m_readPosition);
- }
- else
- {
- retval = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- retval |= NetBitWriter.ReadUInt32(m_data, numberOfBits - 32, m_readPosition) << 32;
- }
- return retval;
- }
-
- ///
- /// Reads the specified number of bits into an Int64 without advancing the read pointer
- ///
- public Int64 PeekInt64(int numberOfBits)
- {
- NetException.Assert(((numberOfBits > 0) && (numberOfBits < 65)), "ReadInt64(bits) can only read between 1 and 64 bits");
- return (long)PeekUInt64(numberOfBits);
- }
-
- //
- // Floating point
- //
- ///
- /// Reads a 32-bit Single without advancing the read pointer
- ///
- public float PeekFloat()
- {
- return PeekSingle();
- }
-
- ///
- /// Reads a 32-bit Single without advancing the read pointer
- ///
- public float PeekSingle()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 32, c_readOverflowError);
-
- if ((m_readPosition & 7) == 0) // read directly
- {
- float retval = BitConverter.ToSingle(m_data, m_readPosition >> 3);
- return retval;
- }
-
- byte[] bytes = PeekBytes(4);
- return BitConverter.ToSingle(bytes, 0);
- }
-
- ///
- /// Reads a 64-bit Double without advancing the read pointer
- ///
- public double PeekDouble()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 64, c_readOverflowError);
-
- if ((m_readPosition & 7) == 0) // read directly
- {
- // read directly
- double retval = BitConverter.ToDouble(m_data, m_readPosition >> 3);
- return retval;
- }
-
- byte[] bytes = PeekBytes(8);
- return BitConverter.ToDouble(bytes, 0);
- }
-
- ///
- /// Reads a string without advancing the read pointer
- ///
- public string PeekString()
- {
- int wasReadPosition = m_readPosition;
- string retval = ReadString();
- m_readPosition = wasReadPosition;
- return retval;
- }
- }
-}
diff --git a/Lidgren.Network/NetIncomingMessage.Read.Reflection.cs b/Lidgren.Network/NetIncomingMessage.Read.Reflection.cs
deleted file mode 100644
index e5b44bc..0000000
--- a/Lidgren.Network/NetIncomingMessage.Read.Reflection.cs
+++ /dev/null
@@ -1,103 +0,0 @@
-/* Copyright (c) 2010 Michael Lidgren
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software
-and associated documentation files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
-the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or
-substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-USE OR OTHER DEALINGS IN THE SOFTWARE.
-*/
-using System;
-using System.Reflection;
-
-namespace Lidgren.Network
-{
- public partial class NetIncomingMessage
- {
- ///
- /// Reads all public and private declared instance fields of the object in alphabetical order using reflection
- ///
- public void ReadAllFields(object target)
- {
- ReadAllFields(target, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
- }
-
- ///
- /// Reads all fields with the specified binding of the object in alphabetical order using reflection
- ///
- public void ReadAllFields(object target, BindingFlags flags)
- {
- if (target == null)
- return;
- Type tp = target.GetType();
-
- FieldInfo[] fields = tp.GetFields(flags);
- NetUtility.SortMembersList(fields);
-
- foreach (FieldInfo fi in fields)
- {
- object value;
-
- // find read method
- MethodInfo readMethod;
- if (s_readMethods.TryGetValue(fi.FieldType, out readMethod))
- {
- // read value
- value = readMethod.Invoke(this, null);
-
- // set the value
- fi.SetValue(target, value);
- }
- }
- }
-
- ///
- /// Reads all public and private declared instance fields of the object in alphabetical order using reflection
- ///
- public void ReadAllProperties(object target)
- {
- ReadAllProperties(target, BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
- }
-
- ///
- /// Reads all fields with the specified binding of the object in alphabetical order using reflection
- ///
- public void ReadAllProperties(object target, BindingFlags flags)
- {
- if (target == null)
- throw new ArgumentNullException("target");
-
- if (target == null)
- return;
- Type tp = target.GetType();
-
- PropertyInfo[] fields = tp.GetProperties(flags);
- NetUtility.SortMembersList(fields);
- foreach (PropertyInfo fi in fields)
- {
- object value;
-
- // find read method
- MethodInfo readMethod;
- if (s_readMethods.TryGetValue(fi.PropertyType, out readMethod))
- {
- // read value
- value = readMethod.Invoke(this, null);
-
- // set the value
- MethodInfo setMethod = fi.GetSetMethod((flags & BindingFlags.NonPublic) == BindingFlags.NonPublic);
- setMethod.Invoke(target, new object[] { value });
- }
- }
- }
- }
-}
diff --git a/Lidgren.Network/NetIncomingMessage.Read.cs b/Lidgren.Network/NetIncomingMessage.Read.cs
deleted file mode 100644
index 838798f..0000000
--- a/Lidgren.Network/NetIncomingMessage.Read.cs
+++ /dev/null
@@ -1,714 +0,0 @@
-/* Copyright (c) 2010 Michael Lidgren
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software
-and associated documentation files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
-the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or
-substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-*/
-using System;
-using System.Collections.Generic;
-using System.Net;
-using System.Reflection;
-
-namespace Lidgren.Network
-{
- public partial class NetIncomingMessage
- {
- private const string c_readOverflowError = "Trying to read past the buffer size - likely caused by mismatching Write/Reads, different size or order.";
-
- private static readonly Dictionary s_readMethods;
-
- internal int m_readPosition;
-
- ///
- /// Gets or sets the read position in the buffer, in bits (not bytes)
- ///
- public long Position
- {
- get { return (long)m_readPosition; }
- set { m_readPosition = (int)value; }
- }
-
- ///
- /// Gets the position in the buffer in bytes; note that the bits of the first returned byte may already have been read - check the Position property to make sure.
- ///
- public int PositionInBytes
- {
- get { return (int)(m_readPosition / 8); }
- }
-
- static NetIncomingMessage()
- {
- Type[] integralTypes = typeof(Byte).Assembly.GetTypes();
-
- s_readMethods = new Dictionary();
- MethodInfo[] methods = typeof(NetIncomingMessage).GetMethods(BindingFlags.Instance | BindingFlags.Public);
- foreach (MethodInfo mi in methods)
- {
- if (mi.GetParameters().Length == 0 && mi.Name.StartsWith("Read", StringComparison.InvariantCulture))
- {
- string n = mi.Name.Substring(4);
- foreach (Type it in integralTypes)
- {
- if (it.Name == n)
- s_readMethods[it] = mi;
- }
- }
- }
- }
-
- ///
- /// Reads a boolean value (stored as a single bit) written using Write(bool)
- ///
- public bool ReadBoolean()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 1, c_readOverflowError);
- byte retval = NetBitWriter.ReadByte(m_data, 1, m_readPosition);
- m_readPosition += 1;
- return (retval > 0 ? true : false);
- }
-
- ///
- /// Reads a byte
- ///
- public byte ReadByte()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 8, c_readOverflowError);
- byte retval = NetBitWriter.ReadByte(m_data, 8, m_readPosition);
- m_readPosition += 8;
- return retval;
- }
-
- ///
- /// Reads a byte and returns true or false for success
- ///
- public bool ReadByte(out byte result)
- {
- if (m_bitLength - m_readPosition < 8)
- {
- result = 0;
- return false;
- }
- result = NetBitWriter.ReadByte(m_data, 8, m_readPosition);
- m_readPosition += 8;
- return true;
- }
-
- ///
- /// Reads a signed byte
- ///
- [CLSCompliant(false)]
- public sbyte ReadSByte()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 8, c_readOverflowError);
- byte retval = NetBitWriter.ReadByte(m_data, 8, m_readPosition);
- m_readPosition += 8;
- return (sbyte)retval;
- }
-
- ///
- /// Reads 1 to 8 bits into a byte
- ///
- public byte ReadByte(int numberOfBits)
- {
- NetException.Assert(numberOfBits > 0 && numberOfBits <= 8, "ReadByte(bits) can only read between 1 and 8 bits");
- byte retval = NetBitWriter.ReadByte(m_data, numberOfBits, m_readPosition);
- m_readPosition += numberOfBits;
- return retval;
- }
-
- ///
- /// Reads the specified number of bytes
- ///
- public byte[] ReadBytes(int numberOfBytes)
- {
- NetException.Assert(m_bitLength - m_readPosition + 7 >= (numberOfBytes * 8), c_readOverflowError);
-
- byte[] retval = new byte[numberOfBytes];
- NetBitWriter.ReadBytes(m_data, numberOfBytes, m_readPosition, retval, 0);
- m_readPosition += (8 * numberOfBytes);
- return retval;
- }
-
- ///
- /// Reads the specified number of bytes and returns true for success
- ///
- public bool ReadBytes(int numberOfBytes, out byte[] result)
- {
- if (m_bitLength - m_readPosition + 7 < (numberOfBytes * 8))
- {
- result = null;
- return false;
- }
-
- result = new byte[numberOfBytes];
- NetBitWriter.ReadBytes(m_data, numberOfBytes, m_readPosition, result, 0);
- m_readPosition += (8 * numberOfBytes);
- return true;
- }
-
- ///
- /// Reads the specified number of bytes into a preallocated array
- ///
- /// The destination array
- /// The offset where to start writing in the destination array
- /// The number of bytes to read
- public void ReadBytes(byte[] into, int offset, int numberOfBytes)
- {
- NetException.Assert(m_bitLength - m_readPosition + 7 >= (numberOfBytes * 8), c_readOverflowError);
- NetException.Assert(offset + numberOfBytes <= into.Length);
-
- NetBitWriter.ReadBytes(m_data, numberOfBytes, m_readPosition, into, offset);
- m_readPosition += (8 * numberOfBytes);
- return;
- }
-
- ///
- /// Reads the specified number of bits into a preallocated array
- ///
- /// The destination array
- /// The offset where to start writing in the destination array
- /// The number of bits to read
- public void ReadBits(byte[] into, int offset, int numberOfBits)
- {
- NetException.Assert(m_bitLength - m_readPosition >= numberOfBits, c_readOverflowError);
- NetException.Assert(offset + NetUtility.BytesToHoldBits(numberOfBits) <= into.Length);
-
- int numberOfWholeBytes = numberOfBits / 8;
- int extraBits = numberOfBits - (numberOfWholeBytes * 8);
-
- NetBitWriter.ReadBytes(m_data, numberOfWholeBytes, m_readPosition, into, offset);
- m_readPosition += (8 * numberOfWholeBytes);
-
- if (extraBits > 0)
- into[offset + numberOfWholeBytes] = ReadByte(extraBits);
-
- return;
- }
-
- ///
- /// Reads a 16 bit signed integer written using Write(Int16)
- ///
- public Int16 ReadInt16()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
- uint retval = NetBitWriter.ReadUInt32(m_data, 16, m_readPosition);
- m_readPosition += 16;
- return (short)retval;
- }
-
- ///
- /// Reads a 16 bit unsigned integer written using Write(UInt16)
- ///
- [CLSCompliant(false)]
- public UInt16 ReadUInt16()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 16, c_readOverflowError);
- uint retval = NetBitWriter.ReadUInt32(m_data, 16, m_readPosition);
- m_readPosition += 16;
- return (ushort)retval;
- }
-
- ///
- /// Reads a 32 bit signed integer written using Write(Int32)
- ///
- public Int32 ReadInt32()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 32, c_readOverflowError);
- uint retval = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- m_readPosition += 32;
- return (Int32)retval;
- }
-
- ///
- /// Reads a 32 bit signed integer written using Write(Int32)
- ///
- [CLSCompliant(false)]
- public bool ReadInt32(out Int32 result)
- {
- if (m_bitLength - m_readPosition < 32)
- {
- result = 0;
- return false;
- }
-
- result = (Int32)NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- m_readPosition += 32;
- return true;
- }
-
- ///
- /// Reads a signed integer stored in 1 to 32 bits, written using Write(Int32, Int32)
- ///
- public Int32 ReadInt32(int numberOfBits)
- {
- NetException.Assert(numberOfBits > 0 && numberOfBits <= 32, "ReadInt32(bits) can only read between 1 and 32 bits");
- NetException.Assert(m_bitLength - m_readPosition >= numberOfBits, c_readOverflowError);
-
- uint retval = NetBitWriter.ReadUInt32(m_data, numberOfBits, m_readPosition);
- m_readPosition += numberOfBits;
-
- if (numberOfBits == 32)
- return (int)retval;
-
- int signBit = 1 << (numberOfBits - 1);
- if ((retval & signBit) == 0)
- return (int)retval; // positive
-
- // negative
- unchecked
- {
- uint mask = ((uint)-1) >> (33 - numberOfBits);
- uint tmp = (retval & mask) + 1;
- return -((int)tmp);
- }
- }
-
- ///
- /// Reads an 32 bit unsigned integer written using Write(UInt32)
- ///
- [CLSCompliant(false)]
- public UInt32 ReadUInt32()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 32, c_readOverflowError);
- uint retval = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- m_readPosition += 32;
- return retval;
- }
-
- ///
- /// Reads an 32 bit unsigned integer written using Write(UInt32) and returns true for success
- ///
- [CLSCompliant(false)]
- public bool ReadUInt32(out UInt32 result)
- {
- if (m_bitLength - m_readPosition < 32)
- {
- result = 0;
- return false;
- }
- result = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- m_readPosition += 32;
- return true;
- }
-
- ///
- /// Reads an unsigned integer stored in 1 to 32 bits, written using Write(UInt32, Int32)
- ///
- [CLSCompliant(false)]
- public UInt32 ReadUInt32(int numberOfBits)
- {
- NetException.Assert(numberOfBits > 0 && numberOfBits <= 32, "ReadUInt32(bits) can only read between 1 and 32 bits");
- //NetException.Assert(m_bitLength - m_readBitPtr >= numberOfBits, "tried to read past buffer size");
-
- UInt32 retval = NetBitWriter.ReadUInt32(m_data, numberOfBits, m_readPosition);
- m_readPosition += numberOfBits;
- return retval;
- }
-
- ///
- /// Reads a 64 bit unsigned integer written using Write(UInt64)
- ///
- [CLSCompliant(false)]
- public UInt64 ReadUInt64()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 64, c_readOverflowError);
-
- ulong low = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- m_readPosition += 32;
- ulong high = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
-
- ulong retval = low + (high << 32);
-
- m_readPosition += 32;
- return retval;
- }
-
- ///
- /// Reads a 64 bit signed integer written using Write(Int64)
- ///
- public Int64 ReadInt64()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 64, c_readOverflowError);
- unchecked
- {
- ulong retval = ReadUInt64();
- long longRetval = (long)retval;
- return longRetval;
- }
- }
-
- ///
- /// Reads an unsigned integer stored in 1 to 64 bits, written using Write(UInt64, Int32)
- ///
- [CLSCompliant(false)]
- public UInt64 ReadUInt64(int numberOfBits)
- {
- NetException.Assert(numberOfBits > 0 && numberOfBits <= 64, "ReadUInt64(bits) can only read between 1 and 64 bits");
- NetException.Assert(m_bitLength - m_readPosition >= numberOfBits, c_readOverflowError);
-
- ulong retval;
- if (numberOfBits <= 32)
- {
- retval = (ulong)NetBitWriter.ReadUInt32(m_data, numberOfBits, m_readPosition);
- }
- else
- {
- retval = NetBitWriter.ReadUInt32(m_data, 32, m_readPosition);
- retval |= NetBitWriter.ReadUInt32(m_data, numberOfBits - 32, m_readPosition) << 32;
- }
- m_readPosition += numberOfBits;
- return retval;
- }
-
- ///
- /// Reads a signed integer stored in 1 to 64 bits, written using Write(Int64, Int32)
- ///
- public Int64 ReadInt64(int numberOfBits)
- {
- NetException.Assert(((numberOfBits > 0) && (numberOfBits <= 64)), "ReadInt64(bits) can only read between 1 and 64 bits");
- return (long)ReadUInt64(numberOfBits);
- }
-
- ///
- /// Reads a 32 bit floating point value written using Write(Single)
- ///
- public float ReadFloat()
- {
- return ReadSingle();
- }
-
- ///
- /// Reads a 32 bit floating point value written using Write(Single)
- ///
- public float ReadSingle()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 32, c_readOverflowError);
-
- if ((m_readPosition & 7) == 0) // read directly
- {
- float retval = BitConverter.ToSingle(m_data, m_readPosition >> 3);
- m_readPosition += 32;
- return retval;
- }
-
- byte[] bytes = ReadBytes(4);
- return BitConverter.ToSingle(bytes, 0);
- }
-
- ///
- /// Reads a 32 bit floating point value written using Write(Single)
- ///
- public bool ReadSingle(out float result)
- {
- if (m_bitLength - m_readPosition < 32)
- {
- result = 0.0f;
- return false;
- }
-
- if ((m_readPosition & 7) == 0) // read directly
- {
- result = BitConverter.ToSingle(m_data, m_readPosition >> 3);
- m_readPosition += 32;
- return true;
- }
-
- byte[] bytes = ReadBytes(4);
- result = BitConverter.ToSingle(bytes, 0);
- return true;
- }
-
- ///
- /// Reads a 64 bit floating point value written using Write(Double)
- ///
- public double ReadDouble()
- {
- NetException.Assert(m_bitLength - m_readPosition >= 64, c_readOverflowError);
-
- if ((m_readPosition & 7) == 0) // read directly
- {
- // read directly
- double retval = BitConverter.ToDouble(m_data, m_readPosition >> 3);
- m_readPosition += 64;
- return retval;
- }
-
- byte[] bytes = ReadBytes(8);
- return BitConverter.ToDouble(bytes, 0);
- }
-
- //
- // Variable bit count
- //
-
- ///
- /// Reads a variable sized UInt32 written using WriteVariableUInt32()
- ///
- [CLSCompliant(false)]
- public uint ReadVariableUInt32()
- {
- int num1 = 0;
- int num2 = 0;
- while (true)
- {
- byte num3 = this.ReadByte();
- num1 |= (num3 & 0x7f) << num2;
- num2 += 7;
- if ((num3 & 0x80) == 0)
- return (uint)num1;
- }
- }
-
- ///
- /// Reads a variable sized UInt32 written using WriteVariableUInt32() and returns true for success
- ///
- [CLSCompliant(false)]
- public bool ReadVariableUInt32(out uint result)
- {
- int num1 = 0;
- int num2 = 0;
- while (true)
- {
- byte num3;
- if (ReadByte(out num3) == false)
- {
- result = 0;
- return false;
- }
- num1 |= (num3 & 0x7f) << num2;
- num2 += 7;
- if ((num3 & 0x80) == 0)
- {
- result = (uint)num1;
- return true;
- }
- }
- }
-
- ///
- /// Reads a variable sized Int32 written using WriteVariableInt32()
- ///
- public int ReadVariableInt32()
- {
- uint n = ReadVariableUInt32();
- return (int)(n >> 1) ^ -(int)(n & 1); // decode zigzag
- }
-
- ///
- /// Reads a variable sized Int64 written using WriteVariableInt64()
- ///
- public Int64 ReadVariableInt64()
- {
- UInt64 n = ReadVariableUInt64();
- return (Int64)(n >> 1) ^ -(long)(n & 1); // decode zigzag
- }
-
- ///
- /// Reads a variable sized UInt32 written using WriteVariableInt64()
- ///
- [CLSCompliant(false)]
- public UInt64 ReadVariableUInt64()
- {
- UInt64 num1 = 0;
- int num2 = 0;
- while (true)
- {
- //if (num2 == 0x23)
- // throw new FormatException("Bad 7-bit encoded integer");
-
- byte num3 = this.ReadByte();
- num1 |= ((UInt64)num3 & 0x7f) << num2;
- num2 += 7;
- if ((num3 & 0x80) == 0)
- return num1;
- }
- }
-
- ///
- /// Reads a 32 bit floating point value written using WriteSignedSingle()
- ///
- /// The number of bits used when writing the value
- /// A floating point value larger or equal to -1 and smaller or equal to 1
- public float ReadSignedSingle(int numberOfBits)
- {
- uint encodedVal = ReadUInt32(numberOfBits);
- int maxVal = (1 << numberOfBits) - 1;
- return ((float)(encodedVal + 1) / (float)(maxVal + 1) - 0.5f) * 2.0f;
- }
-
- ///
- /// Reads a 32 bit floating point value written using WriteUnitSingle()
- ///
- /// The number of bits used when writing the value
- /// A floating point value larger or equal to 0 and smaller or equal to 1
- public float ReadUnitSingle(int numberOfBits)
- {
- uint encodedVal = ReadUInt32(numberOfBits);
- int maxVal = (1 << numberOfBits) - 1;
- return (float)(encodedVal + 1) / (float)(maxVal + 1);
- }
-
- ///
- /// Reads a 32 bit floating point value written using WriteRangedSingle()
- ///
- /// The minimum value used when writing the value
- /// The maximum value used when writing the value
- /// The number of bits used when writing the value
- /// A floating point value larger or equal to MIN and smaller or equal to MAX
- public float ReadRangedSingle(float min, float max, int numberOfBits)
- {
- float range = max - min;
- int maxVal = (1 << numberOfBits) - 1;
- float encodedVal = (float)ReadUInt32(numberOfBits);
- float unit = encodedVal / (float)maxVal;
- return min + (unit * range);
- }
-
- ///
- /// Reads a 32 bit integer value written using WriteRangedInteger()
- ///
- /// The minimum value used when writing the value
- /// The maximum value used when writing the value
- /// A signed integer value larger or equal to MIN and smaller or equal to MAX
- public int ReadRangedInteger(int min, int max)
- {
- uint range = (uint)(max - min);
- int numBits = NetUtility.BitsToHoldUInt(range);
-
- uint rvalue = ReadUInt32(numBits);
- return (int)(min + rvalue);
- }
-
- ///
- /// Reads a string written using Write(string)
- ///
- public string ReadString()
- {
- int byteLen = (int)ReadVariableUInt32();
-
- if (byteLen == 0)
- return String.Empty;
-
- NetException.Assert(m_bitLength - m_readPosition >= (byteLen * 8), c_readOverflowError);
-
- if ((m_readPosition & 7) == 0)
- {
- // read directly
- string retval = System.Text.Encoding.UTF8.GetString(m_data, m_readPosition >> 3, byteLen);
- m_readPosition += (8 * byteLen);
- return retval;
- }
-
- byte[] bytes = ReadBytes(byteLen);
- return System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
- }
-
- ///
- /// Reads a string written using Write(string) and returns true for success
- ///
- public bool ReadString(out string result)
- {
- uint byteLen;
- if (ReadVariableUInt32(out byteLen) == false)
- {
- result = String.Empty;
- return false;
- }
-
- if (byteLen == 0)
- {
- result = String.Empty;
- return true;
- }
-
- if (m_bitLength - m_readPosition < (byteLen * 8))
- {
- result = String.Empty;
- return false;
- }
-
- if ((m_readPosition & 7) == 0)
- {
- // read directly
- result = System.Text.Encoding.UTF8.GetString(m_data, m_readPosition >> 3, (int)byteLen);
- m_readPosition += (8 * (int)byteLen);
- return true;
- }
-
- byte[] bytes;
- if (ReadBytes((int)byteLen, out bytes) == false)
- {
- result = String.Empty;
- return false;
- }
-
- result = System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
- return true;
- }
-
- ///
- /// Reads a stored IPv4 endpoint description
- ///
- public IPEndPoint ReadIPEndpoint()
- {
- byte len = ReadByte();
- byte[] addressBytes = ReadBytes(len);
- int port = (int)ReadUInt16();
-
- IPAddress address = new IPAddress(addressBytes);
- return new IPEndPoint(address, port);
- }
-
- ///
- /// Reads a value, in local time comparable to NetTime.Now, written using WriteTime()
- /// Must have a connected sender
- ///
- public double ReadTime(bool highPrecision)
- {
- double remoteTime = (highPrecision ? ReadDouble() : (double)ReadSingle());
-
- if (m_senderConnection == null)
- throw new NetException("Cannot call ReadTime() on message without a connected sender (ie. unconnected messages)");
-
- // lets bypass NetConnection.GetLocalTime for speed
- return remoteTime - m_senderConnection.m_remoteTimeOffset;
- }
-
- ///
- /// Pads data with enough bits to reach a full byte. Decreases cpu usage for subsequent byte writes.
- ///
- public void SkipPadBits()
- {
- m_readPosition = ((m_readPosition + 7) >> 3) * 8;
- }
-
- ///
- /// Pads data with enough bits to reach a full byte. Decreases cpu usage for subsequent byte writes.
- ///
- public void ReadPadBits()
- {
- m_readPosition = ((m_readPosition + 7) >> 3) * 8;
- }
-
- ///
- /// Pads data with the specified number of bits.
- ///
- public void SkipPadBits(int numberOfBits)
- {
- m_readPosition += numberOfBits;
- }
- }
-}
diff --git a/Lidgren.Network/NetIncomingMessage.Write.cs b/Lidgren.Network/NetIncomingMessage.Write.cs
deleted file mode 100644
index a4e7622..0000000
--- a/Lidgren.Network/NetIncomingMessage.Write.cs
+++ /dev/null
@@ -1,475 +0,0 @@
-/* Copyright (c) 2010 Michael Lidgren
-
-Permission is hereby granted, free of charge, to any person obtaining a copy of this software
-and associated documentation files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
-the Software is furnished to do so, subject to the following conditions:
-
-The above copyright notice and this permission notice shall be included in all copies or
-substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
-TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-*/
-using System;
-using System.Collections.Generic;
-using System.Net;
-using System.Diagnostics;
-using System.Text;
-
-namespace Lidgren.Network
-{
- public partial class NetIncomingMessage
- {
- ///
- /// Ensures the buffer can hold this number of bits
- ///
- private void InternalEnsureBufferSize(int numberOfBits)
- {
- int byteLen = ((numberOfBits + 7) >> 3);
- if (m_data == null)
- {
- m_data = new byte[byteLen];
- return;
- }
- if (m_data.Length < byteLen)
- Array.Resize(ref m_data, byteLen);
- return;
- }
-
- //
- // 1 bit
- //
- internal void Write(bool value)
- {
- InternalEnsureBufferSize(m_bitLength + 1);
- NetBitWriter.WriteByte((value ? (byte)1 : (byte)0), 1, m_data, m_bitLength);
- m_bitLength += 1;
- }
-
- //
- // 8 bit
- //
- internal void Write(byte source)
- {
- InternalEnsureBufferSize(m_bitLength + 8);
- NetBitWriter.WriteByte(source, 8, m_data, m_bitLength);
- m_bitLength += 8;
- }
-
- internal void Write(sbyte source)
- {
- InternalEnsureBufferSize(m_bitLength + 8);
- NetBitWriter.WriteByte((byte)source, 8, m_data, m_bitLength);
- m_bitLength += 8;
- }
-
- internal void Write(byte source, int numberOfBits)
- {
- NetException.Assert((numberOfBits > 0 && numberOfBits <= 8), "Write(byte, numberOfBits) can only write between 1 and 8 bits");
- InternalEnsureBufferSize(m_bitLength + numberOfBits);
- NetBitWriter.WriteByte(source, numberOfBits, m_data, m_bitLength);
- m_bitLength += numberOfBits;
- }
-
- internal void Write(byte[] source)
- {
- if (source == null)
- throw new ArgumentNullException("source");
- int bits = source.Length * 8;
- InternalEnsureBufferSize(m_bitLength + bits);
- NetBitWriter.WriteBytes(source, 0, source.Length, m_data, m_bitLength);
- m_bitLength += bits;
- }
-
- internal void Write(byte[] source, int offsetInBytes, int numberOfBytes)
- {
- if (source == null)
- throw new ArgumentNullException("source");
- int bits = numberOfBytes * 8;
- InternalEnsureBufferSize(m_bitLength + bits);
- NetBitWriter.WriteBytes(source, offsetInBytes, numberOfBytes, m_data, m_bitLength);
- m_bitLength += bits;
- }
-
- //
- // 16 bit
- //
- internal void Write(UInt16 source)
- {
- InternalEnsureBufferSize(m_bitLength + 16);
- NetBitWriter.WriteUInt32((uint)source, 16, m_data, m_bitLength);
- m_bitLength += 16;
- }
-
- internal void Write(UInt16 source, int numberOfBits)
- {
- NetException.Assert((numberOfBits > 0 && numberOfBits <= 16), "Write(ushort, numberOfBits) can only write between 1 and 16 bits");
- InternalEnsureBufferSize(m_bitLength + numberOfBits);
- NetBitWriter.WriteUInt32((uint)source, numberOfBits, m_data, m_bitLength);
- m_bitLength += numberOfBits;
- }
-
- internal void Write(Int16 source)
- {
- InternalEnsureBufferSize(m_bitLength + 16);
- NetBitWriter.WriteUInt32((uint)source, 16, m_data, m_bitLength);
- m_bitLength += 16;
- }
-
- //
- // 32 bit
- //
-#if UNSAFE
- internal unsafe void Write(Int32 source)
- {
- EnsureBufferSize(m_bitLength + 32);
-
- // can write fast?
- if (m_bitLength % 8 == 0)
- {
- fixed (byte* numRef = &Data[m_bitLength / 8])
- {
- *((int*)numRef) = source;
- }
- }
- else
- {
- NetBitWriter.WriteUInt32((UInt32)source, 32, Data, m_bitLength);
- }
- m_bitLength += 32;
- }
-#else
- internal void Write(Int32 source)
- {
- InternalEnsureBufferSize(m_bitLength + 32);
- NetBitWriter.WriteUInt32((UInt32)source, 32, m_data, m_bitLength);
- m_bitLength += 32;
- }
-#endif
-
-#if UNSAFE
- internal unsafe void Write(UInt32 source)
- {
- EnsureBufferSize(m_bitLength + 32);
-
- // can write fast?
- if (m_bitLength % 8 == 0)
- {
- fixed (byte* numRef = &Data[m_bitLength / 8])
- {
- *((uint*)numRef) = source;
- }
- }
- else
- {
- NetBitWriter.WriteUInt32(source, 32, Data, m_bitLength);
- }
-
- m_bitLength += 32;
- }
-#else
- internal void Write(UInt32 source)
- {
- InternalEnsureBufferSize(m_bitLength + 32);
- NetBitWriter.WriteUInt32(source, 32, m_data, m_bitLength);
- m_bitLength += 32;
- }
-#endif
-
- internal void Write(UInt32 source, int numberOfBits)
- {
- NetException.Assert((numberOfBits > 0 && numberOfBits <= 32), "Write(uint, numberOfBits) can only write between 1 and 32 bits");
- InternalEnsureBufferSize(m_bitLength + numberOfBits);
- NetBitWriter.WriteUInt32(source, numberOfBits, m_data, m_bitLength);
- m_bitLength += numberOfBits;
- }
-
- internal void Write(Int32 source, int numberOfBits)
- {
- NetException.Assert((numberOfBits > 0 && numberOfBits <= 32), "Write(int, numberOfBits) can only write between 1 and 32 bits");
- InternalEnsureBufferSize(m_bitLength + numberOfBits);
-
- if (numberOfBits != 32)
- {
- // make first bit sign
- int signBit = 1 << (numberOfBits - 1);
- if (source < 0)
- source = (-source - 1) | signBit;
- else
- source &= (~signBit);
- }
-
- NetBitWriter.WriteUInt32((uint)source, numberOfBits, m_data, m_bitLength);
-
- m_bitLength += numberOfBits;
- }
-
- //
- // 64 bit
- //
- internal void Write(UInt64 source)
- {
- InternalEnsureBufferSize(m_bitLength + 64);
- NetBitWriter.WriteUInt64(source, 64, m_data, m_bitLength);
- m_bitLength += 64;
- }
-
- internal void Write(UInt64 source, int numberOfBits)
- {
- InternalEnsureBufferSize(m_bitLength + numberOfBits);
- NetBitWriter.WriteUInt64(source, numberOfBits, m_data, m_bitLength);
- m_bitLength += numberOfBits;
- }
-
- internal void Write(Int64 source)
- {
- InternalEnsureBufferSize(m_bitLength + 64);
- ulong usource = (ulong)source;
- NetBitWriter.WriteUInt64(usource, 64, m_data, m_bitLength);
- m_bitLength += 64;
- }
-
- internal void Write(Int64 source, int numberOfBits)
- {
- InternalEnsureBufferSize(m_bitLength + numberOfBits);
- ulong usource = (ulong)source;
- NetBitWriter.WriteUInt64(usource, numberOfBits, m_data, m_bitLength);
- m_bitLength += numberOfBits;
- }
-
- //
- // Floating point
- //
-#if UNSAFE
- internal unsafe void Write(float source)
- {
- uint val = *((uint*)&source);
-#if BIGENDIAN
- val = NetUtility.SwapByteOrder(val);
-#endif
- Write(val);
- }
-#else
- internal void Write(float source)
- {
- byte[] val = BitConverter.GetBytes(source);
-#if BIGENDIAN
- // swap byte order
- byte tmp = val[3];
- val[3] = val[0];
- val[0] = tmp;
- tmp = val[2];
- val[2] = val[1];
- val[1] = tmp;
-#endif
- Write(val);
- }
-#endif
-
-#if UNSAFE
- internal unsafe void Write(double source)
- {
- ulong val = *((ulong*)&source);
-#if BIGENDIAN
- val = NetUtility.SwapByteOrder(val);
-#endif
- Write(val);
- }
-#else
- internal void Write(double source)
- {
- byte[] val = BitConverter.GetBytes(source);
-#if BIGENDIAN
- // 0 1 2 3 4 5 6 7
-
- // swap byte order
- byte tmp = val[7];
- val[7] = val[0];
- val[0] = tmp;
-
- tmp = val[6];
- val[6] = val[1];
- val[1] = tmp;
-
- tmp = val[5];
- val[5] = val[2];
- val[2] = tmp;
-
- tmp = val[4];
- val[4] = val[3];
- val[3] = tmp;
-#endif
- Write(val);
- }
-#endif
-
- //
- // Variable bits
- //
-
- ///
- /// Write Base128 encoded variable sized unsigned integer
- ///
- /// number of bytes written
- internal int WriteVariableUInt32(uint value)
- {
- int retval = 1;
- uint num1 = (uint)value;
- while (num1 >= 0x80)
- {
- this.Write((byte)(num1 | 0x80));
- num1 = num1 >> 7;
- retval++;
- }
- this.Write((byte)num1);
- return retval;
- }
-
- ///
- /// Write Base128 encoded variable sized signed integer
- ///
- /// number of bytes written
- internal int WriteVariableInt32(int value)
- {
- int retval = 1;
- uint num1 = (uint)((value << 1) ^ (value >> 31));
- while (num1 >= 0x80)
- {
- this.Write((byte)(num1 | 0x80));
- num1 = num1 >> 7;
- retval++;
- }
- this.Write((byte)num1);
- return retval;
- }
-
- ///
- /// Write Base128 encoded variable sized unsigned integer
- ///
- /// number of bytes written
- internal int WriteVariableUInt64(UInt64 value)
- {
- int retval = 1;
- UInt64 num1 = (UInt64)value;
- while (num1 >= 0x80)
- {
- this.Write((byte)(num1 | 0x80));
- num1 = num1 >> 7;
- retval++;
- }
- this.Write((byte)num1);
- return retval;
- }
-
- ///
- /// Compress (lossy) a float in the range -1..1 using numberOfBits bits
- ///
- internal void WriteSignedSingle(float value, int numberOfBits)
- {
- NetException.Assert(((value >= -1.0) && (value <= 1.0)), " WriteSignedSingle() must be passed a float in the range -1 to 1; val is " + value);
-
- float unit = (value + 1.0f) * 0.5f;
- int maxVal = (1 << numberOfBits) - 1;
- uint writeVal = (uint)(unit * (float)maxVal);
-
- Write(writeVal, numberOfBits);
- }
-
- ///
- /// Compress (lossy) a float in the range 0..1 using numberOfBits bits
- ///
- internal void WriteUnitSingle(float value, int numberOfBits)
- {
- NetException.Assert(((value >= 0.0) && (value <= 1.0)), " WriteUnitSingle() must be passed a float in the range 0 to 1; val is " + value);
-
- int maxValue = (1 << numberOfBits) - 1;
- uint writeVal = (uint)(value * (float)maxValue);
-
- Write(writeVal, numberOfBits);
- }
-
- ///
- /// Compress a float within a specified range using a certain number of bits
- ///
- internal void WriteRangedSingle(float value, float min, float max, int numberOfBits)
- {
- NetException.Assert(((value >= min) && (value <= max)), " WriteRangedSingle() must be passed a float in the range MIN to MAX; val is " + value);
-
- float range = max - min;
- float unit = ((value - min) / range);
- int maxVal = (1 << numberOfBits) - 1;
- Write((UInt32)((float)maxVal * unit), numberOfBits);
- }
-
- ///
- /// Writes an integer with the least amount of bits need for the specified range
- /// Returns number of bits written
- ///
- internal int WriteRangedInteger(int min, int max, int value)
- {
- NetException.Assert(value >= min && value <= max, "Value not within min/max range!");
-
- uint range = (uint)(max - min);
- int numBits = NetUtility.BitsToHoldUInt(range);
-
- uint rvalue = (uint)(value - min);
- Write(rvalue, numBits);
-
- return numBits;
- }
-
- ///
- /// Write a string
- ///
- internal void Write(string source)
- {
- if (string.IsNullOrEmpty(source))
- {
- InternalEnsureBufferSize(m_bitLength + 8);
- WriteVariableUInt32(0);
- return;
- }
-
- byte[] bytes = Encoding.UTF8.GetBytes(source);
- InternalEnsureBufferSize(m_bitLength + ((bytes.Length + 2) * 8));
- WriteVariableUInt32((uint)bytes.Length);
- Write(bytes);
- }
-
- ///
- /// Writes an endpoint description
- ///
- internal void Write(IPEndPoint endPoint)
- {
- byte[] bytes = endPoint.Address.GetAddressBytes();
- Write((byte)bytes.Length);
- Write(bytes);
- Write((ushort)endPoint.Port);
- }
-
- ///
- /// Pads data with enough bits to reach a full byte. Decreases cpu usage for subsequent byte writes.
- ///
- internal void WritePadBits()
- {
- m_bitLength = ((m_bitLength + 7) / 8) * 8;
- InternalEnsureBufferSize(m_bitLength);
- }
-
- ///
- /// Pads data with the specified number of bits.
- ///
- internal void WritePadBits(int numberOfBits)
- {
- m_bitLength += numberOfBits;
- InternalEnsureBufferSize(m_bitLength);
- }
- }
-}