From 01cb5d4015608d13848ca84f635c3ae59d6aca05 Mon Sep 17 00:00:00 2001 From: Nay Date: Thu, 5 Jul 2012 02:54:29 +0100 Subject: [PATCH] Remove ByteBuffer and Packet default constructors Add an assertion and rewrite part of bytebuffer >> string operator --- src/LeEngine/ByteBuffer.cpp | 12 ++++-------- src/LeEngine/ByteBuffer.h | 2 -- src/LeEngine/Packet.h | 7 ++++--- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/LeEngine/ByteBuffer.cpp b/src/LeEngine/ByteBuffer.cpp index a0e0314..47c0102 100644 --- a/src/LeEngine/ByteBuffer.cpp +++ b/src/LeEngine/ByteBuffer.cpp @@ -12,11 +12,6 @@ #define alloca _alloca #endif -ByteBuffer::ByteBuffer() : _readPos(0), _writePos(0) -{ - _buffer.reserve(DEFAULT_BUFFER_SIZE); -} - ByteBuffer::ByteBuffer(uint32 capacity) : _readPos(0), _writePos(0) { _buffer.reserve(capacity); @@ -231,10 +226,11 @@ ByteBuffer& ByteBuffer::operator >>(std::string& value) value.clear(); uint32 length = Read7BitEncodedInt(); - Byte* res = (Byte*)alloca(length); + assert(length < 250000); // alloca is dangerous when allocating many bytes + Byte* res = (Byte*)alloca(length+1); Read(res, length); - value.append((const char*)res, length); - value.append(1, 0); + res[length] = 0; // null terminator + value = (const char*)res; return *this; } diff --git a/src/LeEngine/ByteBuffer.h b/src/LeEngine/ByteBuffer.h index 976a699..851bfa7 100644 --- a/src/LeEngine/ByteBuffer.h +++ b/src/LeEngine/ByteBuffer.h @@ -6,12 +6,10 @@ #include "Defines.h" typedef uint8 Byte; -#define DEFAULT_BUFFER_SIZE 64 // bytes class ByteBuffer { public: - ByteBuffer(); ByteBuffer(uint32 capacity); ByteBuffer(const ByteBuffer& other); diff --git a/src/LeEngine/Packet.h b/src/LeEngine/Packet.h index 37a1901..6a28aa6 100644 --- a/src/LeEngine/Packet.h +++ b/src/LeEngine/Packet.h @@ -8,20 +8,21 @@ class Packet : public ByteBuffer { protected: - // uint32 _time; // unix time + // uint64 _time; // unix time // uint8 _direction; // enum Direction uint16 _opcode; // enum Opcode - // uint32 _length; + // uint16 _length; // Byte* _data; public: - Packet() : ByteBuffer(0), _opcode(0) {} Packet(Opcode opcode, uint32 reserveSize) : ByteBuffer(reserveSize), _opcode((uint16)opcode) {} Packet(const Packet& other) : ByteBuffer(other), _opcode(other._opcode) {} Opcode GetOpcode() const { _return (Opcode)_opcode; } void SetOpcode(Opcode opcode) { _opcode = opcode; } + uint32 GetDataSize() const { return _buffer.size(); } + }; #endif // PACKET_H