Implement PacketReader handling just one buffer
This commit is contained in:
parent
a923dd07a2
commit
28f64a58e1
|
@ -0,0 +1,52 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Feather::Network
|
||||
{
|
||||
|
||||
// Class to read packet data, such as is produced by NetworkMessage
|
||||
class PacketReader
|
||||
{
|
||||
public:
|
||||
PacketReader(const uint8_t *const dataPtr) :
|
||||
m_data(dataPtr),
|
||||
m_length(ReadVarInt())
|
||||
{}
|
||||
|
||||
inline uint8_t ReadByte()
|
||||
{
|
||||
return m_data[m_offset++];
|
||||
}
|
||||
|
||||
int ReadVarInt()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
uint32_t Length() const { return m_length; }
|
||||
private:
|
||||
const uint32_t m_length;
|
||||
const uint8_t *const m_data;
|
||||
uint32_t m_offset = 0;
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue