1
0
Fork 0

Added VarInt64, normalized cPacketizer datatype names.

This commit is contained in:
Mattes D 2015-03-22 23:09:23 +01:00
parent c626848393
commit b913c5da69
8 changed files with 748 additions and 692 deletions

View File

@ -805,11 +805,11 @@ bool cConnection::HandleClientHandshake(void)
// Send the same packet to the server, but with our port:
cByteBuffer Packet(512);
Packet.WriteVarInt(0); // Packet type - initial handshake
Packet.WriteVarInt(ProtocolVersion);
Packet.WriteVarInt32(0); // Packet type - initial handshake
Packet.WriteVarInt32(ProtocolVersion);
Packet.WriteVarUTF8String(ServerHost);
Packet.WriteBEUInt16(m_Server.GetConnectPort());
Packet.WriteVarInt(NextState);
Packet.WriteVarInt32(NextState);
AString Pkt;
Packet.ReadAll(Pkt);
cByteBuffer ToServer(512);
@ -2459,7 +2459,7 @@ bool cConnection::HandleServerStatusResponse(void)
Log("Cannot find the description json element, ProtoProxy signature not inserted");
}
cByteBuffer Packet(Response.size() + 50);
Packet.WriteVarInt(0); // Packet type - status response
Packet.WriteVarInt32(0); // Packet type - status response
Packet.WriteVarUTF8String(Response);
AString Pkt;
Packet.ReadAll(Pkt);
@ -2775,7 +2775,7 @@ bool cConnection::ParseMetadata(cByteBuffer & a_Buffer, AString & a_Metadata)
}
rs = rs - static_cast<int>(a_Buffer.GetReadableSpace());
cByteBuffer LenBuf(8);
LenBuf.WriteVarInt(Len);
LenBuf.WriteVarInt32(Len);
AString VarLen;
LenBuf.ReadAll(VarLen);
a_Metadata.append(VarLen);
@ -2960,7 +2960,7 @@ void cConnection::SendEncryptionKeyResponse(const AString & a_ServerPublicKey, c
DataLog(EncryptedSecret, sizeof(EncryptedSecret), "Encrypted secret (%u bytes)", static_cast<unsigned>(sizeof(EncryptedSecret)));
DataLog(EncryptedNonce, sizeof(EncryptedNonce), "Encrypted nonce (%u bytes)", static_cast<unsigned>(sizeof(EncryptedNonce)));
cByteBuffer Len(5);
Len.WriteVarInt(static_cast<UInt32>(ToServer.GetReadableSpace()));
Len.WriteVarInt32(static_cast<UInt32>(ToServer.GetReadableSpace()));
SERVERSEND(Len);
SERVERSEND(ToServer);
m_ServerState = csEncryptedUnderstood;

View File

@ -81,9 +81,9 @@ public:
void TestWrite(void)
{
cByteBuffer buf(50);
buf.WriteVarInt(5);
buf.WriteVarInt(300);
buf.WriteVarInt(0);
buf.WriteVarInt32(5);
buf.WriteVarInt32(300);
buf.WriteVarInt32(0);
AString All;
buf.ReadAll(All);
assert_test(All.size() == 4);
@ -490,7 +490,7 @@ bool cByteBuffer::ReadBEUTF16String16(AString & a_Value)
bool cByteBuffer::ReadVarInt(UInt32 & a_Value)
bool cByteBuffer::ReadVarInt32(UInt32 & a_Value)
{
CHECK_THREAD
CheckValid();
@ -501,7 +501,29 @@ bool cByteBuffer::ReadVarInt(UInt32 & a_Value)
{
NEEDBYTES(1);
ReadBuf(&b, 1);
Value = Value | (((UInt32)(b & 0x7f)) << Shift);
Value = Value | ((static_cast<UInt32>(b & 0x7f)) << Shift);
Shift += 7;
} while ((b & 0x80) != 0);
a_Value = Value;
return true;
}
bool cByteBuffer::ReadVarInt64(UInt64 & a_Value)
{
CHECK_THREAD
CheckValid();
UInt64 Value = 0;
int Shift = 0;
unsigned char b = 0;
do
{
NEEDBYTES(1);
ReadBuf(&b, 1);
Value = Value | ((static_cast<UInt64>(b & 0x7f)) << Shift);
Shift += 7;
} while ((b & 0x80) != 0);
a_Value = Value;
@ -551,7 +573,7 @@ bool cByteBuffer::ReadLEInt(int & a_Value)
bool cByteBuffer::ReadPosition(int & a_BlockX, int & a_BlockY, int & a_BlockZ)
bool cByteBuffer::ReadPosition64(int & a_BlockX, int & a_BlockY, int & a_BlockZ)
{
CHECK_THREAD
Int64 Value;
@ -719,7 +741,7 @@ bool cByteBuffer::WriteBool(bool a_Value)
bool cByteBuffer::WriteVarInt(UInt32 a_Value)
bool cByteBuffer::WriteVarInt32(UInt32 a_Value)
{
CHECK_THREAD
CheckValid();
@ -740,12 +762,35 @@ bool cByteBuffer::WriteVarInt(UInt32 a_Value)
bool cByteBuffer::WriteVarInt64(UInt64 a_Value)
{
CHECK_THREAD
CheckValid();
// A 64-bit integer can be encoded by at most 10 bytes:
unsigned char b[10];
size_t idx = 0;
do
{
b[idx] = (a_Value & 0x7f) | ((a_Value > 0x7f) ? 0x80 : 0x00);
a_Value = a_Value >> 7;
idx++;
} while (a_Value > 0);
return WriteBuf(b, idx);
}
bool cByteBuffer::WriteVarUTF8String(const AString & a_Value)
{
CHECK_THREAD
CheckValid();
PUTBYTES(a_Value.size() + 1); // This is a lower-bound on the bytes that will be actually written. Fail early.
bool res = WriteVarInt((UInt32)(a_Value.size()));
bool res = WriteVarInt32(static_cast<UInt32>(a_Value.size()));
if (!res)
{
return false;

View File

@ -64,19 +64,20 @@ public:
bool ReadBEDouble (double & a_Value);
bool ReadBool (bool & a_Value);
bool ReadBEUTF16String16(AString & a_Value); // string length as BE short, then string as UTF-16BE
bool ReadVarInt (UInt32 & a_Value);
bool ReadVarInt32 (UInt32 & a_Value);
bool ReadVarInt64 (UInt64 & a_Value);
bool ReadVarUTF8String (AString & a_Value); // string length as VarInt, then string as UTF-8
bool ReadLEInt (int & a_Value);
bool ReadPosition (int & a_BlockX, int & a_BlockY, int & a_BlockZ);
bool ReadPosition64 (int & a_BlockX, int & a_BlockY, int & a_BlockZ);
/** Reads VarInt, assigns it to anything that can be assigned from an UInt32 (unsigned short, char, Byte, double, ...) */
/** Reads VarInt, assigns it to anything that can be assigned from an UInt64 (unsigned short, char, Byte, double, ...) */
template <typename T> bool ReadVarInt(T & a_Value)
{
UInt32 v;
bool res = ReadVarInt(v);
UInt64 v;
bool res = ReadVarInt64(v);
if (res)
{
a_Value = v;
a_Value = static_cast<T>(v);
}
return res;
}
@ -93,7 +94,8 @@ public:
bool WriteBEFloat (float a_Value);
bool WriteBEDouble (double a_Value);
bool WriteBool (bool a_Value);
bool WriteVarInt (UInt32 a_Value);
bool WriteVarInt32 (UInt32 a_Value);
bool WriteVarInt64 (UInt64 a_Value);
bool WriteVarUTF8String (const AString & a_Value); // string length as VarInt, then string as UTF-8
bool WriteLEInt32 (Int32 a_Value);
bool WritePosition64 (Int32 a_BlockX, Int32 a_BlockY, Int32 a_BlockZ);

View File

@ -187,7 +187,7 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu
// Create the packet:
cByteBuffer Packet(512 KiB);
Packet.WriteVarInt(0x21); // Packet id (Chunk Data packet)
Packet.WriteVarInt32(0x21); // Packet id (Chunk Data packet)
Packet.WriteBEInt32(a_ChunkX);
Packet.WriteBEInt32(a_ChunkZ);
Packet.WriteBool(true); // "Ground-up continuous", or rather, "biome data present" flag
@ -201,7 +201,7 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu
sizeof(m_BlockSkyLight) + // Block sky light
BiomeDataSize // Biome data
);
Packet.WriteVarInt(ChunkSize);
Packet.WriteVarInt32(ChunkSize);
// Write the block types to the packet:
for (size_t Index = 0; Index < cChunkDef::NumBlocks; Index++)
@ -234,8 +234,8 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu
else
{
AString PostData;
Buffer.WriteVarInt((UInt32)Packet.GetUsedSpace() + 1);
Buffer.WriteVarInt(0);
Buffer.WriteVarInt32(static_cast<UInt32>(Packet.GetUsedSpace() + 1));
Buffer.WriteVarInt32(0);
Buffer.ReadAll(PostData);
Buffer.CommitRead();

View File

@ -64,7 +64,7 @@ cPacketizer::~cPacketizer()
void cPacketizer::WriteByteAngle(double a_Angle)
{
WriteChar(static_cast<char>(255 * a_Angle / 360));
WriteBEInt8(static_cast<Int8>(255 * a_Angle / 360));
}
@ -73,8 +73,7 @@ void cPacketizer::WriteByteAngle(double a_Angle)
void cPacketizer::WriteFPInt(double a_Value)
{
Int32 Value = static_cast<Int32>(a_Value * 32);
WriteInt(Value);
WriteBEInt32(static_cast<Int32>(a_Value * 32));
}

View File

@ -29,7 +29,7 @@ public:
m_Lock(a_Protocol.m_CSPacket),
m_PacketType(a_PacketType) // Used for logging purposes
{
m_Out.WriteVarInt(a_PacketType);
m_Out.WriteVarInt32(a_PacketType);
}
/** Sends the packet via the contained protocol's SendPacket() function. */
@ -40,19 +40,19 @@ public:
VERIFY(m_Out.WriteBool(a_Value));
}
inline void WriteByte(Byte a_Value)
inline void WriteBEUInt8(UInt8 a_Value)
{
VERIFY(m_Out.WriteBEUInt8(a_Value));
}
inline void WriteChar(char a_Value)
inline void WriteBEInt8(Int8 a_Value)
{
VERIFY(m_Out.WriteBEInt8(a_Value));
}
inline void WriteShort(short a_Value)
inline void WriteBEInt16(Int16 a_Value)
{
VERIFY(m_Out.WriteBEInt16(a_Value));
}
@ -64,39 +64,45 @@ public:
}
inline void WriteInt(Int32 a_Value)
inline void WriteBEInt32(Int32 a_Value)
{
VERIFY(m_Out.WriteBEInt32(a_Value));
}
inline void WriteUInt32(UInt32 a_Value)
inline void WriteBEUInt32(UInt32 a_Value)
{
VERIFY(m_Out.WriteBEUInt32(a_Value));
}
inline void WriteInt64(Int64 a_Value)
inline void WriteBEInt64(Int64 a_Value)
{
VERIFY(m_Out.WriteBEInt64(a_Value));
}
inline void WriteFloat(float a_Value)
inline void WriteBEUInt64(UInt64 a_Value)
{
VERIFY(m_Out.WriteBEUInt64(a_Value));
}
inline void WriteBEFloat(float a_Value)
{
VERIFY(m_Out.WriteBEFloat(a_Value));
}
inline void WriteDouble(double a_Value)
inline void WriteBEDouble(double a_Value)
{
VERIFY(m_Out.WriteBEDouble(a_Value));
}
inline void WriteVarInt(UInt32 a_Value)
inline void WriteVarInt32(UInt32 a_Value)
{
VERIFY(m_Out.WriteVarInt(a_Value));
VERIFY(m_Out.WriteVarInt32(a_Value));
}
@ -113,7 +119,7 @@ public:
/** Writes the specified block position as a single encoded 64-bit BigEndian integer. */
inline void WritePosition(int a_BlockX, int a_BlockY, int a_BlockZ)
inline void WritePosition64(int a_BlockX, int a_BlockY, int a_BlockZ)
{
VERIFY(m_Out.WritePosition64(a_BlockX, a_BlockY, a_BlockZ));
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff