tibia-client/src/framework/net/inputmessage.cpp

73 lines
1.2 KiB
C++
Raw Normal View History

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
}
2011-08-01 06:28:41 +02:00
uint8 InputMessage::getU8(bool blockReadPos)
2011-05-21 21:02:37 +02:00
{
2011-07-28 01:01:33 +02:00
assert(canRead(1));
2011-08-01 06:28:41 +02:00
uint8 v = m_buffer[m_readPos];
if(!blockReadPos)
m_readPos += 1;
return v;
2011-05-21 21:02:37 +02:00
}
2011-08-01 06:28:41 +02:00
uint16 InputMessage::getU16(bool blockReadPos)
2011-05-21 21:02:37 +02:00
{
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);
2011-08-01 06:28:41 +02:00
if(!blockReadPos)
m_readPos += 2;
2011-05-21 21:02:37 +02:00
return v;
}
2011-08-01 06:28:41 +02:00
uint32 InputMessage::getU32(bool blockReadPos)
2011-05-21 21:02:37 +02:00
{
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);
2011-08-01 06:28:41 +02:00
if(!blockReadPos)
m_readPos += 4;
2011-05-21 21:02:37 +02:00
return v;
}
2011-08-01 06:28:41 +02:00
uint64 InputMessage::getU64(bool blockReadPos)
2011-05-21 21:02:37 +02:00
{
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);
2011-08-01 06:28:41 +02:00
if(!blockReadPos)
m_readPos += 8;
2011-05-21 21:02:37 +02:00
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
}