1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-16 23:26:32 +09:00

NetConnection.LocalHailMessage exposed. WriteVariableInt64 and ReadVariableInt64 added.

This commit is contained in:
lidgren
2010-12-28 13:13:00 +00:00
parent b83ec10d8d
commit eb056bc9aa
4 changed files with 58 additions and 3 deletions

View File

@@ -60,6 +60,11 @@ namespace Lidgren.Network
/// </summary> /// </summary>
public long RemoteUniqueIdentifier { get { return m_remoteUniqueIdentifier; } } public long RemoteUniqueIdentifier { get { return m_remoteUniqueIdentifier; } }
/// <summary>
/// Gets the local hail message that was sent as part of the handshake
/// </summary>
public NetOutgoingMessage LocalHailMessage { get { return m_localHailMessage; } }
// gets the time before automatically resending an unacked message // gets the time before automatically resending an unacked message
internal float GetResendDelay() internal float GetResendDelay()
{ {

View File

@@ -363,6 +363,29 @@ namespace Lidgren.Network
} }
} }
/// <summary>
/// Reads a Int64 written using WriteVariableInt64()
/// </summary>
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);
}
}
}
/// <summary> /// <summary>
/// Reads a UInt32 written using WriteVariableInt64() /// Reads a UInt32 written using WriteVariableInt64()
/// </summary> /// </summary>

View File

@@ -429,6 +429,24 @@ namespace Lidgren.Network
return retval; return retval;
} }
/// <summary>
/// Write Base128 encoded variable sized signed integer
/// </summary>
/// <returns>number of bytes written</returns>
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;
}
/// <summary> /// <summary>
/// Write Base128 encoded variable sized unsigned integer /// Write Base128 encoded variable sized unsigned integer
/// </summary> /// </summary>

View File

@@ -23,10 +23,17 @@ namespace UnitTests
msg.WritePadBits(); msg.WritePadBits();
int bcnt = 0;
msg.Write(45.0f); msg.Write(45.0f);
msg.Write(46.0); msg.Write(46.0);
msg.WriteVariableInt32(-47); bcnt += msg.WriteVariableInt32(-47);
msg.WriteVariableInt32(470000);
msg.WriteVariableUInt32(48); msg.WriteVariableUInt32(48);
bcnt += msg.WriteVariableInt64(-49);
if (bcnt != 2)
throw new NetException("WriteVariable* wrote too many bytes!");
byte[] data = msg.PeekDataBuffer(); byte[] data = msg.PeekDataBuffer();
@@ -51,9 +58,11 @@ namespace UnitTests
bdr.Append(inc.ReadSingle()); bdr.Append(inc.ReadSingle());
bdr.Append(inc.ReadDouble()); bdr.Append(inc.ReadDouble());
bdr.Append(inc.ReadVariableInt32()); bdr.Append(inc.ReadVariableInt32());
bdr.Append(inc.ReadVariableInt32());
bdr.Append(inc.ReadVariableUInt32()); bdr.Append(inc.ReadVariableUInt32());
bdr.Append(inc.ReadVariableInt64());
if (bdr.ToString().Equals("False-342duke of earl4344True4546-4748")) if (bdr.ToString().Equals("False-342duke of earl4344True4546-4747000048-49"))
Console.WriteLine("Read/write tests OK"); Console.WriteLine("Read/write tests OK");
else else
throw new NetException("Read/write tests FAILED!"); throw new NetException("Read/write tests FAILED!");