diff --git a/Lidgren.Network/NetIncomingMessage.Read.cs b/Lidgren.Network/NetIncomingMessage.Read.cs index 90d3fe0..a2e2d87 100644 --- a/Lidgren.Network/NetIncomingMessage.Read.cs +++ b/Lidgren.Network/NetIncomingMessage.Read.cs @@ -329,9 +329,6 @@ namespace Lidgren.Network int num2 = 0; while (true) { - if (num2 == 0x23) - throw new FormatException("Bad 7-bit encoded integer"); - byte num3 = this.ReadByte(); num1 |= (num3 & 0x7f) << num2; num2 += 7; @@ -345,22 +342,8 @@ namespace Lidgren.Network /// public int ReadVariableInt32() { - int num1 = 0; - int num2 = 0; - while (true) - { - if (num2 == 0x23) - throw new FormatException("Bad 7-bit encoded integer"); - - byte num3 = this.ReadByte(); - num1 |= (num3 & 0x7f) << (num2 & 0x1f); - num2 += 7; - if ((num3 & 0x80) == 0) - { - int sign = (num1 << 31) >> 31; - return sign ^ (num1 >> 1); - } - } + uint n = ReadVariableUInt32(); + return (int)(n >> 1) ^ -(int)(n & 1); // decode zigzag } /// @@ -368,22 +351,8 @@ namespace Lidgren.Network /// public Int64 ReadVariableInt64() { - Int64 num1 = 0; - int num2 = 0; - while (true) - { - if (num2 == 0x23) - throw new FormatException("Bad 7-bit encoded integer"); - - byte num3 = this.ReadByte(); - num1 |= (Int64)((Int64)(num3 & 0x7f) << (num2 & 0x1f)); - num2 += 7; - if ((num3 & 0x80) == 0) - { - Int64 sign = (num1 << 63) >> 63; - return sign ^ (num1 >> 1); - } - } + UInt64 n = ReadVariableUInt64(); + return (Int64)(n >> 1) ^ -(long)(n & 1); // decode zigzag } /// @@ -396,8 +365,8 @@ namespace Lidgren.Network int num2 = 0; while (true) { - if (num2 == 0x23) - throw new FormatException("Bad 7-bit encoded integer"); + //if (num2 == 0x23) + // throw new FormatException("Bad 7-bit encoded integer"); byte num3 = this.ReadByte(); num1 |= ((UInt64)num3 & 0x7f) << num2; @@ -511,6 +480,14 @@ namespace Lidgren.Network 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. /// diff --git a/Lidgren.Network/NetOutgoingMessage.Write.cs b/Lidgren.Network/NetOutgoingMessage.Write.cs index 82f5808..d12bbd1 100644 --- a/Lidgren.Network/NetOutgoingMessage.Write.cs +++ b/Lidgren.Network/NetOutgoingMessage.Write.cs @@ -393,7 +393,7 @@ namespace Lidgren.Network // /// - /// Write Base128 encoded variable sized unsigned integer + /// Write Base128 encoded variable sized unsigned integer of up to 32 bits /// /// number of bytes written [CLSCompliant(false)] @@ -412,43 +412,27 @@ namespace Lidgren.Network } /// - /// Write Base128 encoded variable sized signed integer + /// Write Base128 encoded variable sized signed integer of up to 32 bits /// /// number of bytes written public 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; + uint zigzag = (uint)(value << 1) ^ (uint)(value >> 31); + return WriteVariableUInt32(zigzag); } /// - /// Write Base128 encoded variable sized signed integer + /// Write Base128 encoded variable sized signed integer of up to 64 bits /// /// number of bytes written public int WriteVariableInt64(Int64 value) { - int retval = 1; - UInt64 num1 = (UInt64)((value << 1) ^ (value >> 31)); - while (num1 >= 0x80) - { - this.Write((byte)(num1 | 0x80)); - num1 = num1 >> 7; - retval++; - } - this.Write((byte)num1); - return retval; + ulong zigzag = (ulong)(value << 1) ^ (ulong)(value >> 63); + return WriteVariableUInt64(zigzag); } /// - /// Write Base128 encoded variable sized unsigned integer + /// Write Base128 encoded variable sized unsigned integer of up to 64 bits /// /// number of bytes written [CLSCompliant(false)] diff --git a/UnitTests/ReadWriteTests.cs b/UnitTests/ReadWriteTests.cs index 3a60284..0327d71 100644 --- a/UnitTests/ReadWriteTests.cs +++ b/UnitTests/ReadWriteTests.cs @@ -9,6 +9,30 @@ namespace UnitTests { public static class ReadWriteTests { + public static string ToBinaryString(ulong value, int bits, bool includeSpaces) + { + int numSpaces = Math.Max(0, (bits / 8) - 1); + if (includeSpaces == false) + numSpaces = 0; + + StringBuilder bdr = new StringBuilder(bits + numSpaces); + for (int i = 0; i < bits + numSpaces; i++) + bdr.Append(' '); + + for (int i = 0; i < bits; i++) + { + ulong shifted = (ulong)(value >> i); + bool isSet = ((shifted & 1) != 0); + + int pos = bits - 1 - i; + if (includeSpaces) + pos += Math.Max(0, (pos / 8)); + + bdr[pos] = (isSet ? '1' : '0'); + } + return bdr.ToString(); + } + public static void Run(NetPeer peer) { NetOutgoingMessage msg = peer.CreateMessage(); @@ -26,6 +50,7 @@ namespace UnitTests int bcnt = 0; msg.Write(45.0f); + msg.WriteVariableInt32(2115998022); msg.Write(46.0); bcnt += msg.WriteVariableInt32(-47); msg.WriteVariableInt32(470000); @@ -56,13 +81,14 @@ namespace UnitTests inc.SkipPadBits(); bdr.Append(inc.ReadSingle()); + bdr.Append(inc.ReadVariableInt32()); bdr.Append(inc.ReadDouble()); bdr.Append(inc.ReadVariableInt32()); bdr.Append(inc.ReadVariableInt32()); bdr.Append(inc.ReadVariableUInt32()); bdr.Append(inc.ReadVariableInt64()); - if (bdr.ToString().Equals("False-342duke of earl4344True4546-4747000048-49")) + if (bdr.ToString().Equals("False-342duke of earl4344True45211599802246-4747000048-49")) Console.WriteLine("Read/write tests OK"); else throw new NetException("Read/write tests FAILED!");