diff --git a/VC2008/MCServer.vcproj b/VC2008/MCServer.vcproj index abc362484..fec83695d 100644 --- a/VC2008/MCServer.vcproj +++ b/VC2008/MCServer.vcproj @@ -285,6 +285,14 @@ RelativePath="..\source\BlockID.h" > + + + + diff --git a/source/ByteBuffer.cpp b/source/ByteBuffer.cpp new file mode 100644 index 000000000..d25eb8e0b --- /dev/null +++ b/source/ByteBuffer.cpp @@ -0,0 +1,324 @@ + +// ByteBuffer.cpp + +// Implements the cByteBuffer class representing a ringbuffer of bytes + +#include "Globals.h" + +#include "ByteBuffer.h" +#include "Endianness.h" + + + + + +#define NEEDBYTES(Num) if (!CanReadBytes(Num)) return false; + + + + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// cByteBuffer: + +cByteBuffer::cByteBuffer(int a_BufferSize) : + m_Buffer(new char[a_BufferSize + 1]), + m_BufferSize(a_BufferSize + 1), + m_DataStart(0), + m_WritePos(0), + m_ReadPos(0) +{ + // Allocating one byte more than the buffer size requested, so that we can distinguish between + // completely-full and completely-empty states +} + + + + + +cByteBuffer::~cByteBuffer() +{ + delete m_Buffer; +} + + + + + +bool cByteBuffer::Write(const char * a_Bytes, int a_Count) +{ + if (GetFreeSpace() < a_Count) + { + return false; + } + int TillEnd = m_BufferSize - m_WritePos; + if (TillEnd < a_Count) + { + // Need to wrap around the ringbuffer end + memcpy(m_Buffer + m_WritePos, a_Bytes, TillEnd); + m_WritePos = 0; + a_Bytes += TillEnd; + a_Count -= TillEnd; + } + + // We're guaranteed that we'll fit in a single write op + memcpy(m_Buffer + m_WritePos, a_Bytes, a_Count); + m_WritePos += a_Count; + return true; +} + + + + + +int cByteBuffer::GetFreeSpace(void) const +{ + if (m_WritePos >= m_DataStart) + { + // Wrap around the buffer end: + return m_BufferSize - m_WritePos + m_DataStart - 1; + } + // Single free space partition: + return m_DataStart - m_WritePos - 1; +} + + + + + +/// Returns the number of bytes that are currently in the ringbuffer. Note GetReadableBytes() +int cByteBuffer::GetUsedSpace(void) const +{ + return m_BufferSize - GetFreeSpace(); +} + + + + + +/// Returns the number of bytes that are currently available for reading (may be less than UsedSpace due to some data having been read already) +int cByteBuffer::GetReadableSpace(void) const +{ + if (m_ReadPos > m_WritePos) + { + // Wrap around the buffer end: + return m_BufferSize - m_ReadPos + m_WritePos; + } + // Single readable space partition: + return m_WritePos - m_ReadPos ; +} + + + + + +bool cByteBuffer::CanReadBytes(int a_Count) const +{ + return (a_Count <= GetReadableSpace()); +} + + + + + +bool cByteBuffer::ReadChar(char & a_Value) +{ + NEEDBYTES(1); + a_Value = m_Buffer[m_ReadPos++]; + return true; +} + + + + + +bool cByteBuffer::ReadByte(unsigned char & a_Value) +{ + NEEDBYTES(1); + a_Value = (unsigned char)(m_Buffer[m_ReadPos++]); + return true; +} + + + + + +bool cByteBuffer::ReadBEShort(short & a_Value) +{ + NEEDBYTES(2); + ReadBuf(&a_Value, 2); + a_Value = ntohs(a_Value); + return true; +} + + + + + +bool cByteBuffer::ReadBEInt(int & a_Value) +{ + NEEDBYTES(4); + ReadBuf(&a_Value, 4); + a_Value = ntohl(a_Value); + return true; +} + + + + + +bool cByteBuffer::ReadBEInt64(Int64 & a_Value) +{ + NEEDBYTES(8); + ReadBuf(&a_Value, 8); + a_Value = NetworkToHostLong8(&a_Value); + return true; +} + + + + + +bool cByteBuffer::ReadBEFloat(float & a_Value) +{ + NEEDBYTES(4); + ReadBuf(&a_Value, 4); + a_Value = NetworkToHostFloat4(&a_Value); + return true; +} + + + + + +bool cByteBuffer::ReadBEDouble(double & a_Value) +{ + NEEDBYTES(8); + ReadBuf(&a_Value, 8); + a_Value = NetworkToHostDouble8(&a_Value); + return true; +} + + + + + +bool cByteBuffer::ReadBool(bool & a_Value) +{ + NEEDBYTES(1); + a_Value = (m_Buffer[m_ReadPos++] != 0); + return true; +} + + + + + +bool cByteBuffer::ReadBEUTF16String16(AString & a_Value) +{ + short Length; + if (!ReadBEShort(Length)) + { + return false; + } + return ReadUTF16String(a_Value, Length); +} + + + + + +bool cByteBuffer::ReadBuf(void * a_Buffer, int a_Count) +{ + NEEDBYTES(a_Count); + char * Dst = (char *)a_Buffer; // So that we can do byte math + int BytesToEndOfBuffer = m_BufferSize - m_ReadPos; + if (BytesToEndOfBuffer < a_Count) + { + // Reading across the ringbuffer end, read the first part and adjust parameters: + memcpy(Dst, m_Buffer + m_ReadPos, BytesToEndOfBuffer); + Dst += BytesToEndOfBuffer; + a_Count -= BytesToEndOfBuffer; + m_ReadPos = 0; + } + + // Read the rest of the bytes in a single read (guaranteed to fit): + memcpy(Dst, m_Buffer + m_ReadPos, a_Count); + m_ReadPos += a_Count; + return true; +} + + + + + +bool cByteBuffer::ReadString(AString & a_String, int a_Count) +{ + NEEDBYTES(a_Count); + a_String.clear(); + a_String.reserve(a_Count); + int BytesToEndOfBuffer = m_BufferSize - m_ReadPos; + if (BytesToEndOfBuffer < a_Count) + { + // Reading across the ringbuffer end, read the first part and adjust parameters: + a_String.assign(m_Buffer + m_ReadPos, BytesToEndOfBuffer); + a_Count -= BytesToEndOfBuffer; + m_ReadPos = 0; + } + + // Read the rest of the bytes in a single read (guaranteed to fit): + a_String.append(m_Buffer + m_ReadPos, a_Count); + m_ReadPos += a_Count; + return true; +} + + + + + +bool cByteBuffer::ReadUTF16String(AString & a_String, int a_NumChars) +{ + // Reads 2 * a_NumChars bytes and interprets it as a UTF16 string, converting it into UTF8 string a_String + AString RawData; + if (!ReadString(RawData, a_NumChars * 2)) + { + return false; + } + RawBEToUTF8((short *)(RawData.data()), a_NumChars, a_String); + return true; +} + + + + + +void cByteBuffer::CommitRead(void) +{ + m_DataStart = m_ReadPos; +} + + + + + +void cByteBuffer::ResetRead(void) +{ + m_ReadPos = m_DataStart; +} + + + + + +void cByteBuffer::AdvanceReadPos(int a_Count) +{ + m_ReadPos += a_Count; + if (m_ReadPos > m_BufferSize) + { + m_ReadPos -= m_BufferSize; + } +} + + + + diff --git a/source/ByteBuffer.h b/source/ByteBuffer.h new file mode 100644 index 000000000..23be3e922 --- /dev/null +++ b/source/ByteBuffer.h @@ -0,0 +1,90 @@ + +// ByteStream.h + +// Interfaces to the cByteBuffer class representing a ringbuffer of bytes + + + + + +#pragma once + + + + + +/** An object that can store incoming bytes and lets its clients read the bytes sequentially +The bytes are stored in a ringbuffer of constant size; if more than that size +is requested, the write operation fails. +The bytes stored can be retrieved using various ReadXXX functions; these assume that the needed +number of bytes are present in the buffer (ASSERT; for performance reasons). +The reading doesn't actually remove the bytes, it only moves the internal read ptr. +To remove the bytes, call CommitRead(). +To re-start reading from the beginning, call ResetRead(). +This class doesn't implement thread safety, the clients of this class need to provide +their own synchronization. +*/ +class cByteBuffer +{ +public: + cByteBuffer(int a_BufferSize); + ~cByteBuffer(); + + /// Writes the bytes specified to the ringbuffer. Returns true if successful, false if not + bool Write(const char * a_Bytes, int a_Count); + + /// Returns the number of bytes that can be successfully written to the ringbuffer + int GetFreeSpace(void) const; + + /// Returns the number of bytes that are currently in the ringbuffer. Note GetReadableBytes() + int GetUsedSpace(void) const; + + /// Returns the number of bytes that are currently available for reading (may be less than UsedSpace due to some data having been read already) + int GetReadableSpace(void) const; + + /// Returns true if the specified amount of bytes are available for reading + bool CanReadBytes(int a_Count) const; + + // Read the specified datatype and advance the read pointer; return true if successfully read: + bool ReadChar (char & a_Value); + bool ReadByte (unsigned char & a_Value); + bool ReadBEShort (short & a_Value); + bool ReadBEInt (int & a_Value); + bool ReadBEInt64 (Int64 & a_Value); + bool ReadBEFloat (float & a_Value); + bool ReadBEDouble (double & a_Value); + bool ReadBool (bool & a_Value); + bool ReadBEUTF16String16(AString & a_Value); + + /// Reads a_Count bytes into a_Buffer; return true if successful + bool ReadBuf(void * a_Buffer, int a_Count); + + /// Reads a_Count bytes into a_String; return true if successful + bool ReadString(AString & a_String, int a_Count); + + /// Reads 2 * a_NumChars bytes and interprets it as a UTF16 string, converting it into UTF8 string a_String + bool ReadUTF16String(AString & a_String, int a_NumChars); + + /// Skips reading by a_Count bytes + void SkipRead(int a_Count); + + /// Removes the bytes that have been read from the ringbuffer + void CommitRead(void); + + /// Restarts next reading operation at the start of the ringbuffer + void ResetRead(void); + +protected: + char * m_Buffer; + int m_BufferSize; // Total size of the ringbuffer + int m_DataStart; // Where the data starts in the ringbuffer + int m_WritePos; // Where the data ends in the ringbuffer + int m_ReadPos; // Where the next read will start in the ringbuffer + + /// Advances the m_ReadPos by a_Count bytes + void AdvanceReadPos(int a_Count); +} ; + + + + diff --git a/source/Globals.h b/source/Globals.h index 7d51bd52f..9ea0825c1 100644 --- a/source/Globals.h +++ b/source/Globals.h @@ -95,6 +95,11 @@ typedef short Int16; // Windows SDK defines min and max macros, messing up with our std::min and std::max usage #undef min #undef max + + // Windows SDK defines GetFreeSpace as a constant, probably a Win16 API remnant + #ifdef GetFreeSpace + #undef GetFreeSpace + #endif // GetFreeSpace #else #include #include // for mkdir diff --git a/source/StringUtils.cpp b/source/StringUtils.cpp index fa61a61e7..ccdbe687a 100644 --- a/source/StringUtils.cpp +++ b/source/StringUtils.cpp @@ -233,3 +233,49 @@ AStringList GetDirectoryContents(const char * a_Directory) + +// Converts a stream of BE shorts into UTF-8 string; returns a ref to a_UTF8 +AString & RawBEToUTF8(short * a_RawData, int a_NumShorts, AString & a_UTF8) +{ + a_UTF8.clear(); + a_UTF8.reserve(3 * a_NumShorts / 2); // a quick guess of the resulting size + for (int i = 0; i < a_NumShorts; i++) + { + int c = ntohs(*(a_RawData + i)); + if (c < 0x80) + { + a_UTF8.push_back((char)c); + } + else if (c < 0x800) + { + a_UTF8.push_back((char)(192 + c / 64)); + a_UTF8.push_back((char)(128 + c % 64)); + } + else if (c - 0xd800u < 0x800) + { + // Error, silently drop + } + else if (c < 0x10000) + { + a_UTF8.push_back((char)(224 + c / 4096)); + a_UTF8.push_back((char)(128 + c / 64 % 64)); + a_UTF8.push_back((char)(128 + c % 64)); + } + else if (c < 0x110000) + { + a_UTF8.push_back((char)(240 + c / 262144)); + a_UTF8.push_back((char)(128 + c / 4096 % 64)); + a_UTF8.push_back((char)(128 + c / 64 % 64)); + a_UTF8.push_back((char)(128 + c % 64)); + } + else + { + // Error, silently drop + } + } + return a_UTF8; +} + + + + diff --git a/source/StringUtils.h b/source/StringUtils.h index fee71481e..bc3c937b5 100644 --- a/source/StringUtils.h +++ b/source/StringUtils.h @@ -48,6 +48,9 @@ extern void ReplaceString(AString & iHayStack, const AString & iNeedle, const AS /// Returns the list of all items in the specified directory (files, folders, nix pipes, whatever's there) extern AStringList GetDirectoryContents(const char * a_Directory); +/// Converts a stream of BE shorts into UTF-8 string; returns a ref to a_UTF8 +extern AString & RawBEToUTF8(short * a_RawData, int a_NumShorts, AString & a_UTF8); + // If you have any other string helper functions, declare them here diff --git a/source/cClientHandle.cpp b/source/cClientHandle.cpp index 916fd781d..c045a4ed7 100644 --- a/source/cClientHandle.cpp +++ b/source/cClientHandle.cpp @@ -112,6 +112,7 @@ int cClientHandle::s_ClientCount = 0; cClientHandle::cClientHandle(const cSocket & a_Socket, int a_ViewDistance) : m_ViewDistance(a_ViewDistance) , m_ProtocolVersion(MCS_PROTOCOL_VERSION) + , m_ReceivedData(64 KiB) , m_Socket(a_Socket) , m_bDestroyed(false) , m_Player(NULL) @@ -1357,6 +1358,16 @@ void cClientHandle::Send(const cPacket & a_Packet, ENUM_PRIORITY a_Priority /* = +void cClientHandle::SendDisconnect(const AString & a_Reason) +{ + cPacket_Disconnect DC(a_Reason); + m_Socket.Send(&DC); +} + + + + + void cClientHandle::CheckIfWorldDownloaded(void) { if (m_State != csDownloadingWorld) @@ -1453,32 +1464,43 @@ void cClientHandle::AddWantedChunk(int a_ChunkX, int a_ChunkZ) void cClientHandle::DataReceived(const char * a_Data, int a_Size) { // Data is received from the client - - m_ReceivedData.append(a_Data, a_Size); + + if (!m_ReceivedData.Write(a_Data, a_Size)) + { + // Too much data in the incoming queue, the server is probably too busy, kick the client: + LOGERROR("Too much data in queue for client \"%s\" @ %s, kicking them.", m_Username.c_str(), m_Socket.GetIPString().c_str()); + SendDisconnect("Server busy"); + // TODO: QueueDestroy(); + cSleep::MilliSleep(1000); // Give packet some time to be received + Destroy(); + return; + } // Parse and handle all complete packets in m_ReceivedData: - while (!m_ReceivedData.empty()) + while (m_ReceivedData.CanReadBytes(1)) { - cPacket* pPacket = m_PacketMap[(unsigned char)m_ReceivedData[0]]; + unsigned char PacketType; + m_ReceivedData.ReadByte(PacketType); + cPacket* pPacket = m_PacketMap[PacketType]; if (pPacket == NULL) { - LOGERROR("Unknown packet type 0x%02x from client \"%s\"", (unsigned char)m_ReceivedData[0], m_Username.c_str()); + LOGERROR("Unknown packet type 0x%02x from client \"%s\" @ %s", PacketType, m_Username.c_str(), m_Socket.GetIPString().c_str()); AString Reason; - Printf(Reason, "[C->S] Unknown PacketID: 0x%02x", (unsigned char)m_ReceivedData[0]); - cPacket_Disconnect DC(Reason); - m_Socket.Send(&DC); + Printf(Reason, "[C->S] Unknown PacketID: 0x%02x", PacketType); + SendDisconnect(Reason); + // TODO: QueueDestroy(); cSleep::MilliSleep(1000); // Give packet some time to be received Destroy(); return; } - int NumBytes = pPacket->Parse(m_ReceivedData.data() + 1, m_ReceivedData.size() - 1); + int NumBytes = pPacket->Parse(m_ReceivedData); if (NumBytes == PACKET_ERROR) { - LOGERROR("Protocol error while parsing packet type 0x%02x; disconnecting client \"%s\"", (unsigned char)m_ReceivedData[0], m_Username.c_str()); - cPacket_Disconnect DC("Protocol error"); - m_Socket.Send(&DC); + LOGERROR("Protocol error while parsing packet type 0x%02x; disconnecting client \"%s\"", PacketType, m_Username.c_str()); + SendDisconnect("Protocol error"); + // TODO: QueueDestroy(); cSleep::MilliSleep(1000); // Give packet some time to be received Destroy(); return; @@ -1486,6 +1508,7 @@ void cClientHandle::DataReceived(const char * a_Data, int a_Size) else if (NumBytes == PACKET_INCOMPLETE) { // Not a complete packet + m_ReceivedData.ResetRead(); break; } else @@ -1493,10 +1516,9 @@ void cClientHandle::DataReceived(const char * a_Data, int a_Size) // Packet parsed successfully, add it to internal queue: HandlePacket(pPacket); // Erase the packet from the buffer: - ASSERT(m_ReceivedData.size() > (size_t)NumBytes); - m_ReceivedData.erase(0, NumBytes + 1); + m_ReceivedData.CommitRead(); } - } // while (!Received.empty()) + } // while (!Received.CanReadBytes(1)) } diff --git a/source/cClientHandle.h b/source/cClientHandle.h index 790bcc5b2..aee835337 100644 --- a/source/cClientHandle.h +++ b/source/cClientHandle.h @@ -39,6 +39,7 @@ #include "packets/cPacket_WindowClose.h" #include "packets/cPacket_UpdateSign.h" #include "packets/cPacket_Ping.h" +#include "ByteBuffer.h" @@ -100,6 +101,8 @@ public: bool IsPlaying(void) const {return (m_State == csPlaying); } void Send(const cPacket & a_Packet, ENUM_PRIORITY a_Priority = E_PRIORITY_NORMAL); + + void SendDisconnect(const AString & a_Reason); const AString & GetUsername(void) const; //tolua_export @@ -122,11 +125,11 @@ private: static const int GENERATEDISTANCE = 2; // Server generates this many chunks AHEAD of player sight. 2 is the minimum, since foliage is generated 1 step behind chunk terrain generation - int m_ProtocolVersion; + int m_ProtocolVersion; AString m_Username; AString m_Password; - AString m_ReceivedData; // Accumulator for the data received from the socket, waiting to be parsed; accessed from the cSocketThreads' thread only! + cByteBuffer m_ReceivedData; // Accumulator for the data received from the socket, waiting to be parsed; accessed from the cSocketThreads' thread only! cCriticalSection m_CSPackets; PacketList m_PendingNrmSendPackets; diff --git a/source/packets/cPacket.cpp b/source/packets/cPacket.cpp index 6fad331ed..8307e67fa 100644 --- a/source/packets/cPacket.cpp +++ b/source/packets/cPacket.cpp @@ -28,160 +28,6 @@ -int cPacket::ReadString16(const char * a_Data, int a_Size, AString & a_OutString ) -{ - int TotalBytes = 0; - short StrLen; - HANDLE_PACKET_READ(ReadShort, StrLen, TotalBytes); - - if (2 * StrLen > a_Size - TotalBytes) - { - // The string is not yet complete in the buffer - return PACKET_INCOMPLETE; - } - - // Simple UTF-16 to UTF-8 conversion - discard higher bits, ignore multishort sequences: - a_OutString.clear(); - a_OutString.reserve(StrLen); - short * UTF16 = (short *)(a_Data + TotalBytes); - for ( int i = 0; i < StrLen; ++i ) - { - a_OutString.push_back( (char)ntohs(UTF16[i]) ); - } - - return TotalBytes + StrLen * sizeof(short); -} - - - - - -int cPacket::ReadShort(const char * a_Data, int a_Size, short & a_OutShort ) -{ - if (a_Size < 2) - { - return PACKET_INCOMPLETE; - } - a_OutShort = ntohs(*((short *)a_Data)); - return 2; -} - - - - - -int cPacket::ReadInteger(const char * a_Data, int a_Size, int & a_OutInteger ) -{ - if (a_Size < 4) - { - return PACKET_INCOMPLETE; - } - a_OutInteger = ntohl(*((int *)a_Data)); - return 4; -} - - - - - -int cPacket::ReadInteger(const char * a_Data, int a_Size, unsigned int & a_OutInteger ) -{ - if (a_Size < 4) - { - return PACKET_INCOMPLETE; - } - a_OutInteger = ntohl(*((unsigned int *)a_Data)); - return 4; -} - - - - - -int cPacket::ReadFloat(const char * a_Data, int a_Size, float & a_OutFloat ) -{ - if (a_Size < sizeof(float)) - { - return PACKET_INCOMPLETE; - } - a_OutFloat = NetworkToHostFloat4(a_Data); - return sizeof(float); -} - - - - - -int cPacket::ReadDouble(const char * a_Data, int a_Size, double & a_OutDouble ) -{ - if (a_Size < sizeof(double)) - { - return PACKET_INCOMPLETE; - } - a_OutDouble = NetworkToHostDouble8(a_Data); - return sizeof(double); -} - - - - - -int cPacket::ReadByte(const char * a_Data, int a_Size, char & a_OutByte ) -{ - if (a_Size < 1) - { - return PACKET_INCOMPLETE; - } - a_OutByte = *a_Data; - return 1; -} - - - - - -int cPacket::ReadByte(const char * a_Data, int a_Size, unsigned char & a_OutByte ) -{ - if (a_Size < 1) - { - return PACKET_INCOMPLETE; - } - a_OutByte = *((unsigned char *)a_Data); - return 1; -} - - - - - -int cPacket::ReadLong(const char * a_Data, int a_Size, long long & a_OutLong ) -{ - if (a_Size < sizeof(a_OutLong)) - { - return PACKET_INCOMPLETE; - } - a_OutLong = NetworkToHostLong8(a_Data); - return sizeof(a_OutLong); -} - - - - - -int cPacket::ReadBool(const char * a_Data, int a_Size, bool & a_OutBool ) -{ - if (a_Size < sizeof(bool)) - { - return PACKET_INCOMPLETE; - } - a_OutBool = (*a_Data != 0); - return sizeof(bool); -} - - - - - void cPacket::AppendString(AString & a_Dst, const AString & a_String) { AppendShort(a_Dst, (unsigned short)a_String.size()); diff --git a/source/packets/cPacket.h b/source/packets/cPacket.h index 1c0b24868..1d6e8506f 100644 --- a/source/packets/cPacket.h +++ b/source/packets/cPacket.h @@ -3,6 +3,7 @@ #include "../cSocket.h" #include "../PacketID.h" +#include "../ByteBuffer.h" @@ -10,6 +11,7 @@ #define PACKET_INCOMPLETE -2 #define PACKET_ERROR -1 +#define PACKET_OK 1 @@ -18,12 +20,11 @@ // Use this macro to simplify handling several ReadXXX in a row. It assumes that you want [a_Data, a_Size] parsed (which is true for all Parse() functions) #define HANDLE_PACKET_READ(Proc, Var, TotalBytes) \ { \ - int res = Proc(a_Data + TotalBytes, a_Size - TotalBytes, Var); \ - if (res < 0) \ + if (!a_Buffer.Proc(Var)) \ { \ - return res; \ + return PACKET_INCOMPLETE; \ } \ - TotalBytes += res; \ + TotalBytes = PACKET_OK; \ } @@ -38,22 +39,23 @@ public: {} virtual ~cPacket() {} - /// Called to parse the packet. Packet type has already been read and the correct packet type created. Return the number of characters processed, PACKET_INCOMPLETE for incomplete data, PACKET_ERROR for error - virtual int Parse(const char * a_Data, int a_Size) + /// Called to parse the packet. Packet type has already been read and the correct packet type created. Return PACKET_INCOMPLETE for incomplete data, PACKET_ERROR for error, any positive number for success + virtual int Parse(cByteBuffer & a_Buffer) { - UNUSED(a_Data); - UNUSED(a_Size); - LOGERROR("Undefined Parse function for packet type 0x%x\n", m_PacketID ); - ASSERT(!"Undefined Parse function"); - return -1; + // There are packets that are sent S->C only, those don't have a parsing function + UNUSED(a_Buffer); + LOGERROR("Packet type 0x%02x has no parser defined!", m_PacketID); + ASSERT(!"Unparsed packet type!"); + return PACKET_ERROR; } /// Called to serialize the packet into a string. Append all packet data to a_Data, including the packet type! virtual void Serialize(AString & a_Data) const { + // There are packets that are sent C->S only, those don't have a serializing function UNUSED(a_Data); - LOGERROR("Undefined Serialize function for packet type 0x%x\n", m_PacketID ); - ASSERT(!"Undefined Serialize function"); + LOGERROR("Packet type 0x%02x has no serializer defined!", m_PacketID); + ASSERT(!"Unserialized packet"); } virtual cPacket * Clone() const = 0; @@ -61,19 +63,6 @@ public: unsigned char m_PacketID; protected: - - // These return the number of characters processed, PACKET_INCOMPLETE for incomplete data, PACKET_ERROR for error: - static int ReadString16(const char * a_Data, int a_Size, AString & a_OutString ); - static int ReadShort (const char * a_Data, int a_Size, short & a_Short ); - static int ReadInteger (const char * a_Data, int a_Size, int & a_OutInteger ); - static int ReadInteger (const char * a_Data, int a_Size, unsigned int & a_OutInteger ); - static int ReadFloat (const char * a_Data, int a_Size, float & a_OutFloat ); - static int ReadDouble (const char * a_Data, int a_Size, double & a_OutDouble ); - static int ReadByte (const char * a_Data, int a_Size, char & a_OutByte ); - static int ReadByte (const char * a_Data, int a_Size, unsigned char & a_OutByte ); - static int ReadLong (const char * a_Data, int a_Size, long long & a_OutLong ); - static int ReadBool (const char * a_Data, int a_Size, bool & a_OutBool ); - // These append the data into the a_Dst string: static void AppendString ( AString & a_Dst, const AString & a_String); static void AppendString16( AString & a_Dst, const AString & a_String); diff --git a/source/packets/cPacket_13.cpp b/source/packets/cPacket_13.cpp index 4edeb8c95..f31c67704 100644 --- a/source/packets/cPacket_13.cpp +++ b/source/packets/cPacket_13.cpp @@ -7,11 +7,11 @@ -int cPacket_13::Parse(const char * a_Data, int a_Size) +int cPacket_13::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadInteger, m_EntityID, TotalBytes); - HANDLE_PACKET_READ(ReadByte , m_ActionID, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_EntityID, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_ActionID, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_13.h b/source/packets/cPacket_13.h index d0a257302..24caf5cc0 100644 --- a/source/packets/cPacket_13.h +++ b/source/packets/cPacket_13.h @@ -21,7 +21,7 @@ public: { m_PacketID = E_PACKET_13; } virtual cPacket* Clone() const { return new cPacket_13( *this ); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; int m_EntityID; char m_ActionID; diff --git a/source/packets/cPacket_ArmAnim.cpp b/source/packets/cPacket_ArmAnim.cpp index dcd416449..6f7a261d4 100644 --- a/source/packets/cPacket_ArmAnim.cpp +++ b/source/packets/cPacket_ArmAnim.cpp @@ -7,11 +7,11 @@ -int cPacket_ArmAnim::Parse(const char * a_Data, int a_Size) +int cPacket_ArmAnim::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadInteger, m_EntityID, TotalBytes); - HANDLE_PACKET_READ(ReadByte , m_Animation, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_EntityID, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Animation, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_ArmAnim.h b/source/packets/cPacket_ArmAnim.h index 79670de1c..2fa613b83 100644 --- a/source/packets/cPacket_ArmAnim.h +++ b/source/packets/cPacket_ArmAnim.h @@ -12,7 +12,7 @@ public: { m_PacketID = E_ANIMATION; } virtual cPacket* Clone() const { return new cPacket_ArmAnim(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; int m_EntityID; diff --git a/source/packets/cPacket_BlockDig.cpp b/source/packets/cPacket_BlockDig.cpp index be2088cd0..0c90436dd 100644 --- a/source/packets/cPacket_BlockDig.cpp +++ b/source/packets/cPacket_BlockDig.cpp @@ -20,14 +20,14 @@ void cPacket_BlockDig::Serialize(AString & a_Data) const -int cPacket_BlockDig::Parse(const char * a_Data, int a_Size) +int cPacket_BlockDig::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadByte, m_Status, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_PosX, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_PosY, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_PosZ, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_Direction, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Status, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Direction, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_BlockDig.h b/source/packets/cPacket_BlockDig.h index 1fde63db6..e1bdc14f1 100644 --- a/source/packets/cPacket_BlockDig.h +++ b/source/packets/cPacket_BlockDig.h @@ -15,7 +15,7 @@ public: { m_PacketID = E_BLOCK_DIG; } //tolua_export virtual cPacket* Clone() const { return new cPacket_BlockDig(*this); } //tolua_export - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; char m_Status; // tolua_export diff --git a/source/packets/cPacket_BlockPlace.cpp b/source/packets/cPacket_BlockPlace.cpp index 2033bd91e..f767456a2 100644 --- a/source/packets/cPacket_BlockPlace.cpp +++ b/source/packets/cPacket_BlockPlace.cpp @@ -8,16 +8,16 @@ -int cPacket_BlockPlace::Parse(const char * a_Data, int a_Size) +int cPacket_BlockPlace::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadInteger, m_PosX, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_PosY, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_PosZ, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_Direction, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Direction, TotalBytes); cPacket_ItemData Item; - int res = Item.Parse(a_Data + TotalBytes, a_Size - TotalBytes); + int res = Item.Parse(a_Buffer); if (res < 0) { return res; diff --git a/source/packets/cPacket_BlockPlace.h b/source/packets/cPacket_BlockPlace.h index b8db56fd0..fa8aae585 100644 --- a/source/packets/cPacket_BlockPlace.h +++ b/source/packets/cPacket_BlockPlace.h @@ -21,7 +21,7 @@ public: { m_PacketID = E_BLOCK_PLACE; } virtual cPacket* Clone() const { return new cPacket_BlockPlace(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; int m_PosX; //tolua_export unsigned char m_PosY; //tolua_export diff --git a/source/packets/cPacket_Chat.cpp b/source/packets/cPacket_Chat.cpp index 43b1af3fa..da00c0aae 100644 --- a/source/packets/cPacket_Chat.cpp +++ b/source/packets/cPacket_Chat.cpp @@ -7,10 +7,10 @@ -int cPacket_Chat::Parse(const char * a_Data, int a_Size) +int cPacket_Chat::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadString16, m_Message, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_Message, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_Chat.h b/source/packets/cPacket_Chat.h index fac4a4097..cb154f732 100644 --- a/source/packets/cPacket_Chat.h +++ b/source/packets/cPacket_Chat.h @@ -10,7 +10,7 @@ public: cPacket_Chat( const std::string & a_Message ) : m_Message( a_Message) { m_PacketID = E_CHAT; } virtual cPacket* Clone() const { return new cPacket_Chat(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; AString m_Message; diff --git a/source/packets/cPacket_CreativeInventoryAction.cpp b/source/packets/cPacket_CreativeInventoryAction.cpp index 46f125701..a71585df3 100644 --- a/source/packets/cPacket_CreativeInventoryAction.cpp +++ b/source/packets/cPacket_CreativeInventoryAction.cpp @@ -21,13 +21,13 @@ cPacket_CreativeInventoryAction::cPacket_CreativeInventoryAction( const cPacket_ -int cPacket_CreativeInventoryAction::Parse(const char * a_Data, int a_Size) +int cPacket_CreativeInventoryAction::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadShort, m_Slot, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_Slot, TotalBytes); cPacket_ItemData Item; - int res = Item.Parse(a_Data + TotalBytes, a_Size - TotalBytes); + int res = Item.Parse(a_Buffer); if (res < 0) { return res; diff --git a/source/packets/cPacket_CreativeInventoryAction.h b/source/packets/cPacket_CreativeInventoryAction.h index 4fbed4e71..4d983e51d 100644 --- a/source/packets/cPacket_CreativeInventoryAction.h +++ b/source/packets/cPacket_CreativeInventoryAction.h @@ -21,7 +21,7 @@ public: cPacket_CreativeInventoryAction( const cPacket_CreativeInventoryAction & a_Copy ); virtual cPacket* Clone() const { return new cPacket_CreativeInventoryAction(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; short m_Slot; // 0 = hold 1-4 = armor diff --git a/source/packets/cPacket_Disconnect.cpp b/source/packets/cPacket_Disconnect.cpp index 0ddad3744..19eecd365 100644 --- a/source/packets/cPacket_Disconnect.cpp +++ b/source/packets/cPacket_Disconnect.cpp @@ -7,10 +7,10 @@ -int cPacket_Disconnect::Parse(const char * a_Data, int a_Size) +int cPacket_Disconnect::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadString16, m_Reason, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_Reason, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_Disconnect.h b/source/packets/cPacket_Disconnect.h index e91a09980..f2a820607 100644 --- a/source/packets/cPacket_Disconnect.h +++ b/source/packets/cPacket_Disconnect.h @@ -14,7 +14,7 @@ public: cPacket_Disconnect(const AString & a_Reason) { m_PacketID = E_DISCONNECT; m_Reason = a_Reason; } virtual cPacket* Clone() const { return new cPacket_Disconnect(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; AString m_Reason; diff --git a/source/packets/cPacket_EntityEquipment.cpp b/source/packets/cPacket_EntityEquipment.cpp index 08bf30a6e..c66b1b6b4 100644 --- a/source/packets/cPacket_EntityEquipment.cpp +++ b/source/packets/cPacket_EntityEquipment.cpp @@ -20,13 +20,13 @@ cPacket_EntityEquipment::cPacket_EntityEquipment( const cPacket_EntityEquipment -int cPacket_EntityEquipment::Parse(const char * a_Data, int a_Size) +int cPacket_EntityEquipment::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadInteger, m_UniqueID, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_Slot, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_ItemID, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_Short, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_UniqueID, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_Slot, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_ItemID, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_Short, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_EntityEquipment.h b/source/packets/cPacket_EntityEquipment.h index ec09bbf5c..3c6197f32 100644 --- a/source/packets/cPacket_EntityEquipment.h +++ b/source/packets/cPacket_EntityEquipment.h @@ -18,7 +18,7 @@ public: cPacket_EntityEquipment( const cPacket_EntityEquipment & a_Copy ); virtual cPacket* Clone() const { return new cPacket_EntityEquipment(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; int m_UniqueID; diff --git a/source/packets/cPacket_Flying.cpp b/source/packets/cPacket_Flying.cpp index 64fbf11ea..bc730cb5e 100644 --- a/source/packets/cPacket_Flying.cpp +++ b/source/packets/cPacket_Flying.cpp @@ -7,7 +7,7 @@ -int cPacket_Flying::Parse(const char * a_Data, int a_Size) +int cPacket_Flying::Parse(cByteBuffer & a_Buffer) { int TotalBytes= 0; HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); diff --git a/source/packets/cPacket_Flying.h b/source/packets/cPacket_Flying.h index 2d19820de..52f33e7fd 100644 --- a/source/packets/cPacket_Flying.h +++ b/source/packets/cPacket_Flying.h @@ -16,7 +16,7 @@ public: { m_PacketID = E_FLYING; } virtual cPacket* Clone() const { return new cPacket_Flying(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; bool m_bFlying; static const unsigned int c_Size = 2; diff --git a/source/packets/cPacket_Handshake.cpp b/source/packets/cPacket_Handshake.cpp index 737eca330..76d04883c 100644 --- a/source/packets/cPacket_Handshake.cpp +++ b/source/packets/cPacket_Handshake.cpp @@ -7,10 +7,10 @@ -int cPacket_Handshake::Parse(const char * a_Data, int a_Size) +int cPacket_Handshake::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadString16, m_Username, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_Username, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_Handshake.h b/source/packets/cPacket_Handshake.h index 020be9eaf..d1b06dacc 100644 --- a/source/packets/cPacket_Handshake.h +++ b/source/packets/cPacket_Handshake.h @@ -13,7 +13,7 @@ public: cPacket_Handshake() { m_PacketID = E_HANDSHAKE; } virtual cPacket* Clone() const { return new cPacket_Handshake(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; std::string m_Username; diff --git a/source/packets/cPacket_ItemData.cpp b/source/packets/cPacket_ItemData.cpp index 0892f433a..2e4203bfc 100644 --- a/source/packets/cPacket_ItemData.cpp +++ b/source/packets/cPacket_ItemData.cpp @@ -7,10 +7,10 @@ -int cPacket_ItemData::Parse(const char * a_Data, int a_Size) +int cPacket_ItemData::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadShort, m_ItemID, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_ItemID, TotalBytes); if (m_ItemID <= -1) { @@ -19,12 +19,12 @@ int cPacket_ItemData::Parse(const char * a_Data, int a_Size) return TotalBytes; } - HANDLE_PACKET_READ(ReadByte , m_ItemCount, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_ItemUses, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_ItemCount, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_ItemUses, TotalBytes); if (cItem::IsEnchantable((ENUM_ITEM_ID) m_ItemID)) { - HANDLE_PACKET_READ(ReadShort, m_EnchantNums, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_EnchantNums, TotalBytes); if ( m_EnchantNums > -1 ) { diff --git a/source/packets/cPacket_ItemData.h b/source/packets/cPacket_ItemData.h index 47878969f..5a1554b19 100644 --- a/source/packets/cPacket_ItemData.h +++ b/source/packets/cPacket_ItemData.h @@ -17,7 +17,7 @@ public: virtual cPacket* Clone() const { return new cPacket_ItemData(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; static void AppendItem(AString & a_Data, short a_ItemID, char a_Quantity, short a_Damage); static void AppendItem(AString & a_Data, const cItem * a_Item); diff --git a/source/packets/cPacket_ItemSwitch.cpp b/source/packets/cPacket_ItemSwitch.cpp index bd4532938..3ea9d26c2 100644 --- a/source/packets/cPacket_ItemSwitch.cpp +++ b/source/packets/cPacket_ItemSwitch.cpp @@ -7,10 +7,10 @@ -int cPacket_ItemSwitch::Parse(const char * a_Data, int a_Size) +int cPacket_ItemSwitch::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadShort, m_SlotNum, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_SlotNum, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_ItemSwitch.h b/source/packets/cPacket_ItemSwitch.h index fb6860624..1b0323ea6 100644 --- a/source/packets/cPacket_ItemSwitch.h +++ b/source/packets/cPacket_ItemSwitch.h @@ -15,7 +15,7 @@ public: { m_PacketID = E_ITEM_SWITCH; } virtual cPacket* Clone() const { return new cPacket_ItemSwitch(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; short m_SlotNum; diff --git a/source/packets/cPacket_KeepAlive.cpp b/source/packets/cPacket_KeepAlive.cpp index cfc6b99b3..0dc9302b3 100644 --- a/source/packets/cPacket_KeepAlive.cpp +++ b/source/packets/cPacket_KeepAlive.cpp @@ -17,10 +17,10 @@ void cPacket_KeepAlive::Serialize(AString & a_Data) const -int cPacket_KeepAlive::Parse(const char * a_Data, int a_Size) +int cPacket_KeepAlive::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadInteger, m_KeepAliveID, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_KeepAliveID, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_KeepAlive.h b/source/packets/cPacket_KeepAlive.h index e94ec72a6..0eeb5e095 100644 --- a/source/packets/cPacket_KeepAlive.h +++ b/source/packets/cPacket_KeepAlive.h @@ -14,7 +14,7 @@ public: cPacket_KeepAlive(int a_PingID) { m_KeepAliveID = a_PingID; } virtual cPacket* Clone() const { return new cPacket_KeepAlive(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; int m_KeepAliveID; diff --git a/source/packets/cPacket_Login.cpp b/source/packets/cPacket_Login.cpp index 2a8ae7135..90da4c009 100644 --- a/source/packets/cPacket_Login.cpp +++ b/source/packets/cPacket_Login.cpp @@ -14,19 +14,19 @@ const std::string cPacket_Login::LEVEL_TYPE_SUPERFLAT = "SUPERFLAT"; -int cPacket_Login::Parse(const char * a_Data, int a_Size) +int cPacket_Login::Parse(cByteBuffer & a_Buffer) { //printf("Parse: NEW Login\n"); int TotalBytes = 0; m_Username.clear(); - HANDLE_PACKET_READ(ReadInteger, m_ProtocolVersion, TotalBytes); - HANDLE_PACKET_READ(ReadString16, m_Username, TotalBytes); - HANDLE_PACKET_READ(ReadString16, m_LevelType, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_ServerMode, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_Dimension, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_Difficulty, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_WorldHeight, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_MaxPlayers, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_ProtocolVersion, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_Username, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_LevelType, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_ServerMode, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_Dimension, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Difficulty, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_WorldHeight, TotalBytes); + HANDLE_PACKET_READ(ReadByte, m_MaxPlayers, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_Login.h b/source/packets/cPacket_Login.h index b9769fdec..f512b098a 100644 --- a/source/packets/cPacket_Login.h +++ b/source/packets/cPacket_Login.h @@ -21,7 +21,7 @@ public: { m_PacketID = E_LOGIN; } virtual cPacket* Clone() const { return new cPacket_Login(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; int m_ProtocolVersion; //tolua_export diff --git a/source/packets/cPacket_NewInvalidState.cpp b/source/packets/cPacket_NewInvalidState.cpp index a243a0e51..3e44a4877 100644 --- a/source/packets/cPacket_NewInvalidState.cpp +++ b/source/packets/cPacket_NewInvalidState.cpp @@ -18,11 +18,11 @@ cPacket_NewInvalidState::cPacket_NewInvalidState( const cPacket_NewInvalidState -int cPacket_NewInvalidState::Parse(const char * a_Data, int a_Size) +int cPacket_NewInvalidState::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadByte, m_Reason, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_GameMode, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Reason, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_GameMode, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_NewInvalidState.h b/source/packets/cPacket_NewInvalidState.h index 2c7565e63..6bfacd17c 100644 --- a/source/packets/cPacket_NewInvalidState.h +++ b/source/packets/cPacket_NewInvalidState.h @@ -17,7 +17,7 @@ public: cPacket_NewInvalidState( const cPacket_NewInvalidState & a_Copy ); virtual cPacket* Clone() const { return new cPacket_NewInvalidState(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; char m_Reason; // 0 = Invalid Bed, 1 = Begin Raining, 2 End Raining, 3 = Change Gamemode diff --git a/source/packets/cPacket_PickupSpawn.cpp b/source/packets/cPacket_PickupSpawn.cpp index a1a60947d..0a858ba76 100644 --- a/source/packets/cPacket_PickupSpawn.cpp +++ b/source/packets/cPacket_PickupSpawn.cpp @@ -7,19 +7,19 @@ -int cPacket_PickupSpawn::Parse(const char * a_Data, int a_Size) +int cPacket_PickupSpawn::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadInteger, m_UniqueID, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_Item, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_Count, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_Health, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_PosX, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_PosY, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_PosZ, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_Rotation, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_Pitch, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_Roll, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_UniqueID, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_Item, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Count, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_Health, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Rotation, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Pitch, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Roll, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_PickupSpawn.h b/source/packets/cPacket_PickupSpawn.h index 1c1a6c640..ea004b730 100644 --- a/source/packets/cPacket_PickupSpawn.h +++ b/source/packets/cPacket_PickupSpawn.h @@ -24,7 +24,7 @@ public: { m_PacketID = E_PICKUP_SPAWN; } virtual cPacket* Clone() const { return new cPacket_PickupSpawn(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; int m_UniqueID; diff --git a/source/packets/cPacket_Ping.h b/source/packets/cPacket_Ping.h index 0a856c7ec..f74385a81 100644 --- a/source/packets/cPacket_Ping.h +++ b/source/packets/cPacket_Ping.h @@ -14,7 +14,7 @@ public: { m_PacketID = E_PING; } virtual cPacket* Clone() const { return new cPacket_Ping(*this); } - virtual int Parse(const char * a_Data, int a_Size) override {return 0; } + virtual int Parse(cByteBuffer & a_Buffer) override {return 0; } static const unsigned int c_Size = 1; }; diff --git a/source/packets/cPacket_Player.cpp b/source/packets/cPacket_Player.cpp index 7d9a5de6b..a71cdee33 100644 --- a/source/packets/cPacket_Player.cpp +++ b/source/packets/cPacket_Player.cpp @@ -22,17 +22,14 @@ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cPacket_PlayerAbilities: -int cPacket_PlayerAbilities::Parse(const char * a_Data, int a_Size) +int cPacket_PlayerAbilities::Parse(cByteBuffer & a_Buffer) { - if (a_Size < 4) - { - return PACKET_INCOMPLETE; - } - m_Invulnerable = (a_Data[0] != 0); - m_IsFlying = (a_Data[1] != 0); - m_CanFly = (a_Data[2] != 0); - m_InstaMine = (a_Data[3] != 0); - return 4; + int TotalBytes = 0; + HANDLE_PACKET_READ(ReadBool, m_Invulnerable, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_IsFlying, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_CanFly, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_InstaMine, TotalBytes); + return TotalBytes; } @@ -69,12 +66,12 @@ cPacket_PlayerListItem::cPacket_PlayerListItem(const AString & a_PlayerName, boo -int cPacket_PlayerListItem::Parse(const char * a_Data, int a_Size) +int cPacket_PlayerListItem::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadString16, m_PlayerName, TotalBytes); - HANDLE_PACKET_READ(ReadBool, m_Online, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_Ping, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_PlayerName, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_Online, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_Ping, TotalBytes); return TotalBytes; } @@ -119,12 +116,12 @@ cPacket_PlayerLook::cPacket_PlayerLook( cPlayer* a_Player ) -int cPacket_PlayerLook::Parse(const char * a_Data, int a_Size) +int cPacket_PlayerLook::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadFloat, m_Rotation, TotalBytes); - HANDLE_PACKET_READ(ReadFloat, m_Pitch, TotalBytes); - HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); + HANDLE_PACKET_READ(ReadBEFloat, m_Rotation, TotalBytes); + HANDLE_PACKET_READ(ReadBEFloat, m_Pitch, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); return TotalBytes; } @@ -162,16 +159,16 @@ cPacket_PlayerMoveLook::cPacket_PlayerMoveLook( cPlayer* a_Player ) -int cPacket_PlayerMoveLook::Parse(const char * a_Data, int a_Size) +int cPacket_PlayerMoveLook::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadDouble, m_PosX, TotalBytes); - HANDLE_PACKET_READ(ReadDouble, m_PosY, TotalBytes); - HANDLE_PACKET_READ(ReadDouble, m_Stance, TotalBytes); - HANDLE_PACKET_READ(ReadDouble, m_PosZ, TotalBytes); - HANDLE_PACKET_READ(ReadFloat, m_Rotation, TotalBytes); - HANDLE_PACKET_READ(ReadFloat, m_Pitch, TotalBytes); - HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); + HANDLE_PACKET_READ(ReadBEDouble, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadBEDouble, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadBEDouble, m_Stance, TotalBytes); + HANDLE_PACKET_READ(ReadBEDouble, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadBEFloat, m_Rotation, TotalBytes); + HANDLE_PACKET_READ(ReadBEFloat, m_Pitch, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); return TotalBytes; } @@ -213,14 +210,14 @@ cPacket_PlayerPosition::cPacket_PlayerPosition( cPlayer* a_Player ) -int cPacket_PlayerPosition::Parse(const char * a_Data, int a_Size) +int cPacket_PlayerPosition::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadDouble, m_PosX, TotalBytes); - HANDLE_PACKET_READ(ReadDouble, m_PosY, TotalBytes); - HANDLE_PACKET_READ(ReadDouble, m_Stance, TotalBytes); - HANDLE_PACKET_READ(ReadDouble, m_PosZ, TotalBytes); - HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); + HANDLE_PACKET_READ(ReadBEDouble, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadBEDouble, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadBEDouble, m_Stance, TotalBytes); + HANDLE_PACKET_READ(ReadBEDouble, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_bFlying, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_Player.h b/source/packets/cPacket_Player.h index 5b17feabf..8dadcdaea 100644 --- a/source/packets/cPacket_Player.h +++ b/source/packets/cPacket_Player.h @@ -33,7 +33,7 @@ class cPacket_PlayerAbilities : public cPacket public: cPacket_PlayerAbilities(void) { m_PacketID = E_PLAYER_LIST_ITEM; } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; virtual cPacket * Clone() const { return new cPacket_PlayerAbilities(*this); } @@ -54,7 +54,7 @@ public: cPacket_PlayerListItem() { m_PacketID = E_PLAYER_LIST_ITEM; } cPacket_PlayerListItem(const AString & a_PlayerName, bool a_Online, short a_Ping); - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; virtual cPacket* Clone() const { return new cPacket_PlayerListItem(*this); } @@ -79,7 +79,7 @@ public: cPacket_PlayerLook( cPlayer* a_Player ); virtual cPacket* Clone() const { return new cPacket_PlayerLook(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; float m_Rotation; @@ -106,7 +106,7 @@ public: cPacket_PlayerMoveLook( cPlayer* a_Player ); virtual cPacket* Clone() const { return new cPacket_PlayerMoveLook(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; double m_PosX; @@ -135,7 +135,7 @@ public: { m_PacketID = E_PLAYERPOS; } virtual cPacket* Clone() const { return new cPacket_PlayerPosition(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; double m_PosX; diff --git a/source/packets/cPacket_Respawn.cpp b/source/packets/cPacket_Respawn.cpp index 657fd70b5..94c757003 100644 --- a/source/packets/cPacket_Respawn.cpp +++ b/source/packets/cPacket_Respawn.cpp @@ -22,15 +22,15 @@ void cPacket_Respawn::Serialize(AString & a_Data) const -int cPacket_Respawn::Parse(const char * a_Data, int a_Size) +int cPacket_Respawn::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadInteger, m_Dimension, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_Difficulty, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_CreativeMode, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_WorldHeight, TotalBytes); - HANDLE_PACKET_READ(ReadString16, m_LevelType, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_Dimension, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Difficulty, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_CreativeMode, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_WorldHeight, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_LevelType, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_Respawn.h b/source/packets/cPacket_Respawn.h index 302dbe25e..d51869cda 100644 --- a/source/packets/cPacket_Respawn.h +++ b/source/packets/cPacket_Respawn.h @@ -22,7 +22,7 @@ public: virtual cPacket* Clone() const { return new cPacket_Respawn( *this ); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; int m_Dimension; diff --git a/source/packets/cPacket_TimeUpdate.cpp b/source/packets/cPacket_TimeUpdate.cpp index be54e1484..1195305d4 100644 --- a/source/packets/cPacket_TimeUpdate.cpp +++ b/source/packets/cPacket_TimeUpdate.cpp @@ -7,10 +7,10 @@ -int cPacket_TimeUpdate::Parse(const char * a_Data, int a_Size) +int cPacket_TimeUpdate::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadLong, m_Time, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt64, m_Time, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_TimeUpdate.h b/source/packets/cPacket_TimeUpdate.h index 8864c9b00..9aaa02cae 100644 --- a/source/packets/cPacket_TimeUpdate.h +++ b/source/packets/cPacket_TimeUpdate.h @@ -16,7 +16,7 @@ public: cPacket_TimeUpdate( long long a_Time ) { m_PacketID = E_UPDATE_TIME; m_Time = a_Time; } virtual cPacket* Clone() const { return new cPacket_TimeUpdate(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; long long m_Time; diff --git a/source/packets/cPacket_UpdateSign.cpp b/source/packets/cPacket_UpdateSign.cpp index a3ea53968..3ef587f13 100644 --- a/source/packets/cPacket_UpdateSign.cpp +++ b/source/packets/cPacket_UpdateSign.cpp @@ -7,16 +7,16 @@ -int cPacket_UpdateSign::Parse(const char * a_Data, int a_Size) +int cPacket_UpdateSign::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadInteger, m_PosX, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_PosY, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_PosZ, TotalBytes); - HANDLE_PACKET_READ(ReadString16, m_Line1, TotalBytes); - HANDLE_PACKET_READ(ReadString16, m_Line2, TotalBytes); - HANDLE_PACKET_READ(ReadString16, m_Line3, TotalBytes); - HANDLE_PACKET_READ(ReadString16, m_Line4, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_PosX, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_PosY, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_PosZ, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_Line1, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_Line2, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_Line3, TotalBytes); + HANDLE_PACKET_READ(ReadBEUTF16String16, m_Line4, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_UpdateSign.h b/source/packets/cPacket_UpdateSign.h index b2c0effad..9c3790c77 100644 --- a/source/packets/cPacket_UpdateSign.h +++ b/source/packets/cPacket_UpdateSign.h @@ -17,7 +17,7 @@ public: { m_PacketID = E_UPDATE_SIGN; } virtual cPacket* Clone() const { return new cPacket_UpdateSign( *this ); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; int m_PosX; diff --git a/source/packets/cPacket_UseEntity.cpp b/source/packets/cPacket_UseEntity.cpp index 6f296e96b..73b4f7f82 100644 --- a/source/packets/cPacket_UseEntity.cpp +++ b/source/packets/cPacket_UseEntity.cpp @@ -7,12 +7,12 @@ -int cPacket_UseEntity::Parse(const char * a_Data, int a_Size) +int cPacket_UseEntity::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadInteger, m_UniqueID, TotalBytes); - HANDLE_PACKET_READ(ReadInteger, m_TargetID, TotalBytes); - HANDLE_PACKET_READ(ReadBool, m_bLeftClick, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_UniqueID, TotalBytes); + HANDLE_PACKET_READ(ReadBEInt, m_TargetID, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_bLeftClick, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_UseEntity.h b/source/packets/cPacket_UseEntity.h index 7a35bd54c..799122bf9 100644 --- a/source/packets/cPacket_UseEntity.h +++ b/source/packets/cPacket_UseEntity.h @@ -17,7 +17,7 @@ public: { m_PacketID = E_USE_ENTITY; } virtual cPacket* Clone() const { return new cPacket_UseEntity(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; int m_UniqueID; int m_TargetID; diff --git a/source/packets/cPacket_WindowClick.cpp b/source/packets/cPacket_WindowClick.cpp index 05be3c53c..c250c187e 100644 --- a/source/packets/cPacket_WindowClick.cpp +++ b/source/packets/cPacket_WindowClick.cpp @@ -9,18 +9,17 @@ -int cPacket_WindowClick::Parse(const char * a_Data, int a_Size) +int cPacket_WindowClick::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadByte, m_WindowID, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_SlotNum, TotalBytes); - HANDLE_PACKET_READ(ReadByte, m_RightMouse, TotalBytes); - HANDLE_PACKET_READ(ReadShort, m_NumClicks, TotalBytes); - HANDLE_PACKET_READ(ReadBool, m_IsShiftPressed, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_WindowID, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_SlotNum, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_RightMouse, TotalBytes); + HANDLE_PACKET_READ(ReadBEShort, m_NumClicks, TotalBytes); + HANDLE_PACKET_READ(ReadBool, m_IsShiftPressed, TotalBytes); cPacket_ItemData Item; - - int res = Item.Parse(a_Data + TotalBytes, a_Size - TotalBytes); + int res = Item.Parse(a_Buffer); if (res < 0) { return res; diff --git a/source/packets/cPacket_WindowClick.h b/source/packets/cPacket_WindowClick.h index 93dc5dc2e..72ebad2a1 100644 --- a/source/packets/cPacket_WindowClick.h +++ b/source/packets/cPacket_WindowClick.h @@ -23,7 +23,7 @@ public: { m_PacketID = E_WINDOW_CLICK; } virtual cPacket* Clone() const { return new cPacket_WindowClick(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; char m_WindowID; short m_SlotNum; // Slot diff --git a/source/packets/cPacket_WindowClose.cpp b/source/packets/cPacket_WindowClose.cpp index f4361e4a1..c15e90d3d 100644 --- a/source/packets/cPacket_WindowClose.cpp +++ b/source/packets/cPacket_WindowClose.cpp @@ -7,10 +7,10 @@ -int cPacket_WindowClose::Parse(const char * a_Data, int a_Size) +int cPacket_WindowClose::Parse(cByteBuffer & a_Buffer) { int TotalBytes = 0; - HANDLE_PACKET_READ(ReadByte, m_Close, TotalBytes); + HANDLE_PACKET_READ(ReadChar, m_Close, TotalBytes); return TotalBytes; } diff --git a/source/packets/cPacket_WindowClose.h b/source/packets/cPacket_WindowClose.h index 674c071d1..3bf7b8240 100644 --- a/source/packets/cPacket_WindowClose.h +++ b/source/packets/cPacket_WindowClose.h @@ -15,7 +15,7 @@ public: { m_PacketID = E_WINDOW_CLOSE; } virtual cPacket* Clone() const { return new cPacket_WindowClose(*this); } - virtual int Parse(const char * a_Data, int a_Size) override; + virtual int Parse(cByteBuffer & a_Buffer) override; virtual void Serialize(AString & a_Data) const override; char m_Close; // m_Close == cWindow WindowType number