TrinityCore
Loading...
Searching...
No Matches
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 constexpr size_t DEFAULT_SIZE = 0x1000
 
static constexpr uint8 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<< (std::string_view value)
 
ByteBufferoperator<< (std::string const &str)
 
ByteBufferoperator<< (char const *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.
 
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)
 
void WriteString (std::string const &str)
 
void WriteString (std::string_view str)
 
void WriteString (char const *str, size_t len)
 
std::string ReadCString (bool requireValidUtf8=true)
 
std::string ReadString (uint32 length, bool requireValidUtf8=true)
 
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 shrink_to_fit ()
 
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)
 

Detailed Description

Definition at line 61 of file ByteBuffer.h.

Constructor & Destructor Documentation

◆ ByteBuffer() [1/6]

ByteBuffer::ByteBuffer ( )
inline

Definition at line 68 of file ByteBuffer.h.

69 {
70 _storage.reserve(DEFAULT_SIZE);
71 }
size_t _wpos
Definition: ByteBuffer.h:638
std::vector< uint8 > _storage
Definition: ByteBuffer.h:640
size_t _bitpos
Definition: ByteBuffer.h:638
uint8 _curbitval
Definition: ByteBuffer.h:639
static constexpr size_t DEFAULT_SIZE
Definition: ByteBuffer.h:64
static constexpr uint8 InitialBitPos
Definition: ByteBuffer.h:65
size_t _rpos
Definition: ByteBuffer.h:638

◆ ByteBuffer() [2/6]

ByteBuffer::ByteBuffer ( size_t  size,
Reserve   
)
inline

Definition at line 77 of file ByteBuffer.h.

78 {
79 _storage.reserve(size);
80 }
size_t size() const
Definition: ByteBuffer.h:538

◆ ByteBuffer() [3/6]

ByteBuffer::ByteBuffer ( size_t  size,
Resize   
)
inline

Definition at line 82 of file ByteBuffer.h.

83 {
84 _storage.resize(size);
85 }

◆ ByteBuffer() [4/6]

ByteBuffer::ByteBuffer ( ByteBuffer &&  buf)
inlinenoexcept

Definition at line 87 of file ByteBuffer.h.

87 : _rpos(buf._rpos), _wpos(buf._wpos),
88 _bitpos(buf._bitpos), _curbitval(buf._curbitval), _storage(buf.Move()) { }
std::vector< uint8 > && Move() noexcept
Definition: ByteBuffer.h:94

◆ ByteBuffer() [5/6]

ByteBuffer::ByteBuffer ( ByteBuffer const &  right)
default

◆ ByteBuffer() [6/6]

ByteBuffer::ByteBuffer ( MessageBuffer &&  buffer)

Definition at line 28 of file ByteBuffer.cpp.

28 : _rpos(0), _wpos(0), _bitpos(InitialBitPos), _curbitval(0), _storage(buffer.Move())
29{
30}
std::vector< uint8 > && Move()

◆ ~ByteBuffer()

virtual ByteBuffer::~ByteBuffer ( )
virtualdefault

Member Function Documentation

◆ append() [1/6]

void ByteBuffer::append ( ByteBuffer const &  buffer)
inline

Definition at line 572 of file ByteBuffer.h.

573 {
574 if (!buffer.empty())
575 append(buffer.contents(), buffer.size());
576 }
void append(T value)
Definition: ByteBuffer.h:143
+ Here is the call graph for this function:

◆ append() [2/6]

void ByteBuffer::append ( const char *  src,
size_t  cnt 
)
inline

Definition at line 559 of file ByteBuffer.h.

560 {
561 return append((const uint8 *)src, cnt);
562 }
uint8_t uint8
Definition: Define.h:145

◆ append() [3/6]

template<class T >
void ByteBuffer::append ( const T *  src,
size_t  cnt 
)
inline

Definition at line 565 of file ByteBuffer.h.

566 {
567 return append((const uint8 *)src, cnt * sizeof(T));
568 }

◆ append() [4/6]

template<size_t Size>
void ByteBuffer::append ( std::array< uint8, Size > const &  arr)
inline

Definition at line 579 of file ByteBuffer.h.

580 {
581 append(arr.data(), Size);
582 }

◆ append() [5/6]

template<typename T >
void ByteBuffer::append ( value)
inline

Definition at line 143 of file ByteBuffer.h.

144 {
145 static_assert(std::is_trivially_copyable<T>::value, "append(T) must be used with trivially copyable types");
146 EndianConvert(value);
147 append((uint8 *)&value, sizeof(value));
148 }
void EndianConvert(T &val)
Definition: ByteConverter.h:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ append() [6/6]

void ByteBuffer::append ( uint8 const *  src,
size_t  cnt 
)

Definition at line 110 of file ByteBuffer.cpp.

111{
112 ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size());
113 ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", _wpos, size());
114 ASSERT(size() < 10000000);
115
116 FlushBits();
117
118 size_t const newSize = _wpos + cnt;
119 if (_storage.capacity() < newSize) // custom memory allocation rules
120 {
121 if (newSize < 100)
122 _storage.reserve(300);
123 else if (newSize < 750)
124 _storage.reserve(2500);
125 else if (newSize < 6000)
126 _storage.reserve(10000);
127 else
128 _storage.reserve(400000);
129 }
130
131 if (_storage.size() < newSize)
132 _storage.resize(newSize);
133 std::memcpy(&_storage[_wpos], src, cnt);
134 _wpos = newSize;
135}
#define SZFMTD
Definition: Define.h:133
#define ASSERT
Definition: Errors.h:68
void FlushBits()
Definition: ByteBuffer.h:155
+ Here is the call graph for this function:

◆ AppendPackedTime()

void ByteBuffer::AppendPackedTime ( time_t  time)

Definition at line 137 of file ByteBuffer.cpp.

138{
139 tm lt;
140 localtime_r(&time, &lt);
141 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);
142}
+ Here is the caller graph for this function:

◆ AppendPackedUInt64()

void ByteBuffer::AppendPackedUInt64 ( uint64  guid)
inline

Definition at line 594 of file ByteBuffer.h.

595 {
596 uint8 mask = 0;
597 size_t pos = wpos();
598 *this << uint8(mask);
599
600 uint8 packed[8];
601 if (size_t packedSize = PackUInt64(guid, &mask, packed))
602 append(packed, packedSize);
603
604 put<uint8>(pos, mask);
605 }
static size_t PackUInt64(uint64 value, uint8 *mask, uint8 *result)
Definition: ByteBuffer.h:607
size_t wpos() const
Definition: ByteBuffer.h:412

◆ appendPackXYZ()

void ByteBuffer::appendPackXYZ ( float  x,
float  y,
float  z 
)
inline

Definition at line 585 of file ByteBuffer.h.

586 {
587 uint32 packed = 0;
588 packed |= ((int)(x / 0.25f) & 0x7FF);
589 packed |= ((int)(y / 0.25f) & 0x7FF) << 11;
590 packed |= ((int)(z / 0.25f) & 0x3FF) << 22;
591 *this << packed;
592 }
uint32_t uint32
Definition: Define.h:143
+ Here is the caller graph for this function:

◆ bitwpos() [1/2]

size_t ByteBuffer::bitwpos ( ) const
inline

Returns position of last written bit.

Definition at line 421 of file ByteBuffer.h.

421{ return _wpos * 8 + 8 - _bitpos; }

◆ bitwpos() [2/2]

size_t ByteBuffer::bitwpos ( size_t  newPos)
inline

Definition at line 423 of file ByteBuffer.h.

424 {
425 _wpos = newPos / 8;
426 _bitpos = 8 - (newPos % 8);
427 return _wpos * 8 + 8 - _bitpos;
428 }

◆ clear()

void ByteBuffer::clear ( )
inline

Definition at line 133 of file ByteBuffer.h.

134 {
135 _rpos = 0;
136 _wpos = 0;
138 _curbitval = 0;
139 _storage.clear();
140 }
+ Here is the caller graph for this function:

◆ contents() [1/2]

uint8 * ByteBuffer::contents ( )
inline

Definition at line 524 of file ByteBuffer.h.

525 {
526 if (_storage.empty())
527 throw ByteBufferException();
528 return _storage.data();
529 }
+ Here is the caller graph for this function:

◆ contents() [2/2]

uint8 const * ByteBuffer::contents ( ) const
inline

Definition at line 531 of file ByteBuffer.h.

532 {
533 if (_storage.empty())
534 throw ByteBufferException();
535 return _storage.data();
536 }

◆ empty()

bool ByteBuffer::empty ( ) const
inline

Definition at line 539 of file ByteBuffer.h.

539{ return _storage.empty(); }
+ Here is the caller graph for this function:

◆ FlushBits()

void ByteBuffer::FlushBits ( )
inline

Definition at line 155 of file ByteBuffer.h.

156 {
157 if (_bitpos == 8)
158 return;
159
160 _bitpos = 8;
161
162 append((uint8 *)&_curbitval, sizeof(uint8));
163 _curbitval = 0;
164 }

◆ HasUnfinishedBitPack()

bool ByteBuffer::HasUnfinishedBitPack ( ) const
inline

Definition at line 150 of file ByteBuffer.h.

151 {
152 return _bitpos != 8;
153 }
+ Here is the caller graph for this function:

◆ hexlike()

void ByteBuffer::hexlike ( ) const

Definition at line 200 of file ByteBuffer.cpp.

201{
202 if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
203 return;
204
205 uint32 j = 1, k = 1;
206
207 std::ostringstream o;
208 o << "STORAGE_SIZE: " << size();
209
210 for (uint32 i = 0; i < size(); ++i)
211 {
212 char buf[4];
213 snprintf(buf, 4, "%2X", read<uint8>(i));
214 if ((i == (j * 8)) && ((i != (k * 16))))
215 {
216 o << "| ";
217 ++j;
218 }
219 else if (i == (k * 16))
220 {
221 o << "\n";
222 ++k;
223 ++j;
224 }
225
226 o << buf;
227 }
228 o << " ";
229 TC_LOG_TRACE("network", "{}", o.str());
230}
@ LOG_LEVEL_TRACE
Definition: LogCommon.h:27
#define TC_LOG_TRACE(filterType__,...)
Definition: Log.h:153
#define sLog
Definition: Log.h:130
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Move()

std::vector< uint8 > && ByteBuffer::Move ( )
inlinenoexcept

Definition at line 94 of file ByteBuffer.h.

95 {
96 _rpos = 0;
97 _wpos = 0;
99 _curbitval = 0;
100 return std::move(_storage);
101 }

◆ operator<<() [1/13]

ByteBuffer & ByteBuffer::operator<< ( char const *  str)
inline

Definition at line 316 of file ByteBuffer.h.

317 {
318 return operator<<(std::string_view(str ? str : ""));
319 }
ByteBuffer & operator<<(uint8 value)
Definition: ByteBuffer.h:241
+ Here is the call graph for this function:

◆ operator<<() [2/13]

ByteBuffer & ByteBuffer::operator<< ( double  value)
inline

Definition at line 297 of file ByteBuffer.h.

298 {
299 append<double>(value);
300 return *this;
301 }

◆ operator<<() [3/13]

ByteBuffer & ByteBuffer::operator<< ( float  value)
inline

Definition at line 291 of file ByteBuffer.h.

292 {
293 append<float>(value);
294 return *this;
295 }

◆ operator<<() [4/13]

ByteBuffer & ByteBuffer::operator<< ( int16  value)
inline

Definition at line 272 of file ByteBuffer.h.

273 {
274 append<int16>(value);
275 return *this;
276 }

◆ operator<<() [5/13]

ByteBuffer & ByteBuffer::operator<< ( int32  value)
inline

Definition at line 278 of file ByteBuffer.h.

279 {
280 append<int32>(value);
281 return *this;
282 }

◆ operator<<() [6/13]

ByteBuffer & ByteBuffer::operator<< ( int64  value)
inline

Definition at line 284 of file ByteBuffer.h.

285 {
286 append<int64>(value);
287 return *this;
288 }

◆ operator<<() [7/13]

ByteBuffer & ByteBuffer::operator<< ( int8  value)
inline

Definition at line 266 of file ByteBuffer.h.

267 {
268 append<int8>(value);
269 return *this;
270 }

◆ operator<<() [8/13]

ByteBuffer & ByteBuffer::operator<< ( std::string const &  str)
inline

Definition at line 311 of file ByteBuffer.h.

312 {
313 return operator<<(std::string_view(str));
314 }
+ Here is the call graph for this function:

◆ operator<<() [9/13]

ByteBuffer & ByteBuffer::operator<< ( std::string_view  value)
inline

Definition at line 303 of file ByteBuffer.h.

304 {
305 if (size_t len = value.length())
306 append(reinterpret_cast<uint8 const*>(value.data()), len);
307 append(static_cast<uint8>(0));
308 return *this;
309 }

◆ operator<<() [10/13]

ByteBuffer & ByteBuffer::operator<< ( uint16  value)
inline

Definition at line 247 of file ByteBuffer.h.

248 {
249 append<uint16>(value);
250 return *this;
251 }

◆ operator<<() [11/13]

ByteBuffer & ByteBuffer::operator<< ( uint32  value)
inline

Definition at line 253 of file ByteBuffer.h.

254 {
255 append<uint32>(value);
256 return *this;
257 }

◆ operator<<() [12/13]

ByteBuffer & ByteBuffer::operator<< ( uint64  value)
inline

Definition at line 259 of file ByteBuffer.h.

260 {
261 append<uint64>(value);
262 return *this;
263 }

◆ operator<<() [13/13]

ByteBuffer & ByteBuffer::operator<< ( uint8  value)
inline

Definition at line 241 of file ByteBuffer.h.

242 {
243 append<uint8>(value);
244 return *this;
245 }

◆ operator=() [1/2]

ByteBuffer & ByteBuffer::operator= ( ByteBuffer &&  right)
inlinenoexcept

Definition at line 117 of file ByteBuffer.h.

118 {
119 if (this != &right)
120 {
121 _rpos = right._rpos;
122 _wpos = right._wpos;
123 _bitpos = right._bitpos;
124 _curbitval = right._curbitval;
125 _storage = right.Move();
126 }
127
128 return *this;
129 }

◆ operator=() [2/2]

ByteBuffer & ByteBuffer::operator= ( ByteBuffer const &  right)
inline

Definition at line 103 of file ByteBuffer.h.

104 {
105 if (this != &right)
106 {
107 _rpos = right._rpos;
108 _wpos = right._wpos;
109 _bitpos = right._bitpos;
110 _curbitval = right._curbitval;
111 _storage = right._storage;
112 }
113
114 return *this;
115 }
+ Here is the caller graph for this function:

◆ operator>>() [1/12]

ByteBuffer & ByteBuffer::operator>> ( bool &  value)
inline

Definition at line 321 of file ByteBuffer.h.

322 {
323 value = read<char>() > 0 ? true : false;
324 return *this;
325 }

◆ operator>>() [2/12]

ByteBuffer & ByteBuffer::operator>> ( double &  value)

Definition at line 56 of file ByteBuffer.cpp.

57{
58 value = read<double>();
59 if (!std::isfinite(value))
60 throw ByteBufferInvalidValueException("double", "infinity");
61 return *this;
62}

◆ operator>>() [3/12]

ByteBuffer & ByteBuffer::operator>> ( float &  value)

Definition at line 48 of file ByteBuffer.cpp.

49{
50 value = read<float>();
51 if (!std::isfinite(value))
52 throw ByteBufferInvalidValueException("float", "infinity");
53 return *this;
54}

◆ operator>>() [4/12]

ByteBuffer & ByteBuffer::operator>> ( int16 value)
inline

Definition at line 358 of file ByteBuffer.h.

359 {
360 value = read<int16>();
361 return *this;
362 }

◆ operator>>() [5/12]

ByteBuffer & ByteBuffer::operator>> ( int32 value)
inline

Definition at line 364 of file ByteBuffer.h.

365 {
366 value = read<int32>();
367 return *this;
368 }

◆ operator>>() [6/12]

ByteBuffer & ByteBuffer::operator>> ( int64 value)
inline

Definition at line 370 of file ByteBuffer.h.

371 {
372 value = read<int64>();
373 return *this;
374 }

◆ operator>>() [7/12]

ByteBuffer & ByteBuffer::operator>> ( int8 value)
inline

Definition at line 352 of file ByteBuffer.h.

353 {
354 value = read<int8>();
355 return *this;
356 }

◆ operator>>() [8/12]

ByteBuffer & ByteBuffer::operator>> ( std::string &  value)
inline

Definition at line 379 of file ByteBuffer.h.

380 {
381 value = ReadCString(true);
382 return *this;
383 }
std::string ReadCString(bool requireValidUtf8=true)
Definition: ByteBuffer.cpp:64

◆ operator>>() [9/12]

ByteBuffer & ByteBuffer::operator>> ( uint16 value)
inline

Definition at line 333 of file ByteBuffer.h.

334 {
335 value = read<uint16>();
336 return *this;
337 }

◆ operator>>() [10/12]

ByteBuffer & ByteBuffer::operator>> ( uint32 value)
inline

Definition at line 339 of file ByteBuffer.h.

340 {
341 value = read<uint32>();
342 return *this;
343 }

◆ operator>>() [11/12]

ByteBuffer & ByteBuffer::operator>> ( uint64 value)
inline

Definition at line 345 of file ByteBuffer.h.

346 {
347 value = read<uint64>();
348 return *this;
349 }

◆ operator>>() [12/12]

ByteBuffer & ByteBuffer::operator>> ( uint8 value)
inline

Definition at line 327 of file ByteBuffer.h.

328 {
329 value = read<uint8>();
330 return *this;
331 }

◆ operator[]() [1/2]

uint8 & ByteBuffer::operator[] ( size_t const  pos)
inline

Definition at line 385 of file ByteBuffer.h.

386 {
387 if (pos >= size())
388 throw ByteBufferPositionException(pos, 1, size());
389 return _storage[pos];
390 }

◆ operator[]() [2/2]

uint8 const & ByteBuffer::operator[] ( size_t const  pos) const
inline

Definition at line 392 of file ByteBuffer.h.

393 {
394 if (pos >= size())
395 throw ByteBufferPositionException(pos, 1, size());
396 return _storage[pos];
397 }

◆ PackUInt64()

static size_t ByteBuffer::PackUInt64 ( uint64  value,
uint8 mask,
uint8 result 
)
inlinestatic

Definition at line 607 of file ByteBuffer.h.

608 {
609 size_t resultSize = 0;
610 *mask = 0;
611 memset(result, 0, 8);
612
613 for (uint8 i = 0; value != 0; ++i)
614 {
615 if (value & 0xFF)
616 {
617 *mask |= uint8(1 << i);
618 result[resultSize++] = uint8(value & 0xFF);
619 }
620
621 value >>= 8;
622 }
623
624 return resultSize;
625 }

◆ print_storage()

void ByteBuffer::print_storage ( ) const

Definition at line 169 of file ByteBuffer.cpp.

170{
171 if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
172 return;
173
174 std::ostringstream o;
175 o << "STORAGE_SIZE: " << size();
176 for (uint32 i = 0; i < size(); ++i)
177 o << read<uint8>(i) << " - ";
178 o << " ";
179
180 TC_LOG_TRACE("network", "{}", o.str());
181}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ put() [1/2]

void ByteBuffer::put ( size_t  pos,
uint8 const *  src,
size_t  cnt 
)

Definition at line 144 of file ByteBuffer.cpp.

145{
146 ASSERT(pos + cnt <= size(), "Attempted to put value with size: " SZFMTD " in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", cnt, pos, size());
147 ASSERT(src, "Attempted to put a NULL-pointer in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size());
148 ASSERT(cnt, "Attempted to put a zero-sized value in ByteBuffer (pos: " SZFMTD " size: " SZFMTD ")", pos, size());
149
150 std::memcpy(&_storage[pos], src, cnt);
151}
+ Here is the call graph for this function:

◆ put() [2/2]

template<typename T >
void ByteBuffer::put ( std::size_t  pos,
value 
)
inline

Definition at line 220 of file ByteBuffer.h.

221 {
222 static_assert(std::is_trivially_copyable<T>::value, "put(size_t, T) must be used with trivially copyable types");
223 EndianConvert(value);
224 put(pos, (uint8 *)&value, sizeof(value));
225 }
void put(std::size_t pos, T value)
Definition: ByteBuffer.h:220
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ PutBits()

void ByteBuffer::PutBits ( std::size_t  pos,
std::size_t  value,
uint32  bitCount 
)

Definition at line 153 of file ByteBuffer.cpp.

154{
155 ASSERT(pos + bitCount <= size() * 8, "Attempted to put %u bits in ByteBuffer (bitpos: " SZFMTD " size: " SZFMTD ")", bitCount, pos, size());
156 ASSERT(bitCount, "Attempted to put a zero bits in ByteBuffer");
157
158 for (uint32 i = 0; i < bitCount; ++i)
159 {
160 std::size_t wp = (pos + i) / 8;
161 std::size_t bit = (pos + i) % 8;
162 if ((value >> (bitCount - i - 1)) & 1)
163 _storage[wp] |= 1 << (7 - bit);
164 else
165 _storage[wp] &= ~(1 << (7 - bit));
166 }
167}
+ Here is the call graph for this function:

◆ read() [1/5]

template<typename T , typename Underlying = T>
T ByteBuffer::read ( )
inline

Definition at line 443 of file ByteBuffer.h.

444 {
445 ResetBitPos();
446 T r = read<T, Underlying>(_rpos);
447 _rpos += sizeof(Underlying);
448 return r;
449 }
void ResetBitPos()
Definition: ByteBuffer.h:166
+ 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

Definition at line 452 of file ByteBuffer.h.

453 {
454 if (pos + sizeof(Underlying) > size())
455 throw ByteBufferPositionException(pos, sizeof(Underlying), size());
456 Underlying val;
457 std::memcpy(&val, &_storage[pos], sizeof(Underlying));
458 EndianConvert(val);
459 return static_cast<T>(val);
460 }
+ Here is the call graph for this function:

◆ read() [3/5]

template<size_t Size>
void ByteBuffer::read ( std::array< uint8, Size > &  arr)
inline

Definition at line 480 of file ByteBuffer.h.

481 {
482 read(arr.data(), Size);
483 }

◆ read() [4/5]

template<class T >
void ByteBuffer::read ( T *  dest,
size_t  count 
)
inline

Definition at line 463 of file ByteBuffer.h.

464 {
465 static_assert(std::is_trivially_copyable<T>::value, "read(T*, size_t) must be used with trivially copyable types");
466 return read(reinterpret_cast<uint8*>(dest), count * sizeof(T));
467 }

◆ read() [5/5]

void ByteBuffer::read ( uint8 dest,
size_t  len 
)
inline

Definition at line 469 of file ByteBuffer.h.

470 {
471 if (_rpos + len > size())
473
474 ResetBitPos();
475 std::memcpy(dest, &_storage[_rpos], len);
476 _rpos += len;
477 }

◆ read_skip() [1/4]

template<typename T >
void ByteBuffer::read_skip ( )
inline

Definition at line 431 of file ByteBuffer.h.

431{ read_skip(sizeof(T)); }
void read_skip()
Definition: ByteBuffer.h:431
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read_skip() [2/4]

template<>
void ByteBuffer::read_skip ( )
inline

Definition at line 652 of file ByteBuffer.h.

653{
654 std::string temp;
655 *this >> temp;
656}

◆ read_skip() [3/4]

template<>
void ByteBuffer::read_skip ( )
inline

Definition at line 659 of file ByteBuffer.h.

660{
661 read_skip<char*>();
662}

◆ read_skip() [4/4]

void ByteBuffer::read_skip ( size_t  skip)
inline

Definition at line 433 of file ByteBuffer.h.

434 {
435 if (_rpos + skip > size())
436 throw ByteBufferPositionException(_rpos, skip, size());
437
438 ResetBitPos();
439 _rpos += skip;
440 }

◆ ReadBit()

bool ByteBuffer::ReadBit ( )
inline

Definition at line 191 of file ByteBuffer.h.

192 {
193 ++_bitpos;
194 if (_bitpos > 7)
195 {
196 _curbitval = read<uint8>();
197 _bitpos = 0;
198 }
199
200 return ((_curbitval >> (7-_bitpos)) & 1) != 0;
201 }
+ Here is the caller graph for this function:

◆ ReadBits()

uint32 ByteBuffer::ReadBits ( int32  bits)
inline

Definition at line 209 of file ByteBuffer.h.

210 {
211 uint32 value = 0;
212 for (int32 i = bits - 1; i >= 0; --i)
213 if (ReadBit())
214 value |= (1 << (i));
215
216 return value;
217 }
int32_t int32
Definition: Define.h:139
bool ReadBit()
Definition: ByteBuffer.h:191
+ Here is the caller graph for this function:

◆ ReadCString()

std::string ByteBuffer::ReadCString ( bool  requireValidUtf8 = true)

Definition at line 64 of file ByteBuffer.cpp.

65{
66 std::string value;
67 while (rpos() < size()) // prevent crash at wrong string format in packet
68 {
69 char c = read<char>();
70 if (c == 0)
71 break;
72 value += c;
73 }
74 if (requireValidUtf8 && !utf8::is_valid(value.begin(), value.end()))
75 throw ByteBufferInvalidValueException("string", value.c_str());
76 return value;
77}
size_t rpos() const
Definition: ByteBuffer.h:399
+ Here is the call graph for this function:

◆ ReadPackedTime()

uint32 ByteBuffer::ReadPackedTime ( )

Definition at line 95 of file ByteBuffer.cpp.

96{
97 uint32 packedDate = read<uint32>();
98 tm lt = tm();
99
100 lt.tm_min = packedDate & 0x3F;
101 lt.tm_hour = (packedDate >> 6) & 0x1F;
102 //lt.tm_wday = (packedDate >> 11) & 7;
103 lt.tm_mday = ((packedDate >> 14) & 0x3F) + 1;
104 lt.tm_mon = (packedDate >> 20) & 0xF;
105 lt.tm_year = ((packedDate >> 24) & 0x1F) + 100;
106
107 return uint32(mktime(&lt));
108}
+ Here is the caller graph for this function:

◆ ReadPackedUInt64() [1/2]

void ByteBuffer::ReadPackedUInt64 ( uint64 guid)
inline

Definition at line 485 of file ByteBuffer.h.

486 {
487 guid = 0;
488 ReadPackedUInt64(read<uint8>(), guid);
489 }
void ReadPackedUInt64(uint64 &guid)
Definition: ByteBuffer.h:485

◆ ReadPackedUInt64() [2/2]

void ByteBuffer::ReadPackedUInt64 ( uint8  mask,
uint64 value 
)
inline

Definition at line 491 of file ByteBuffer.h.

492 {
493 for (uint32 i = 0; i < 8; ++i)
494 if (mask & (uint8(1) << i))
495 value |= (uint64(read<uint8>()) << (i * 8));
496 }
uint64_t uint64
Definition: Define.h:142

◆ ReadString()

std::string ByteBuffer::ReadString ( uint32  length,
bool  requireValidUtf8 = true 
)

Definition at line 79 of file ByteBuffer.cpp.

80{
81 if (_rpos + length > size())
82 throw ByteBufferPositionException(_rpos, length, size());
83
85 if (!length)
86 return std::string();
87
88 std::string value(reinterpret_cast<char const*>(&_storage[_rpos]), length);
89 _rpos += length;
90 if (requireValidUtf8 && !utf8::is_valid(value.begin(), value.end()))
91 throw ByteBufferInvalidValueException("string", value.c_str());
92 return value;
93}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reserve()

void ByteBuffer::reserve ( size_t  ressize)
inline

Definition at line 548 of file ByteBuffer.h.

549 {
550 if (ressize > size())
551 _storage.reserve(ressize);
552 }
+ Here is the caller graph for this function:

◆ ResetBitPos()

void ByteBuffer::ResetBitPos ( )
inline

Definition at line 166 of file ByteBuffer.h.

167 {
168 if (_bitpos > 7)
169 return;
170
171 _bitpos = 8;
172 _curbitval = 0;
173 }
+ Here is the caller graph for this function:

◆ resize()

void ByteBuffer::resize ( size_t  newsize)
inline

Definition at line 541 of file ByteBuffer.h.

542 {
543 _storage.resize(newsize, 0);
544 _rpos = 0;
545 _wpos = size();
546 }
+ Here is the caller graph for this function:

◆ rfinish()

void ByteBuffer::rfinish ( )
inline

Definition at line 407 of file ByteBuffer.h.

408 {
409 _rpos = wpos();
410 }
+ Here is the caller graph for this function:

◆ rpos() [1/2]

size_t ByteBuffer::rpos ( ) const
inline

Definition at line 399 of file ByteBuffer.h.

399{ return _rpos; }
+ Here is the caller graph for this function:

◆ rpos() [2/2]

size_t ByteBuffer::rpos ( size_t  rpos_)
inline

Definition at line 401 of file ByteBuffer.h.

402 {
403 _rpos = rpos_;
404 return _rpos;
405 }

◆ shrink_to_fit()

void ByteBuffer::shrink_to_fit ( )
inline

Definition at line 554 of file ByteBuffer.h.

555 {
556 _storage.shrink_to_fit();
557 }

◆ size()

size_t ByteBuffer::size ( ) const
inline

Definition at line 538 of file ByteBuffer.h.

538{ return _storage.size(); }
+ Here is the caller graph for this function:

◆ textlike()

void ByteBuffer::textlike ( ) const

Definition at line 183 of file ByteBuffer.cpp.

184{
185 if (!sLog->ShouldLog("network", LOG_LEVEL_TRACE)) // optimize disabled trace output
186 return;
187
188 std::ostringstream o;
189 o << "STORAGE_SIZE: " << size();
190 for (uint32 i = 0; i < size(); ++i)
191 {
192 char buf[2];
193 snprintf(buf, 2, "%c", read<uint8>(i));
194 o << buf;
195 }
196 o << " ";
197 TC_LOG_TRACE("network", "{}", o.str());
198}
+ Here is the call graph for this function:

◆ wpos() [1/2]

size_t ByteBuffer::wpos ( ) const
inline

Definition at line 412 of file ByteBuffer.h.

412{ return _wpos; }
+ Here is the caller graph for this function:

◆ wpos() [2/2]

size_t ByteBuffer::wpos ( size_t  wpos_)
inline

Definition at line 414 of file ByteBuffer.h.

415 {
416 _wpos = wpos_;
417 return _wpos;
418 }

◆ WriteBit()

bool ByteBuffer::WriteBit ( bool  bit)
inline

Definition at line 175 of file ByteBuffer.h.

176 {
177 --_bitpos;
178 if (bit)
179 _curbitval |= (1 << (_bitpos));
180
181 if (_bitpos == 0)
182 {
183 _bitpos = 8;
184 append((uint8 *)&_curbitval, sizeof(_curbitval));
185 _curbitval = 0;
186 }
187
188 return bit;
189 }
+ Here is the caller graph for this function:

◆ WriteBits()

void ByteBuffer::WriteBits ( std::size_t  value,
int32  bits 
)
inline

Definition at line 203 of file ByteBuffer.h.

204 {
205 for (int32 i = bits - 1; i >= 0; --i)
206 WriteBit((value >> i) & 1);
207 }
bool WriteBit(bool bit)
Definition: ByteBuffer.h:175
+ Here is the caller graph for this function:

◆ WriteString() [1/3]

void ByteBuffer::WriteString ( char const *  str,
size_t  len 
)
inline

Definition at line 512 of file ByteBuffer.h.

513 {
514 if (len)
515 append(str, len);
516 }

◆ WriteString() [2/3]

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

Definition at line 500 of file ByteBuffer.h.

501 {
502 if (size_t len = str.length())
503 append(str.c_str(), len);
504 }
+ Here is the caller graph for this function:

◆ WriteString() [3/3]

void ByteBuffer::WriteString ( std::string_view  str)
inline

Definition at line 506 of file ByteBuffer.h.

507 {
508 if (size_t len = str.length())
509 append(str.data(), len);
510 }

Member Data Documentation

◆ _bitpos

size_t ByteBuffer::_bitpos
protected

Definition at line 638 of file ByteBuffer.h.

◆ _curbitval

uint8 ByteBuffer::_curbitval
protected

Definition at line 639 of file ByteBuffer.h.

◆ _rpos

size_t ByteBuffer::_rpos
protected

Definition at line 638 of file ByteBuffer.h.

◆ _storage

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

Definition at line 640 of file ByteBuffer.h.

◆ _wpos

size_t ByteBuffer::_wpos
protected

Definition at line 638 of file ByteBuffer.h.

◆ DEFAULT_SIZE

constexpr size_t ByteBuffer::DEFAULT_SIZE = 0x1000
staticconstexpr

Definition at line 64 of file ByteBuffer.h.

◆ InitialBitPos

constexpr uint8 ByteBuffer::InitialBitPos = 8
staticconstexpr

Definition at line 65 of file ByteBuffer.h.


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