TrinityCore
ByteBuffer Class Reference

#include <ByteBuffer.h>

Classes

struct  Reserve
 
struct  Resize
 

Public Member Functions

 ByteBuffer ()
 
 ByteBuffer (size_t size, Reserve)
 
 ByteBuffer (size_t size, Resize)
 
 ByteBuffer (ByteBuffer &&buf) noexcept
 
 ByteBuffer (ByteBuffer const &right)=default
 
 ByteBuffer (MessageBuffer &&buffer)
 
std::vector< uint8 > && Move () noexcept
 
ByteBufferoperator= (ByteBuffer const &right)
 
ByteBufferoperator= (ByteBuffer &&right) noexcept
 
virtual ~ByteBuffer ()=default
 
void clear ()
 
template<typename T >
void append (T value)
 
bool HasUnfinishedBitPack () const
 
void FlushBits ()
 
void ResetBitPos ()
 
bool WriteBit (bool bit)
 
bool ReadBit ()
 
void WriteBits (std::size_t value, int32 bits)
 
uint32 ReadBits (int32 bits)
 
template<typename T >
void put (std::size_t pos, T value)
 
template<>
void read_skip ()
 
template<>
void read_skip ()
 

Static Public Attributes

static size_t const DEFAULT_SIZE = 0x1000
 
static uint8 const InitialBitPos = 8
 

PutBits

Places specified amount of bits of value at specified position in packet. To ensure all bits are correctly written, only call this method after bit flush has been performed

Parameters
posPosition to place the value at, in bits. The entire value must fit in the packet It is advised to obtain the position using bitwpos() function.
valueData to write.
bitCountNumber of bits to store the value on.
size_t _rpos
 
size_t _wpos
 
size_t _bitpos
 
uint8 _curbitval
 
std::vector< uint8_storage
 
void PutBits (std::size_t pos, std::size_t value, uint32 bitCount)
 
ByteBufferoperator<< (uint8 value)
 
ByteBufferoperator<< (uint16 value)
 
ByteBufferoperator<< (uint32 value)
 
ByteBufferoperator<< (uint64 value)
 
ByteBufferoperator<< (int8 value)
 
ByteBufferoperator<< (int16 value)
 
ByteBufferoperator<< (int32 value)
 
ByteBufferoperator<< (int64 value)
 
ByteBufferoperator<< (float value)
 
ByteBufferoperator<< (double value)
 
ByteBufferoperator<< (const std::string &value)
 
ByteBufferoperator<< (const char *str)
 
ByteBufferoperator>> (bool &value)
 
ByteBufferoperator>> (uint8 &value)
 
ByteBufferoperator>> (uint16 &value)
 
ByteBufferoperator>> (uint32 &value)
 
ByteBufferoperator>> (uint64 &value)
 
ByteBufferoperator>> (int8 &value)
 
ByteBufferoperator>> (int16 &value)
 
ByteBufferoperator>> (int32 &value)
 
ByteBufferoperator>> (int64 &value)
 
ByteBufferoperator>> (float &value)
 
ByteBufferoperator>> (double &value)
 
ByteBufferoperator>> (std::string &value)
 
uint8operator[] (size_t const pos)
 
uint8 const & operator[] (size_t const pos) const
 
size_t rpos () const
 
size_t rpos (size_t rpos_)
 
void rfinish ()
 
size_t wpos () const
 
size_t wpos (size_t wpos_)
 
size_t bitwpos () const
 Returns position of last written bit. More...
 
size_t bitwpos (size_t newPos)
 
template<typename T >
void read_skip ()
 
void read_skip (size_t skip)
 
template<typename T , typename Underlying = T>
read ()
 
template<typename T , typename Underlying = T>
read (size_t pos) const
 
template<class T >
void read (T *dest, size_t count)
 
void read (uint8 *dest, size_t len)
 
template<size_t Size>
void read (std::array< uint8, Size > &arr)
 
void ReadPackedUInt64 (uint64 &guid)
 
void ReadPackedUInt64 (uint8 mask, uint64 &value)
 
std::string ReadString (uint32 length)
 
void WriteString (std::string const &str)
 
void WriteString (char const *str, size_t len)
 
uint32 ReadPackedTime ()
 
uint8contents ()
 
uint8 const * contents () const
 
size_t size () const
 
bool empty () const
 
void resize (size_t newsize)
 
void reserve (size_t ressize)
 
void append (const char *src, size_t cnt)
 
template<class T >
void append (const T *src, size_t cnt)
 
void append (uint8 const *src, size_t cnt)
 
void append (ByteBuffer const &buffer)
 
template<size_t Size>
void append (std::array< uint8, Size > const &arr)
 
void appendPackXYZ (float x, float y, float z)
 
void AppendPackedUInt64 (uint64 guid)
 
void AppendPackedTime (time_t time)
 
void put (size_t pos, uint8 const *src, size_t cnt)
 
void print_storage () const
 
void textlike () const
 
void hexlike () const
 
static size_t PackUInt64 (uint64 value, uint8 *mask, uint8 *result)
 

Constructor & Destructor Documentation

◆ ByteBuffer() [1/6]

ByteBuffer::ByteBuffer ( )
inline
61  {
62  _storage.reserve(DEFAULT_SIZE);
63  }
uint8 _curbitval
Definition: ByteBuffer.h:635
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
static uint8 const InitialBitPos
Definition: ByteBuffer.h:57
size_t _bitpos
Definition: ByteBuffer.h:634
static size_t const DEFAULT_SIZE
Definition: ByteBuffer.h:56
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634

◆ ByteBuffer() [2/6]

ByteBuffer::ByteBuffer ( size_t  size,
Reserve   
)
inline
70  {
71  _storage.reserve(size);
72  }
uint8 _curbitval
Definition: ByteBuffer.h:635
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
size_t size() const
Definition: ByteBuffer.h:539
static uint8 const InitialBitPos
Definition: ByteBuffer.h:57
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634

◆ ByteBuffer() [3/6]

ByteBuffer::ByteBuffer ( size_t  size,
Resize   
)
inline
75  {
76  _storage.resize(size);
77  }
uint8 _curbitval
Definition: ByteBuffer.h:635
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
size_t size() const
Definition: ByteBuffer.h:539
static uint8 const InitialBitPos
Definition: ByteBuffer.h:57
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634

◆ ByteBuffer() [4/6]

ByteBuffer::ByteBuffer ( ByteBuffer &&  buf)
inlinenoexcept
79  : _rpos(buf._rpos), _wpos(buf._wpos),
80  _bitpos(buf._bitpos), _curbitval(buf._curbitval), _storage(buf.Move()) { }
std::vector< uint8 > && Move() noexcept
Definition: ByteBuffer.h:86
uint8 _curbitval
Definition: ByteBuffer.h:635
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634

◆ ByteBuffer() [5/6]

ByteBuffer::ByteBuffer ( ByteBuffer const &  right)
default

◆ ByteBuffer() [6/6]

ByteBuffer::ByteBuffer ( MessageBuffer &&  buffer)
26  : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0), _storage(buffer.Move())
27 {
28 }
std::vector< uint8 > && Move()
Definition: MessageBuffer.h:102
uint8 _curbitval
Definition: ByteBuffer.h:635
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
static uint8 const InitialBitPos
Definition: ByteBuffer.h:57
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634

◆ ~ByteBuffer()

virtual ByteBuffer::~ByteBuffer ( )
virtualdefault

Member Function Documentation

◆ append() [1/6]

template<typename T >
void ByteBuffer::append ( value)
inline
136  {
137  static_assert(std::is_trivially_copyable<T>::value, "append(T) must be used with trivially copyable types");
138  EndianConvert(value);
139  append((uint8 *)&value, sizeof(value));
140  }
void EndianConvert(T &val)
Definition: ByteConverter.h:47
void append(T value)
Definition: ByteBuffer.h:135
uint8_t uint8
Definition: Define.h:154
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ append() [2/6]

void ByteBuffer::append ( const char *  src,
size_t  cnt 
)
inline
556  {
557  return append((const uint8 *)src, cnt);
558  }
void append(T value)
Definition: ByteBuffer.h:135
uint8_t uint8
Definition: Define.h:154

◆ append() [3/6]

template<class T >
void ByteBuffer::append ( const T *  src,
size_t  cnt 
)
inline
562  {
563  return append((const uint8 *)src, cnt * sizeof(T));
564  }
void append(T value)
Definition: ByteBuffer.h:135
uint8_t uint8
Definition: Define.h:154

◆ append() [4/6]

void ByteBuffer::append ( uint8 const *  src,
size_t  cnt 
)
73 {
74  ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size());
75  ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size());
76  ASSERT(size() < 10000000);
77 
78  FlushBits();
79 
80  size_t const newSize = _wpos + cnt;
81  if (_storage.capacity() < newSize) // custom memory allocation rules
82  {
83  if (newSize < 100)
84  _storage.reserve(300);
85  else if (newSize < 750)
86  _storage.reserve(2500);
87  else if (newSize < 6000)
88  _storage.reserve(10000);
89  else
90  _storage.reserve(400000);
91  }
92 
93  if (_storage.size() < newSize)
94  _storage.resize(newSize);
95  std::memcpy(&_storage[_wpos], src, cnt);
96  _wpos = newSize;
97 }
#define SZFMTD
Definition: Define.h:145
void FlushBits()
Definition: ByteBuffer.h:147
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
size_t size() const
Definition: ByteBuffer.h:539
#define ASSERT
Definition: Errors.h:57
size_t _wpos
Definition: ByteBuffer.h:634
+ Here is the call graph for this function:

◆ append() [5/6]

void ByteBuffer::append ( ByteBuffer const &  buffer)
inline
569  {
570  if (!buffer.empty())
571  append(buffer.contents(), buffer.size());
572  }
void append(T value)
Definition: ByteBuffer.h:135
+ Here is the call graph for this function:

◆ append() [6/6]

template<size_t Size>
void ByteBuffer::append ( std::array< uint8, Size > const &  arr)
inline
576  {
577  append(arr.data(), Size);
578  }
constexpr std::size_t Size(C const &container)
Definition: Containers.h:45
void append(T value)
Definition: ByteBuffer.h:135
+ Here is the call graph for this function:

◆ AppendPackedTime()

void ByteBuffer::AppendPackedTime ( time_t  time)
100 {
101  tm lt;
102  localtime_r(&time, &lt);
103  append<uint32>((lt.tm_year - 100) << 24 | lt.tm_mon << 20 | (lt.tm_mday - 1) << 14 | lt.tm_wday << 11 | lt.tm_hour << 6 | lt.tm_min);
104 }
TC_COMMON_API struct tm * localtime_r(time_t const *time, struct tm *result)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ AppendPackedUInt64()

void ByteBuffer::AppendPackedUInt64 ( uint64  guid)
inline
591  {
592  uint8 mask = 0;
593  size_t pos = wpos();
594  *this << uint8(mask);
595 
596  uint8 packed[8];
597  if (size_t packedSize = PackUInt64(guid, &mask, packed))
598  append(packed, packedSize);
599 
600  put<uint8>(pos, mask);
601  }
size_t wpos() const
Definition: ByteBuffer.h:409
static size_t PackUInt64(uint64 value, uint8 *mask, uint8 *result)
Definition: ByteBuffer.h:603
void append(T value)
Definition: ByteBuffer.h:135
uint8_t uint8
Definition: Define.h:154

◆ appendPackXYZ()

void ByteBuffer::appendPackXYZ ( float  x,
float  y,
float  z 
)
inline
582  {
583  uint32 packed = 0;
584  packed |= ((int)(x / 0.25f) & 0x7FF);
585  packed |= ((int)(y / 0.25f) & 0x7FF) << 11;
586  packed |= ((int)(z / 0.25f) & 0x3FF) << 22;
587  *this << packed;
588  }
uint32_t uint32
Definition: Define.h:152
+ Here is the caller graph for this function:

◆ bitwpos() [1/2]

size_t ByteBuffer::bitwpos ( ) const
inline

Returns position of last written bit.

418 { return _wpos * 8 + 8 - _bitpos; }
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634

◆ bitwpos() [2/2]

size_t ByteBuffer::bitwpos ( size_t  newPos)
inline
421  {
422  _wpos = newPos / 8;
423  _bitpos = 8 - (newPos % 8);
424  return _wpos * 8 + 8 - _bitpos;
425  }
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634

◆ clear()

void ByteBuffer::clear ( )
inline
126  {
127  _rpos = 0;
128  _wpos = 0;
130  _curbitval = 0;
131  _storage.clear();
132  }
uint8 _curbitval
Definition: ByteBuffer.h:635
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
static uint8 const InitialBitPos
Definition: ByteBuffer.h:57
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ contents() [1/2]

uint8* ByteBuffer::contents ( )
inline
526  {
527  if (_storage.empty())
528  throw ByteBufferException();
529  return _storage.data();
530  }
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
Definition: ByteBuffer.h:31
+ Here is the caller graph for this function:

◆ contents() [2/2]

uint8 const* ByteBuffer::contents ( ) const
inline
533  {
534  if (_storage.empty())
535  throw ByteBufferException();
536  return _storage.data();
537  }
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
Definition: ByteBuffer.h:31

◆ empty()

bool ByteBuffer::empty ( ) const
inline
540 { return _storage.empty(); }
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
+ Here is the caller graph for this function:

◆ FlushBits()

void ByteBuffer::FlushBits ( )
inline
148  {
149  if (_bitpos == 8)
150  return;
151 
152  _bitpos = 8;
153 
154  append((uint8 *)&_curbitval, sizeof(uint8));
155  _curbitval = 0;
156  }
uint8 _curbitval
Definition: ByteBuffer.h:635
void append(T value)
Definition: ByteBuffer.h:135
size_t _bitpos
Definition: ByteBuffer.h:634
uint8_t uint8
Definition: Define.h:154

◆ HasUnfinishedBitPack()

bool ByteBuffer::HasUnfinishedBitPack ( ) const
inline
143  {
144  return _bitpos != 8;
145  }
size_t _bitpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ hexlike()

void ByteBuffer::hexlike ( ) const
163 {
164  if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
165  return;
166 
167  uint32 j = 1, k = 1;
168 
169  std::ostringstream o;
170  o << "STORAGE_SIZE: " << size();
171 
172  for (uint32 i = 0; i < size(); ++i)
173  {
174  char buf[4];
175  snprintf(buf, 4, "%2X", read<uint8>(i));
176  if ((i == (j * 8)) && ((i != (k * 16))))
177  {
178  o << "| ";
179  ++j;
180  }
181  else if (i == (k * 16))
182  {
183  o << "\n";
184  ++k;
185  ++j;
186  }
187 
188  o << buf;
189  }
190  o << " ";
191  TC_LOG_TRACE("network", "%s", o.str().c_str());
192 }
Definition: LogCommon.h:24
#define sLog
Definition: Log.h:131
#define TC_LOG_TRACE(filterType__,...)
Definition: Log.h:174
uint32_t uint32
Definition: Define.h:152
size_t size() const
Definition: ByteBuffer.h:539
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Move()

std::vector<uint8>&& ByteBuffer::Move ( )
inlinenoexcept
87  {
88  _rpos = 0;
89  _wpos = 0;
91  _curbitval = 0;
92  return std::move(_storage);
93  }
uint8 _curbitval
Definition: ByteBuffer.h:635
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
static uint8 const InitialBitPos
Definition: ByteBuffer.h:57
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ operator<<() [1/12]

ByteBuffer& ByteBuffer::operator<< ( uint8  value)
inline
234  {
235  append<uint8>(value);
236  return *this;
237  }

◆ operator<<() [2/12]

ByteBuffer& ByteBuffer::operator<< ( uint16  value)
inline
240  {
241  append<uint16>(value);
242  return *this;
243  }

◆ operator<<() [3/12]

ByteBuffer& ByteBuffer::operator<< ( uint32  value)
inline
246  {
247  append<uint32>(value);
248  return *this;
249  }

◆ operator<<() [4/12]

ByteBuffer& ByteBuffer::operator<< ( uint64  value)
inline
252  {
253  append<uint64>(value);
254  return *this;
255  }

◆ operator<<() [5/12]

ByteBuffer& ByteBuffer::operator<< ( int8  value)
inline
259  {
260  append<int8>(value);
261  return *this;
262  }

◆ operator<<() [6/12]

ByteBuffer& ByteBuffer::operator<< ( int16  value)
inline
265  {
266  append<int16>(value);
267  return *this;
268  }

◆ operator<<() [7/12]

ByteBuffer& ByteBuffer::operator<< ( int32  value)
inline
271  {
272  append<int32>(value);
273  return *this;
274  }

◆ operator<<() [8/12]

ByteBuffer& ByteBuffer::operator<< ( int64  value)
inline
277  {
278  append<int64>(value);
279  return *this;
280  }

◆ operator<<() [9/12]

ByteBuffer& ByteBuffer::operator<< ( float  value)
inline
284  {
285  append<float>(value);
286  return *this;
287  }

◆ operator<<() [10/12]

ByteBuffer& ByteBuffer::operator<< ( double  value)
inline
290  {
291  append<double>(value);
292  return *this;
293  }

◆ operator<<() [11/12]

ByteBuffer& ByteBuffer::operator<< ( const std::string &  value)
inline
296  {
297  if (size_t len = value.length())
298  append((uint8 const*)value.c_str(), len);
299  append<uint8>(0);
300  return *this;
301  }
void append(T value)
Definition: ByteBuffer.h:135
uint8_t uint8
Definition: Define.h:154

◆ operator<<() [12/12]

ByteBuffer& ByteBuffer::operator<< ( const char *  str)
inline
304  {
305  if (size_t len = (str ? strlen(str) : 0))
306  append((uint8 const*)str, len);
307  append<uint8>(0);
308  return *this;
309  }
void append(T value)
Definition: ByteBuffer.h:135
uint8_t uint8
Definition: Define.h:154

◆ operator=() [1/2]

ByteBuffer& ByteBuffer::operator= ( ByteBuffer const &  right)
inline
96  {
97  if (this != &right)
98  {
99  _rpos = right._rpos;
100  _wpos = right._wpos;
101  _bitpos = right._bitpos;
102  _curbitval = right._curbitval;
103  _storage = right._storage;
104  }
105 
106  return *this;
107  }
uint8 _curbitval
Definition: ByteBuffer.h:635
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ operator=() [2/2]

ByteBuffer& ByteBuffer::operator= ( ByteBuffer &&  right)
inlinenoexcept
110  {
111  if (this != &right)
112  {
113  _rpos = right._rpos;
114  _wpos = right._wpos;
115  _bitpos = right._bitpos;
116  _curbitval = right._curbitval;
117  _storage = right.Move();
118  }
119 
120  return *this;
121  }
std::vector< uint8 > && Move() noexcept
Definition: ByteBuffer.h:86
uint8 _curbitval
Definition: ByteBuffer.h:635
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
size_t _bitpos
Definition: ByteBuffer.h:634
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the call graph for this function:

◆ operator>>() [1/12]

ByteBuffer& ByteBuffer::operator>> ( bool &  value)
inline
312  {
313  value = read<char>() > 0 ? true : false;
314  return *this;
315  }

◆ operator>>() [2/12]

ByteBuffer& ByteBuffer::operator>> ( uint8 value)
inline
318  {
319  value = read<uint8>();
320  return *this;
321  }

◆ operator>>() [3/12]

ByteBuffer& ByteBuffer::operator>> ( uint16 value)
inline
324  {
325  value = read<uint16>();
326  return *this;
327  }

◆ operator>>() [4/12]

ByteBuffer& ByteBuffer::operator>> ( uint32 value)
inline
330  {
331  value = read<uint32>();
332  return *this;
333  }

◆ operator>>() [5/12]

ByteBuffer& ByteBuffer::operator>> ( uint64 value)
inline
336  {
337  value = read<uint64>();
338  return *this;
339  }

◆ operator>>() [6/12]

ByteBuffer& ByteBuffer::operator>> ( int8 value)
inline
343  {
344  value = read<int8>();
345  return *this;
346  }

◆ operator>>() [7/12]

ByteBuffer& ByteBuffer::operator>> ( int16 value)
inline
349  {
350  value = read<int16>();
351  return *this;
352  }

◆ operator>>() [8/12]

ByteBuffer& ByteBuffer::operator>> ( int32 value)
inline
355  {
356  value = read<int32>();
357  return *this;
358  }

◆ operator>>() [9/12]

ByteBuffer& ByteBuffer::operator>> ( int64 value)
inline
361  {
362  value = read<int64>();
363  return *this;
364  }
+ Here is the call graph for this function:

◆ operator>>() [10/12]

ByteBuffer & ByteBuffer::operator>> ( float &  value)
42 {
43  value = read<float>();
44  if (!std::isfinite(value))
45  throw ByteBufferException();
46  return *this;
47 }
Definition: ByteBuffer.h:31

◆ operator>>() [11/12]

ByteBuffer & ByteBuffer::operator>> ( double &  value)
50 {
51  value = read<double>();
52  if (!std::isfinite(value))
53  throw ByteBufferException();
54  return *this;
55 }
Definition: ByteBuffer.h:31

◆ operator>>() [12/12]

ByteBuffer& ByteBuffer::operator>> ( std::string &  value)
inline
370  {
371  value.clear();
372  while (rpos() < size()) // prevent crash at wrong string format in packet
373  {
374  char c = read<char>();
375  if (c == 0)
376  break;
377  value += c;
378  }
379  return *this;
380  }
size_t rpos() const
Definition: ByteBuffer.h:396
size_t size() const
Definition: ByteBuffer.h:539
+ Here is the call graph for this function:

◆ operator[]() [1/2]

uint8& ByteBuffer::operator[] ( size_t const  pos)
inline
383  {
384  if (pos >= size())
385  throw ByteBufferPositionException(pos, 1, size());
386  return _storage[pos];
387  }
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
Definition: ByteBuffer.h:45
size_t size() const
Definition: ByteBuffer.h:539
+ Here is the call graph for this function:

◆ operator[]() [2/2]

uint8 const& ByteBuffer::operator[] ( size_t const  pos) const
inline
390  {
391  if (pos >= size())
392  throw ByteBufferPositionException(pos, 1, size());
393  return _storage[pos];
394  }
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
Definition: ByteBuffer.h:45
size_t size() const
Definition: ByteBuffer.h:539
+ Here is the call graph for this function:

◆ PackUInt64()

static size_t ByteBuffer::PackUInt64 ( uint64  value,
uint8 mask,
uint8 result 
)
inlinestatic
604  {
605  size_t resultSize = 0;
606  *mask = 0;
607  memset(result, 0, 8);
608 
609  for (uint8 i = 0; value != 0; ++i)
610  {
611  if (value & 0xFF)
612  {
613  *mask |= uint8(1 << i);
614  result[resultSize++] = uint8(value & 0xFF);
615  }
616 
617  value >>= 8;
618  }
619 
620  return resultSize;
621  }
uint8_t uint8
Definition: Define.h:154
+ Here is the caller graph for this function:

◆ print_storage()

void ByteBuffer::print_storage ( ) const
132 {
133  if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
134  return;
135 
136  std::ostringstream o;
137  o << "STORAGE_SIZE: " << size();
138  for (uint32 i = 0; i < size(); ++i)
139  o << read<uint8>(i) << " - ";
140  o << " ";
141 
142  TC_LOG_TRACE("network", "%s", o.str().c_str());
143 }
Definition: LogCommon.h:24
#define sLog
Definition: Log.h:131
#define TC_LOG_TRACE(filterType__,...)
Definition: Log.h:174
uint32_t uint32
Definition: Define.h:152
size_t size() const
Definition: ByteBuffer.h:539
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ put() [1/2]

template<typename T >
void ByteBuffer::put ( std::size_t  pos,
value 
)
inline
213  {
214  static_assert(std::is_trivially_copyable<T>::value, "put(size_t, T) must be used with trivially copyable types");
215  EndianConvert(value);
216  put(pos, (uint8 *)&value, sizeof(value));
217  }
void EndianConvert(T &val)
Definition: ByteConverter.h:47
uint8_t uint8
Definition: Define.h:154
void put(std::size_t pos, T value)
Definition: ByteBuffer.h:212
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ put() [2/2]

void ByteBuffer::put ( size_t  pos,
uint8 const *  src,
size_t  cnt 
)
107 {
108  ASSERT(pos + cnt <= size(), "Attempted to put value with size: " SZFMTD " in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", cnt, pos, size());
109  ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size());
110  ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size());
111 
112  std::memcpy(&_storage[pos], src, cnt);
113 }
#define SZFMTD
Definition: Define.h:145
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
size_t size() const
Definition: ByteBuffer.h:539
#define ASSERT
Definition: Errors.h:57
+ Here is the call graph for this function:

◆ PutBits()

void ByteBuffer::PutBits ( std::size_t  pos,
std::size_t  value,
uint32  bitCount 
)
116 {
117  ASSERT(pos + bitCount <= size() * 8, "Attempted to put %u bits in ByteBuffer (bitpos: " SZFMTD " size: " SZFMTD ")", bitCount, pos, size());
118  ASSERT(bitCount, "Attempted to put a zero bits in ByteBuffer");
119 
120  for (uint32 i = 0; i < bitCount; ++i)
121  {
122  std::size_t wp = (pos + i) / 8;
123  std::size_t bit = (pos + i) % 8;
124  if ((value >> (bitCount - i - 1)) & 1)
125  _storage[wp] |= 1 << (7 - bit);
126  else
127  _storage[wp] &= ~(1 << (7 - bit));
128  }
129 }
#define SZFMTD
Definition: Define.h:145
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
uint32_t uint32
Definition: Define.h:152
size_t size() const
Definition: ByteBuffer.h:539
#define ASSERT
Definition: Errors.h:57
+ Here is the call graph for this function:

◆ read() [1/5]

template<typename T , typename Underlying = T>
T ByteBuffer::read ( )
inline
441  {
442  ResetBitPos();
443  T r = read<T, Underlying>(_rpos);
444  _rpos += sizeof(Underlying);
445  return r;
446  }
void ResetBitPos()
Definition: ByteBuffer.h:158
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ read() [2/5]

template<typename T , typename Underlying = T>
T ByteBuffer::read ( size_t  pos) const
inline
450  {
451  if (pos + sizeof(Underlying) > size())
452  throw ByteBufferPositionException(pos, sizeof(Underlying), size());
453  Underlying val;
454  std::memcpy(&val, &_storage[pos], sizeof(Underlying));
455  EndianConvert(val);
456  return static_cast<T>(val);
457  }
void EndianConvert(T &val)
Definition: ByteConverter.h:47
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
Definition: ByteBuffer.h:45
size_t size() const
Definition: ByteBuffer.h:539
+ Here is the call graph for this function:

◆ read() [3/5]

template<class T >
void ByteBuffer::read ( T *  dest,
size_t  count 
)
inline
461  {
462  static_assert(std::is_trivially_copyable<T>::value, "read(T*, size_t) must be used with trivially copyable types");
463  return read(reinterpret_cast<uint8*>(dest), count * sizeof(T));
464  }
T read()
Definition: ByteBuffer.h:440

◆ read() [4/5]

void ByteBuffer::read ( uint8 dest,
size_t  len 
)
inline
467  {
468  if (_rpos + len > size())
469  throw ByteBufferPositionException(_rpos, len, size());
470 
471  ResetBitPos();
472  std::memcpy(dest, &_storage[_rpos], len);
473  _rpos += len;
474  }
void ResetBitPos()
Definition: ByteBuffer.h:158
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
Definition: ByteBuffer.h:45
size_t size() const
Definition: ByteBuffer.h:539
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the call graph for this function:

◆ read() [5/5]

template<size_t Size>
void ByteBuffer::read ( std::array< uint8, Size > &  arr)
inline
478  {
479  read(arr.data(), Size);
480  }
constexpr std::size_t Size(C const &container)
Definition: Containers.h:45
T read()
Definition: ByteBuffer.h:440
+ Here is the call graph for this function:

◆ read_skip() [1/4]

template<typename T >
void ByteBuffer::read_skip ( )
inline
428 { read_skip(sizeof(T)); }
void read_skip()
Definition: ByteBuffer.h:428
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read_skip() [2/4]

void ByteBuffer::read_skip ( size_t  skip)
inline
431  {
432  if (_rpos + skip > size())
433  throw ByteBufferPositionException(_rpos, skip, size());
434 
435  ResetBitPos();
436  _rpos += skip;
437  }
void ResetBitPos()
Definition: ByteBuffer.h:158
Definition: ByteBuffer.h:45
size_t size() const
Definition: ByteBuffer.h:539
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the call graph for this function:

◆ read_skip() [3/4]

template<>
void ByteBuffer::read_skip ( )
inline
649 {
650  std::string temp;
651  *this >> temp;
652 }

◆ read_skip() [4/4]

template<>
void ByteBuffer::read_skip ( )
inline
656 {
657  read_skip<char*>();
658 }

◆ ReadBit()

bool ByteBuffer::ReadBit ( )
inline
184  {
185  ++_bitpos;
186  if (_bitpos > 7)
187  {
188  _curbitval = read<uint8>();
189  _bitpos = 0;
190  }
191 
192  return ((_curbitval >> (7-_bitpos)) & 1) != 0;
193  }
uint8 _curbitval
Definition: ByteBuffer.h:635
size_t _bitpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ ReadBits()

uint32 ByteBuffer::ReadBits ( int32  bits)
inline
202  {
203  uint32 value = 0;
204  for (int32 i = bits - 1; i >= 0; --i)
205  if (ReadBit())
206  value |= (1 << (i));
207 
208  return value;
209  }
int32_t int32
Definition: Define.h:148
uint32_t uint32
Definition: Define.h:152
bool ReadBit()
Definition: ByteBuffer.h:183
+ Here is the caller graph for this function:

◆ ReadPackedTime()

uint32 ByteBuffer::ReadPackedTime ( )
58 {
59  uint32 packedDate = read<uint32>();
60  tm lt = tm();
61 
62  lt.tm_min = packedDate & 0x3F;
63  lt.tm_hour = (packedDate >> 6) & 0x1F;
64  //lt.tm_wday = (packedDate >> 11) & 7;
65  lt.tm_mday = ((packedDate >> 14) & 0x3F) + 1;
66  lt.tm_mon = (packedDate >> 20) & 0xF;
67  lt.tm_year = ((packedDate >> 24) & 0x1F) + 100;
68 
69  return uint32(mktime(&lt));
70 }
uint32_t uint32
Definition: Define.h:152
+ Here is the caller graph for this function:

◆ ReadPackedUInt64() [1/2]

void ByteBuffer::ReadPackedUInt64 ( uint64 guid)
inline
483  {
484  guid = 0;
485  ReadPackedUInt64(read<uint8>(), guid);
486  }
void ReadPackedUInt64(uint64 &guid)
Definition: ByteBuffer.h:482
+ Here is the caller graph for this function:

◆ ReadPackedUInt64() [2/2]

void ByteBuffer::ReadPackedUInt64 ( uint8  mask,
uint64 value 
)
inline
489  {
490  for (uint32 i = 0; i < 8; ++i)
491  if (mask & (uint8(1) << i))
492  value |= (uint64(read<uint8>()) << (i * 8));
493  }
uint32_t uint32
Definition: Define.h:152
uint64_t uint64
Definition: Define.h:151
uint8_t uint8
Definition: Define.h:154

◆ ReadString()

std::string ByteBuffer::ReadString ( uint32  length)
inline
496  {
497  if (_rpos + length > size())
498  throw ByteBufferPositionException(_rpos, length, size());
499 
500  ResetBitPos();
501  if (!length)
502  return std::string();
503 
504  std::string str((char const*)&_storage[_rpos], length);
505  _rpos += length;
506  return str;
507  }
void ResetBitPos()
Definition: ByteBuffer.h:158
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
Definition: ByteBuffer.h:45
size_t size() const
Definition: ByteBuffer.h:539
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reserve()

void ByteBuffer::reserve ( size_t  ressize)
inline
550  {
551  if (ressize > size())
552  _storage.reserve(ressize);
553  }
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
size_t size() const
Definition: ByteBuffer.h:539
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ResetBitPos()

void ByteBuffer::ResetBitPos ( )
inline
159  {
160  if (_bitpos > 7)
161  return;
162 
163  _bitpos = 8;
164  _curbitval = 0;
165  }
uint8 _curbitval
Definition: ByteBuffer.h:635
size_t _bitpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ resize()

void ByteBuffer::resize ( size_t  newsize)
inline
543  {
544  _storage.resize(newsize, 0);
545  _rpos = 0;
546  _wpos = size();
547  }
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
size_t size() const
Definition: ByteBuffer.h:539
size_t _wpos
Definition: ByteBuffer.h:634
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ rfinish()

void ByteBuffer::rfinish ( )
inline
405  {
406  _rpos = wpos();
407  }
size_t wpos() const
Definition: ByteBuffer.h:409
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ rpos() [1/2]

size_t ByteBuffer::rpos ( ) const
inline
396 { return _rpos; }
size_t _rpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ rpos() [2/2]

size_t ByteBuffer::rpos ( size_t  rpos_)
inline
399  {
400  _rpos = rpos_;
401  return _rpos;
402  }
size_t _rpos
Definition: ByteBuffer.h:634

◆ size()

size_t ByteBuffer::size ( ) const
inline
539 { return _storage.size(); }
std::vector< uint8 > _storage
Definition: ByteBuffer.h:636
+ Here is the caller graph for this function:

◆ textlike()

void ByteBuffer::textlike ( ) const
146 {
147  if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
148  return;
149 
150  std::ostringstream o;
151  o << "STORAGE_SIZE: " << size();
152  for (uint32 i = 0; i < size(); ++i)
153  {
154  char buf[2];
155  snprintf(buf, 2, "%c", read<uint8>(i));
156  o << buf;
157  }
158  o << " ";
159  TC_LOG_TRACE("network", "%s", o.str().c_str());
160 }
Definition: LogCommon.h:24
#define sLog
Definition: Log.h:131
#define TC_LOG_TRACE(filterType__,...)
Definition: Log.h:174
uint32_t uint32
Definition: Define.h:152
size_t size() const
Definition: ByteBuffer.h:539
+ Here is the call graph for this function:

◆ wpos() [1/2]

size_t ByteBuffer::wpos ( ) const
inline
409 { return _wpos; }
size_t _wpos
Definition: ByteBuffer.h:634
+ Here is the caller graph for this function:

◆ wpos() [2/2]

size_t ByteBuffer::wpos ( size_t  wpos_)
inline
412  {
413  _wpos = wpos_;
414  return _wpos;
415  }
size_t _wpos
Definition: ByteBuffer.h:634

◆ WriteBit()

bool ByteBuffer::WriteBit ( bool  bit)
inline
168  {
169  --_bitpos;
170  if (bit)
171  _curbitval |= (1 << (_bitpos));
172 
173  if (_bitpos == 0)
174  {
175  _bitpos = 8;
176  append((uint8 *)&_curbitval, sizeof(_curbitval));
177  _curbitval = 0;
178  }
179 
180  return bit;
181  }
uint8 _curbitval
Definition: ByteBuffer.h:635
void append(T value)
Definition: ByteBuffer.h:135
size_t _bitpos
Definition: ByteBuffer.h:634
uint8_t uint8
Definition: Define.h:154

◆ WriteBits()

void ByteBuffer::WriteBits ( std::size_t  value,
int32  bits 
)
inline
196  {
197  for (int32 i = bits - 1; i >= 0; --i)
198  WriteBit((value >> i) & 1);
199  }
bool WriteBit(bool bit)
Definition: ByteBuffer.h:167
int32_t int32
Definition: Define.h:148

◆ WriteString() [1/2]

void ByteBuffer::WriteString ( std::string const &  str)
inline

Method for writing strings that have their length sent separately in packet without null-terminating the string

512  {
513  if (size_t len = str.length())
514  append(str.c_str(), len);
515  }
void append(T value)
Definition: ByteBuffer.h:135
+ Here is the caller graph for this function:

◆ WriteString() [2/2]

void ByteBuffer::WriteString ( char const *  str,
size_t  len 
)
inline
518  {
519  if (len)
520  append(str, len);
521  }
void append(T value)
Definition: ByteBuffer.h:135

Member Data Documentation

◆ _bitpos

size_t ByteBuffer::_bitpos
protected

◆ _curbitval

uint8 ByteBuffer::_curbitval
protected

◆ _rpos

size_t ByteBuffer::_rpos
protected

◆ _storage

std::vector<uint8> ByteBuffer::_storage
protected

◆ _wpos

size_t ByteBuffer::_wpos
protected

◆ DEFAULT_SIZE

size_t const ByteBuffer::DEFAULT_SIZE = 0x1000
static

◆ InitialBitPos

uint8 const ByteBuffer::InitialBitPos = 8
static

The documentation for this class was generated from the following files: