[8315] Fixed memory leaks (mostly at server shutdown) and code cleanups.
[getmangos.git] / src / shared / ByteBuffer.h
blob3d59a84692fc3af6de3e67bee1431fce75e47623
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef _BYTEBUFFER_H
20 #define _BYTEBUFFER_H
22 #include "Common.h"
23 #include "Errors.h"
24 #include "Log.h"
25 #include "Utilities/ByteConverter.h"
27 class ByteBuffer
29 public:
30 const static size_t DEFAULT_SIZE = 0x1000;
32 // constructor
33 ByteBuffer(): _rpos(0), _wpos(0)
35 _storage.reserve(DEFAULT_SIZE);
38 // constructor
39 ByteBuffer(size_t res): _rpos(0), _wpos(0)
41 _storage.reserve(res);
44 // copy constructor
45 ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage) { }
47 void clear()
49 _storage.clear();
50 _rpos = _wpos = 0;
53 template <typename T> void append(T value)
55 EndianConvert(value);
56 append((uint8 *)&value, sizeof(value));
59 template <typename T> void put(size_t pos,T value)
61 EndianConvert(value);
62 put(pos,(uint8 *)&value,sizeof(value));
65 ByteBuffer &operator<<(uint8 value)
67 append<uint8>(value);
68 return *this;
71 ByteBuffer &operator<<(uint16 value)
73 append<uint16>(value);
74 return *this;
77 ByteBuffer &operator<<(uint32 value)
79 append<uint32>(value);
80 return *this;
83 ByteBuffer &operator<<(uint64 value)
85 append<uint64>(value);
86 return *this;
89 // signed as in 2e complement
90 ByteBuffer &operator<<(int8 value)
92 append<int8>(value);
93 return *this;
96 ByteBuffer &operator<<(int16 value)
98 append<int16>(value);
99 return *this;
102 ByteBuffer &operator<<(int32 value)
104 append<int32>(value);
105 return *this;
108 ByteBuffer &operator<<(int64 value)
110 append<int64>(value);
111 return *this;
114 // floating points
115 ByteBuffer &operator<<(float value)
117 append<float>(value);
118 return *this;
121 ByteBuffer &operator<<(double value)
123 append<double>(value);
124 return *this;
127 ByteBuffer &operator<<(const std::string &value)
129 append((uint8 const *)value.c_str(), value.length());
130 append((uint8)0);
131 return *this;
134 ByteBuffer &operator<<(const char *str)
136 append((uint8 const *)str, str ? strlen(str) : 0);
137 append((uint8)0);
138 return *this;
141 ByteBuffer &operator>>(bool &value)
143 value = read<char>() > 0 ? true : false;
144 return *this;
147 ByteBuffer &operator>>(uint8 &value)
149 value = read<uint8>();
150 return *this;
153 ByteBuffer &operator>>(uint16 &value)
155 value = read<uint16>();
156 return *this;
159 ByteBuffer &operator>>(uint32 &value)
161 value = read<uint32>();
162 return *this;
165 ByteBuffer &operator>>(uint64 &value)
167 value = read<uint64>();
168 return *this;
171 //signed as in 2e complement
172 ByteBuffer &operator>>(int8 &value)
174 value = read<int8>();
175 return *this;
178 ByteBuffer &operator>>(int16 &value)
180 value = read<int16>();
181 return *this;
184 ByteBuffer &operator>>(int32 &value)
186 value = read<int32>();
187 return *this;
190 ByteBuffer &operator>>(int64 &value)
192 value = read<int64>();
193 return *this;
196 ByteBuffer &operator>>(float &value)
198 value = read<float>();
199 return *this;
202 ByteBuffer &operator>>(double &value)
204 value = read<double>();
205 return *this;
208 ByteBuffer &operator>>(std::string& value)
210 value.clear();
211 while (rpos() < size()) // prevent crash at wrong string format in packet
213 char c = read<char>();
214 if (c == 0)
215 break;
216 value += c;
218 return *this;
221 uint8 operator[](size_t pos) const
223 return read<uint8>(pos);
226 size_t rpos() const { return _rpos; }
228 size_t rpos(size_t rpos_)
230 _rpos = rpos_;
231 return _rpos;
234 size_t wpos() const { return _wpos; }
236 size_t wpos(size_t wpos_)
238 _wpos = wpos_;
239 return _wpos;
242 template <typename T> T read()
244 T r = read<T>(_rpos);
245 _rpos += sizeof(T);
246 return r;
249 template <typename T> T read(size_t pos) const
251 ASSERT(pos + sizeof(T) <= size() || PrintPosError(false, pos, sizeof(T)));
252 T val = *((T const*)&_storage[pos]);
253 EndianConvert(val);
254 return val;
257 void read(uint8 *dest, size_t len)
259 ASSERT(_rpos + len <= size() || PrintPosError(false, _rpos, len));
260 memcpy(dest, &_storage[_rpos], len);
261 _rpos += len;
264 bool readPackGUID(uint64& guid)
266 if(rpos() + 1 > size())
267 return false;
269 guid = 0;
271 uint8 guidmark = 0;
272 (*this) >> guidmark;
274 for(int i = 0; i < 8; ++i)
276 if(guidmark & (uint8(1) << i))
278 if(rpos() + 1 > size())
279 return false;
281 uint8 bit;
282 (*this) >> bit;
283 guid |= (uint64(bit) << (i * 8));
287 return true;
290 const uint8 *contents() const { return &_storage[0]; }
292 size_t size() const { return _storage.size(); }
293 bool empty() const { return _storage.empty(); }
295 void resize(size_t newsize)
297 _storage.resize(newsize);
298 _rpos = 0;
299 _wpos = size();
302 void reserve(size_t ressize)
304 if (ressize > size())
305 _storage.reserve(ressize);
308 void append(const std::string& str)
310 append((uint8 const*)str.c_str(), str.size() + 1);
313 void append(const char *src, size_t cnt)
315 return append((const uint8 *)src, cnt);
318 template<class T> void append(const T *src, size_t cnt)
320 return append((const uint8 *)src, cnt * sizeof(T));
323 void append(const uint8 *src, size_t cnt)
325 if (!cnt)
326 return;
328 ASSERT(size() < 10000000);
330 if (_storage.size() < _wpos + cnt)
331 _storage.resize(_wpos + cnt);
332 memcpy(&_storage[_wpos], src, cnt);
333 _wpos += cnt;
336 void append(const ByteBuffer& buffer)
338 if(buffer.wpos())
339 append(buffer.contents(), buffer.wpos());
342 // can be used in SMSG_MONSTER_MOVE opcode
343 void appendPackXYZ(float x, float y, float z)
345 uint32 packed = 0;
346 packed |= ((int)(x / 0.25f) & 0x7FF);
347 packed |= ((int)(y / 0.25f) & 0x7FF) << 11;
348 packed |= ((int)(z / 0.25f) & 0x3FF) << 22;
349 *this << packed;
352 void appendPackGUID(uint64 guid)
354 if (_storage.size() < _wpos + sizeof(guid) + 1)
355 _storage.resize(_wpos + sizeof(guid) + 1);
357 size_t mask_position = wpos();
358 *this << uint8(0);
359 for(uint8 i = 0; i < 8; ++i)
361 if(guid & 0xFF)
363 _storage[mask_position] |= uint8(1 << i);
364 *this << uint8(guid & 0xFF);
367 guid >>= 8;
371 void put(size_t pos, const uint8 *src, size_t cnt)
373 ASSERT(pos + cnt <= size() || PrintPosError(true, pos, cnt));
374 memcpy(&_storage[pos], src, cnt);
377 void print_storage() const
379 if(!sLog.IsOutDebug()) // optimize disabled debug output
380 return;
382 sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
383 for(uint32 i = 0; i < size(); ++i)
384 sLog.outDebugInLine("%u - ", read<uint8>(i) );
385 sLog.outDebug(" ");
388 void textlike() const
390 if(!sLog.IsOutDebug()) // optimize disabled debug output
391 return;
393 sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
394 for(uint32 i = 0; i < size(); ++i)
395 sLog.outDebugInLine("%c", read<uint8>(i) );
396 sLog.outDebug(" ");
399 void hexlike() const
401 if(!sLog.IsOutDebug()) // optimize disabled debug output
402 return;
404 uint32 j = 1, k = 1;
405 sLog.outDebug("STORAGE_SIZE: %lu", (unsigned long)size() );
407 if(sLog.IsIncludeTime())
408 sLog.outDebugInLine(" ");
410 for(uint32 i = 0; i < size(); ++i)
412 if ((i == (j * 8)) && ((i != (k * 16))))
414 if (read<uint8>(i) < 0x10)
416 sLog.outDebugInLine("| 0%X ", read<uint8>(i) );
418 else
420 sLog.outDebugInLine("| %X ", read<uint8>(i) );
422 ++j;
424 else if (i == (k * 16))
426 if (read<uint8>(i) < 0x10)
428 sLog.outDebugInLine("\n");
429 if(sLog.IsIncludeTime())
430 sLog.outDebugInLine(" ");
432 sLog.outDebugInLine("0%X ", read<uint8>(i) );
434 else
436 sLog.outDebugInLine("\n");
437 if(sLog.IsIncludeTime())
438 sLog.outDebugInLine(" ");
440 sLog.outDebugInLine("%X ", read<uint8>(i) );
443 ++k;
444 ++j;
446 else
448 if (read<uint8>(i) < 0x10)
450 sLog.outDebugInLine("0%X ", read<uint8>(i) );
452 else
454 sLog.outDebugInLine("%X ", read<uint8>(i) );
458 sLog.outDebugInLine("\n");
461 protected:
462 bool PrintPosError(bool add, size_t pos, size_t esize) const
464 sLog.outError("ERROR: Attempt %s in ByteBuffer (pos: %lu size: %lu) value with size: %lu",(add ? "put" : "get"),(unsigned long)pos, (unsigned long)size(), (unsigned long)esize);
466 // assert must fail after function call
467 return false;
470 size_t _rpos, _wpos;
471 std::vector<uint8> _storage;
474 template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::vector<T> v)
476 b << (uint32)v.size();
477 for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); ++i)
479 b << *i;
481 return b;
484 template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::vector<T> &v)
486 uint32 vsize;
487 b >> vsize;
488 v.clear();
489 while(vsize--)
491 T t;
492 b >> t;
493 v.push_back(t);
495 return b;
498 template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::list<T> v)
500 b << (uint32)v.size();
501 for (typename std::list<T>::iterator i = v.begin(); i != v.end(); ++i)
503 b << *i;
505 return b;
508 template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::list<T> &v)
510 uint32 vsize;
511 b >> vsize;
512 v.clear();
513 while(vsize--)
515 T t;
516 b >> t;
517 v.push_back(t);
519 return b;
522 template <typename K, typename V> ByteBuffer &operator<<(ByteBuffer &b, std::map<K, V> &m)
524 b << (uint32)m.size();
525 for (typename std::map<K, V>::iterator i = m.begin(); i != m.end(); ++i)
527 b << i->first << i->second;
529 return b;
532 template <typename K, typename V> ByteBuffer &operator>>(ByteBuffer &b, std::map<K, V> &m)
534 uint32 msize;
535 b >> msize;
536 m.clear();
537 while(msize--)
539 K k;
540 V v;
541 b >> k >> v;
542 m.insert(make_pair(k, v));
544 return b;
546 #endif