Skip to content

Commit

Permalink
Remove ByteBuffer and Packet default constructors
Browse files Browse the repository at this point in the history
Add an assertion and rewrite part of bytebuffer >> string operator
  • Loading branch information
DDuarte committed Jul 5, 2012
1 parent e308ed6 commit 01cb5d4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
12 changes: 4 additions & 8 deletions src/LeEngine/ByteBuffer.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions src/LeEngine/ByteBuffer.h
Expand Up @@ -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);

Expand Down
7 changes: 4 additions & 3 deletions src/LeEngine/Packet.h
Expand Up @@ -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

0 comments on commit 01cb5d4

Please sign in to comment.