2011-07-28 01:01:33 +02:00
|
|
|
#include "inputmessage.h"
|
2011-05-21 21:02:37 +02:00
|
|
|
|
|
|
|
InputMessage::InputMessage()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputMessage::reset()
|
|
|
|
{
|
|
|
|
m_readPos = 0;
|
2011-05-30 05:11:12 +02:00
|
|
|
m_messageSize = 2;
|
2011-05-21 21:02:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8 InputMessage::getU8()
|
|
|
|
{
|
2011-07-28 01:01:33 +02:00
|
|
|
assert(canRead(1));
|
2011-05-21 21:02:37 +02:00
|
|
|
return m_buffer[m_readPos++];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16 InputMessage::getU16()
|
|
|
|
{
|
2011-07-28 01:01:33 +02:00
|
|
|
assert(canRead(2));
|
2011-05-21 21:02:37 +02:00
|
|
|
uint16 v = *(uint16_t*)(m_buffer + m_readPos);
|
|
|
|
m_readPos += 2;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32 InputMessage::getU32()
|
|
|
|
{
|
2011-07-28 01:01:33 +02:00
|
|
|
assert(canRead(4));
|
2011-05-21 21:02:37 +02:00
|
|
|
uint32 v = *(uint32*)(m_buffer + m_readPos);
|
|
|
|
m_readPos += 4;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 InputMessage::getU64()
|
|
|
|
{
|
2011-07-28 01:01:33 +02:00
|
|
|
assert(canRead(8));
|
2011-05-21 21:02:37 +02:00
|
|
|
uint64 v = *(uint64*)(m_buffer + m_readPos);
|
|
|
|
m_readPos += 8;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string InputMessage::getString()
|
|
|
|
{
|
|
|
|
uint16 stringLength = getU16();
|
2011-07-28 01:01:33 +02:00
|
|
|
assert(canRead(stringLength));
|
2011-05-21 21:02:37 +02:00
|
|
|
char* v = (char*)(m_buffer + m_readPos);
|
|
|
|
m_readPos += stringLength;
|
|
|
|
return std::string(v, stringLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputMessage::canRead(int bytes)
|
|
|
|
{
|
2011-05-31 07:24:30 +02:00
|
|
|
if((m_readPos + bytes > m_messageSize) || (m_readPos + bytes > BUFFER_MAXSIZE))
|
2011-07-28 01:01:33 +02:00
|
|
|
return false;
|
2011-05-31 07:24:30 +02:00
|
|
|
return true;
|
2011-05-21 21:02:37 +02:00
|
|
|
}
|