2020-07-28 00:07:18 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
namespace Feather::Network
|
|
|
|
{
|
|
|
|
|
|
|
|
// Class to read packet data, such as is produced by NetworkMessage
|
|
|
|
class PacketReader
|
|
|
|
{
|
|
|
|
public:
|
2020-07-28 01:26:46 +01:00
|
|
|
PacketReader(uint8_t* dataPtr)
|
2020-07-28 05:29:02 +01:00
|
|
|
: m_data(dataPtr), m_length(ReadVarInt())
|
2020-07-28 01:26:46 +01:00
|
|
|
{
|
|
|
|
m_data = dataPtr;
|
|
|
|
m_length = ReadVarInt();
|
|
|
|
}
|
2020-07-28 00:07:18 +01:00
|
|
|
|
|
|
|
inline uint8_t ReadByte()
|
|
|
|
{
|
|
|
|
return m_data[m_offset++];
|
|
|
|
}
|
|
|
|
|
2020-07-28 05:32:55 +01:00
|
|
|
inline int ReadVarInt()
|
2020-07-28 00:07:18 +01:00
|
|
|
{
|
|
|
|
int numRead = 0;
|
|
|
|
int result = 0;
|
|
|
|
uint8_t read;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
read = ReadByte();
|
|
|
|
int value = (read & 0b01111111);
|
|
|
|
result |= (value << (7 * numRead));
|
|
|
|
|
|
|
|
numRead++;
|
|
|
|
if (numRead > 5)
|
|
|
|
{
|
|
|
|
// complain
|
|
|
|
//throw new RuntimeException("VarInt is too big");
|
|
|
|
}
|
|
|
|
} while ((read & 0b10000000) != 0);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-28 05:32:55 +01:00
|
|
|
inline uint32_t Length() const { return m_length; }
|
2020-07-28 00:07:18 +01:00
|
|
|
private:
|
2020-07-28 05:29:02 +01:00
|
|
|
const uint8_t const* m_data;
|
|
|
|
const uint32_t m_length;
|
|
|
|
uint32_t m_offset = 0;
|
2020-07-28 00:07:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|