You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-06 02:11:06 +09:00
bool ReadXXX(out result) versions added for some read methods
This commit is contained in:
@@ -90,7 +90,22 @@ namespace Lidgren.Network
|
||||
m_readPosition += 8;
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reads a byte and returns true or false for success
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a signed byte
|
||||
/// </summary>
|
||||
@@ -127,6 +142,23 @@ namespace Lidgren.Network
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the specified number of bytes and returns true for success
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the specified number of bytes into a preallocated array
|
||||
/// </summary>
|
||||
@@ -239,6 +271,22 @@ namespace Lidgren.Network
|
||||
return retval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an 32 bit unsigned integer written using Write(UInt32) and returns true for success
|
||||
/// </summary>
|
||||
[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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an unsigned integer stored in 1 to 32 bits, written using Write(UInt32, Int32)
|
||||
/// </summary>
|
||||
@@ -384,6 +432,32 @@ namespace Lidgren.Network
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a variable sized UInt32 written using WriteVariableUInt32() and returns true for success
|
||||
/// </summary>
|
||||
[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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a variable sized Int32 written using WriteVariableInt32()
|
||||
/// </summary>
|
||||
@@ -502,6 +576,49 @@ namespace Lidgren.Network
|
||||
return System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a string written using Write(string) and returns true for success
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a stored IPv4 endpoint description
|
||||
/// </summary>
|
||||
|
||||
@@ -69,7 +69,13 @@ namespace UnitTests
|
||||
bdr.Append(inc.ReadBoolean());
|
||||
bdr.Append(inc.ReadInt32(6));
|
||||
bdr.Append(inc.ReadInt32());
|
||||
bdr.Append(inc.ReadString());
|
||||
|
||||
string strResult;
|
||||
bool ok = inc.ReadString(out strResult);
|
||||
if (ok == false)
|
||||
throw new NetException("Read/write failure");
|
||||
bdr.Append(strResult);
|
||||
|
||||
bdr.Append(inc.ReadByte());
|
||||
|
||||
if (inc.PeekUInt16() != (ushort)44)
|
||||
|
||||
Reference in New Issue
Block a user