aboutsummaryrefslogtreecommitdiff
path: root/src/server/shared/Packets/ByteBuffer.h
diff options
context:
space:
mode:
authorSpp <spp@jorge.gr>2013-03-25 13:26:48 +0100
committerSpp <spp@jorge.gr>2013-03-25 13:26:48 +0100
commit1a6a23ec96c7646d2753198f93eaba62d2732a83 (patch)
tree2ef7e8ffd5201bd6e7bf0a4ddfd768a24e263e82 /src/server/shared/Packets/ByteBuffer.h
parent7c36e3a298ce243631848b94364c185a863d1526 (diff)
Core/Misc: Minor optimizations (+code changes to reduce differences with 4.3.4 branch)
Core/Logging: Create new logger type "Cheat". Will be used to log all cheat attempts
Diffstat (limited to 'src/server/shared/Packets/ByteBuffer.h')
-rw-r--r--src/server/shared/Packets/ByteBuffer.h167
1 files changed, 52 insertions, 115 deletions
diff --git a/src/server/shared/Packets/ByteBuffer.h b/src/server/shared/Packets/ByteBuffer.h
index ceab4ca442e..9b16c2e9c0b 100644
--- a/src/server/shared/Packets/ByteBuffer.h
+++ b/src/server/shared/Packets/ByteBuffer.h
@@ -19,64 +19,47 @@
#ifndef _BYTEBUFFER_H
#define _BYTEBUFFER_H
-#include "Common.h"
+#include "Define.h"
#include "Debugging/Errors.h"
-#include "Logging/Log.h"
#include "Utilities/ByteConverter.h"
-class ByteBufferException
+#include <exception>
+#include <list>
+#include <map>
+#include <string>
+#include <vector>
+
+#include <cstring>
+
+// Root of ByteBuffer exception hierarchy
+class ByteBufferException : public std::exception
{
- public:
- ByteBufferException(size_t pos, size_t size, size_t valueSize)
- : Pos(pos), Size(size), ValueSize(valueSize)
- {
- }
+public:
+ ~ByteBufferException() throw() { }
- protected:
- size_t Pos;
- size_t Size;
- size_t ValueSize;
+ char const * what() const throw() { return msg_.c_str(); }
+
+protected:
+ std::string & message() throw() { return msg_; }
+
+private:
+ std::string msg_;
};
class ByteBufferPositionException : public ByteBufferException
{
- public:
- ByteBufferPositionException(bool add, size_t pos, size_t size, size_t valueSize)
- : ByteBufferException(pos, size, valueSize), _add(add)
- {
- PrintError();
- }
-
- protected:
- void PrintError() const
- {
- ACE_Stack_Trace trace;
-
- sLog->outError(LOG_FILTER_NETWORKIO, "Attempted to %s value with size: "SIZEFMTD" in ByteBuffer (pos: " SIZEFMTD " size: "SIZEFMTD")\n[Stacktrace: %s]",
- (_add ? "put" : "get"), ValueSize, Pos, Size, trace.c_str());
- }
+public:
+ ByteBufferPositionException(bool add, size_t pos, size_t size, size_t valueSize);
- private:
- bool _add;
+ ~ByteBufferPositionException() throw() { }
};
class ByteBufferSourceException : public ByteBufferException
{
- public:
- ByteBufferSourceException(size_t pos, size_t size, size_t valueSize)
- : ByteBufferException(pos, size, valueSize)
- {
- PrintError();
- }
+public:
+ ByteBufferSourceException(size_t pos, size_t size, size_t valueSize);
- protected:
- void PrintError() const
- {
- ACE_Stack_Trace trace;
-
- sLog->outError(LOG_FILTER_NETWORKIO, "Attempted to put a %s in ByteBuffer (pos: "SIZEFMTD" size: "SIZEFMTD")\n[Stacktrace: %s]",
- (ValueSize > 0 ? "NULL-pointer" : "zero-sized value"), Pos, Size, trace.c_str());
- }
+ ~ByteBufferSourceException() throw() { }
};
class ByteBuffer
@@ -85,19 +68,21 @@ class ByteBuffer
const static size_t DEFAULT_SIZE = 0x1000;
// constructor
- ByteBuffer(): _rpos(0), _wpos(0)
+ ByteBuffer() : _rpos(0), _wpos(0)
{
_storage.reserve(DEFAULT_SIZE);
}
- // constructor
- ByteBuffer(size_t res): _rpos(0), _wpos(0)
+ ByteBuffer(size_t reserve) : _rpos(0), _wpos(0)
{
- _storage.reserve(res);
+ _storage.reserve(reserve);
}
// copy constructor
- ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage) { }
+ ByteBuffer(const ByteBuffer &buf) : _rpos(buf._rpos), _wpos(buf._wpos),
+ _storage(buf._storage)
+ {
+ }
void clear()
{
@@ -275,9 +260,18 @@ class ByteBuffer
return *this;
}
- uint8 operator[](size_t pos) const
+ uint8& operator[](size_t const pos)
{
- return read<uint8>(pos);
+ if (pos >= size())
+ throw ByteBufferPositionException(false, pos, 1, size());
+ return _storage[pos];
+ }
+
+ uint8 const& operator[](size_t const pos) const
+ {
+ if (pos >= size())
+ throw ByteBufferPositionException(false, pos, 1, size());
+ return _storage[pos];
}
size_t rpos() const { return _rpos; }
@@ -331,7 +325,7 @@ class ByteBuffer
{
if (_rpos + len > size())
throw ByteBufferPositionException(false, _rpos, len, size());
- memcpy(dest, &_storage[_rpos], len);
+ std::memcpy(dest, &_storage[_rpos], len);
_rpos += len;
}
@@ -362,8 +356,7 @@ class ByteBuffer
uint32 ReadPackedTime()
{
uint32 packedDate = read<uint32>();
- tm lt;
- memset(&lt, 0, sizeof(lt));
+ tm lt = tm();
lt.tm_min = packedDate & 0x3F;
lt.tm_hour = (packedDate >> 6) & 0x1F;
@@ -423,7 +416,7 @@ class ByteBuffer
if (_storage.size() < _wpos + cnt)
_storage.resize(_wpos + cnt);
- memcpy(&_storage[_wpos], src, cnt);
+ std::memcpy(&_storage[_wpos], src, cnt);
_wpos += cnt;
}
@@ -476,71 +469,14 @@ class ByteBuffer
if (!src)
throw ByteBufferSourceException(_wpos, size(), cnt);
- memcpy(&_storage[pos], src, cnt);
+ std::memcpy(&_storage[pos], src, cnt);
}
- void print_storage() const
- {
- if (!sLog->ShouldLog(LOG_FILTER_NETWORKIO, LOG_LEVEL_TRACE)) // optimize disabled debug output
- return;
+ void print_storage() const;
- std::ostringstream o;
- o << "STORAGE_SIZE: " << size();
- for (uint32 i = 0; i < size(); ++i)
- o << read<uint8>(i) << " - ";
- o << " ";
+ void textlike() const;
- sLog->outTrace(LOG_FILTER_NETWORKIO, "%s", o.str().c_str());
- }
-
- void textlike() const
- {
- if (!sLog->ShouldLog(LOG_FILTER_NETWORKIO, LOG_LEVEL_TRACE)) // optimize disabled debug output
- return;
-
- std::ostringstream o;
- o << "STORAGE_SIZE: " << size();
- for (uint32 i = 0; i < size(); ++i)
- {
- char buf[1];
- snprintf(buf, 1, "%c", read<uint8>(i));
- o << buf;
- }
- o << " ";
- sLog->outTrace(LOG_FILTER_NETWORKIO, "%s", o.str().c_str());
- }
-
- void hexlike() const
- {
- if (!sLog->ShouldLog(LOG_FILTER_NETWORKIO, LOG_LEVEL_TRACE)) // optimize disabled debug output
- return;
-
- uint32 j = 1, k = 1;
-
- std::ostringstream o;
- o << "STORAGE_SIZE: " << size();
-
- for (uint32 i = 0; i < size(); ++i)
- {
- char buf[3];
- snprintf(buf, 1, "%2X ", read<uint8>(i));
- if ((i == (j * 8)) && ((i != (k * 16))))
- {
- o << "| ";
- ++j;
- }
- else if (i == (k * 16))
- {
- o << "\n";
- ++k;
- ++j;
- }
-
- o << buf;
- }
- o << " ";
- sLog->outTrace(LOG_FILTER_NETWORKIO, "%s", o.str().c_str());
- }
+ void hexlike() const;
protected:
size_t _rpos, _wpos;
@@ -652,5 +588,6 @@ inline void ByteBuffer::read_skip<std::string>()
{
read_skip<char*>();
}
+
#endif