root/trunk/src/shared/ByteBuffer.h @ 44

Revision 44, 12.6 kB (checked in by yumileroy, 17 years ago)

[svn] * Merge Temp dev SVN with Assembla.
* Changes include:

  • Implementation of w12x's Outdoor PvP and Game Event Systems.
  • Temporary removal of IRC Chat Bot (until infinite loop when disabled is fixed).
  • All mangos -> trinity (to convert your mangos_string table, please run mangos_string_to_trinity_string.sql).
  • Improved Config cleanup.
  • And many more changes.

Original author: Seline
Date: 2008-10-14 11:57:03-05:00

Line 
1/*
2 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
3 *
4 * Thanks to the original authors: MaNGOS <http://www.mangosproject.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#ifndef _BYTEBUFFER_H
22#define _BYTEBUFFER_H
23
24#include "Common.h"
25#include "Errors.h"
26#include "Log.h"
27#include "Utilities/ByteConverter.h"
28
29class ByteBuffer
30{
31    public:
32        const static size_t DEFAULT_SIZE = 0x1000;
33
34        // constructor
35        ByteBuffer(): _rpos(0), _wpos(0)
36        {
37            _storage.reserve(DEFAULT_SIZE);
38        }
39        // constructor
40        ByteBuffer(size_t res): _rpos(0), _wpos(0)
41        {
42            _storage.reserve(res);
43        }
44        // copy constructor
45        ByteBuffer(const ByteBuffer &buf): _rpos(buf._rpos), _wpos(buf._wpos), _storage(buf._storage) { }
46
47        void clear()
48        {
49            _storage.clear();
50            _rpos = _wpos = 0;
51        }
52
53        template <typename T> void append(T value)
54        {
55            EndianConvert(value);
56            append((uint8 *)&value, sizeof(value));
57        }
58
59        template <typename T> void put(size_t pos,T value)
60        {
61            EndianConvert(value);
62            put(pos,(uint8 *)&value,sizeof(value));
63        }
64
65        ByteBuffer &operator<<(uint8 value)
66        {
67            append<uint8>(value);
68            return *this;
69        }
70        ByteBuffer &operator<<(uint16 value)
71        {
72            append<uint16>(value);
73            return *this;
74        }
75        ByteBuffer &operator<<(uint32 value)
76        {
77            append<uint32>(value);
78            return *this;
79        }
80        ByteBuffer &operator<<(uint64 value)
81        {
82            append<uint64>(value);
83            return *this;
84        }
85
86        // signed as in 2e complement
87        ByteBuffer &operator<<(int8 value)
88        {
89            append<int8>(value);
90            return *this;
91        }
92        ByteBuffer &operator<<(int16 value)
93        {
94            append<int16>(value);
95            return *this;
96        }
97        ByteBuffer &operator<<(int32 value)
98        {
99            append<int32>(value);
100            return *this;
101        }
102        ByteBuffer &operator<<(int64 value)
103        {
104            append<int64>(value);
105            return *this;
106        }
107
108        // floating points
109        ByteBuffer &operator<<(float value)
110        {
111            append<float>(value);
112            return *this;
113        }
114        ByteBuffer &operator<<(double value)
115        {
116            append<double>(value);
117            return *this;
118        }
119        ByteBuffer &operator<<(const std::string &value)
120        {
121            append((uint8 const *)value.c_str(), value.length());
122            append((uint8)0);
123            return *this;
124        }
125        ByteBuffer &operator<<(const char *str)
126        {
127            append((uint8 const *)str, str ? strlen(str) : 0);
128            append((uint8)0);
129            return *this;
130        }
131
132        ByteBuffer &operator>>(bool &value)
133        {
134            value = read<char>() > 0 ? true : false;
135            return *this;
136        }
137
138        ByteBuffer &operator>>(uint8 &value)
139        {
140            value = read<uint8>();
141            return *this;
142        }
143        ByteBuffer &operator>>(uint16 &value)
144        {
145            value = read<uint16>();
146            return *this;
147        }
148        ByteBuffer &operator>>(uint32 &value)
149        {
150            value = read<uint32>();
151            return *this;
152        }
153        ByteBuffer &operator>>(uint64 &value)
154        {
155            value = read<uint64>();
156            return *this;
157        }
158
159        //signed as in 2e complement
160        ByteBuffer &operator>>(int8 &value)
161        {
162            value = read<int8>();
163            return *this;
164        }
165        ByteBuffer &operator>>(int16 &value)
166        {
167            value = read<int16>();
168            return *this;
169        }
170        ByteBuffer &operator>>(int32 &value)
171        {
172            value = read<int32>();
173            return *this;
174        }
175        ByteBuffer &operator>>(int64 &value)
176        {
177            value = read<int64>();
178            return *this;
179        }
180
181        ByteBuffer &operator>>(float &value)
182        {
183            value = read<float>();
184            return *this;
185        }
186        ByteBuffer &operator>>(double &value)
187        {
188            value = read<double>();
189            return *this;
190        }
191        ByteBuffer &operator>>(std::string& value)
192        {
193            value.clear();
194            while (rpos() < size())                         // prevent crash at wrong string format in packet
195            {
196                char c=read<char>();
197                if (c==0)
198                    break;
199                value+=c;
200            }
201            return *this;
202        }
203
204        uint8 operator[](size_t pos)
205        {
206            return read<uint8>(pos);
207        }
208
209        size_t rpos() const { return _rpos; }
210
211        size_t rpos(size_t rpos_)
212        {
213            _rpos = rpos_;
214            return _rpos;
215        };
216
217        size_t wpos() const { return _wpos; }
218
219        size_t wpos(size_t wpos_)
220        {
221            _wpos = wpos_;
222            return _wpos;
223        }
224
225        template <typename T> T read()
226        {
227            T r=read<T>(_rpos);
228            _rpos += sizeof(T);
229            return r;
230        };
231        template <typename T> T read(size_t pos) const
232        {
233            ASSERT(pos + sizeof(T) <= size() || PrintPosError(false,pos,sizeof(T)));
234            T val = *((T const*)&_storage[pos]);
235            EndianConvert(val);
236            return val;
237        }
238
239        void read(uint8 *dest, size_t len)
240        {
241            ASSERT(_rpos  + len  <= size() || PrintPosError(false,_rpos,len));
242            memcpy(dest, &_storage[_rpos], len);
243            _rpos += len;
244        }
245
246        const uint8 *contents() const { return &_storage[0]; }
247
248        size_t size() const { return _storage.size(); }
249        bool empty() const { return _storage.empty(); }
250
251        void resize(size_t newsize)
252        {
253            _storage.resize(newsize);
254            _rpos = 0;
255            _wpos = size();
256        };
257        void reserve(size_t ressize)
258        {
259            if (ressize > size()) _storage.reserve(ressize);
260        };
261
262        void append(const std::string& str)
263        {
264            append((uint8 const*)str.c_str(),str.size() + 1);
265        }
266        void append(const char *src, size_t cnt)
267        {
268            return append((const uint8 *)src, cnt);
269        }
270        template<class T>
271        void append(const T *src, size_t cnt)
272        {
273            return append((const uint8 *)src, cnt*sizeof(T));
274        }
275        void append(const uint8 *src, size_t cnt)
276        {
277            if (!cnt) return;
278
279            ASSERT(size() < 10000000);
280
281            if (_storage.size() < _wpos + cnt)
282                _storage.resize(_wpos + cnt);
283            memcpy(&_storage[_wpos], src, cnt);
284            _wpos += cnt;
285        }
286        void append(const ByteBuffer& buffer)
287        {
288            if(buffer.size()) append(buffer.contents(),buffer.size());
289        }
290
291        void appendPackGUID(uint64 guid)
292        {
293            size_t mask_position = wpos();
294            *this << uint8(0);
295            for(uint8 i = 0; i < 8; i++)
296            {
297                if(guid & 0xFF)
298                {
299                    _storage[mask_position] |= uint8(1<<i);
300                    *this << uint8(guid & 0xFF);
301                }
302
303                guid >>= 8;
304            }
305        }
306
307        void put(size_t pos, const uint8 *src, size_t cnt)
308        {
309            ASSERT(pos + cnt <= size() || PrintPosError(true,pos,cnt));
310            memcpy(&_storage[pos], src, cnt);
311        }
312        void print_storage()
313        {
314            if(!sLog.IsOutDebug())                          // optimize disabled debug output
315                return;
316
317            sLog.outDebug("STORAGE_SIZE: %u", size() );
318            for(uint32 i = 0; i < size(); i++)
319                sLog.outDebugInLine("%u - ", read<uint8>(i) );
320            sLog.outDebug(" ");
321        }
322
323        void textlike()
324        {
325            if(!sLog.IsOutDebug())                          // optimize disabled debug output
326                return;
327
328            sLog.outDebug("STORAGE_SIZE: %u", size() );
329            for(uint32 i = 0; i < size(); i++)
330                sLog.outDebugInLine("%c", read<uint8>(i) );
331            sLog.outDebug(" ");
332        }
333
334        void hexlike()
335        {
336            if(!sLog.IsOutDebug())                          // optimize disabled debug output
337                return;
338
339            uint32 j = 1, k = 1;
340            sLog.outDebug("STORAGE_SIZE: %u", size() );
341
342            if(sLog.IsIncludeTime())
343                sLog.outDebugInLine("         ");
344
345            for(uint32 i = 0; i < size(); i++)
346            {
347                if ((i == (j*8)) && ((i != (k*16))))
348                {
349                    if (read<uint8>(i) < 0x0F)
350                    {
351                        sLog.outDebugInLine("| 0%X ", read<uint8>(i) );
352                    }
353                    else
354                    {
355                        sLog.outDebugInLine("| %X ", read<uint8>(i) );
356                    }
357                    ++j;
358                }
359                else if (i == (k*16))
360                {
361                    if (read<uint8>(i) < 0x0F)
362                    {
363                        sLog.outDebugInLine("\n");
364                        if(sLog.IsIncludeTime())
365                            sLog.outDebugInLine("         ");
366
367                        sLog.outDebugInLine("0%X ", read<uint8>(i) );
368                    }
369                    else
370                    {
371                        sLog.outDebugInLine("\n");
372                        if(sLog.IsIncludeTime())
373                            sLog.outDebugInLine("         ");
374
375                        sLog.outDebugInLine("%X ", read<uint8>(i) );
376                    }
377
378                    ++k;
379                    ++j;
380                }
381                else
382                {
383                    if (read<uint8>(i) < 0x0F)
384                    {
385                        sLog.outDebugInLine("0%X ", read<uint8>(i) );
386                    }
387                    else
388                    {
389                        sLog.outDebugInLine("%X ", read<uint8>(i) );
390                    }
391                }
392            }
393            sLog.outDebugInLine("\n");
394        }
395
396    protected:
397        bool PrintPosError(bool add, size_t pos, size_t esize) const
398        {
399            sLog.outError("ERROR: Attempt %s in ByteBuffer (pos: %u size: %u) value with size: %u",(add ? "put" : "get"),pos, size(), esize);
400
401            // assert must fail after function call
402            return false;
403        }
404
405        size_t _rpos, _wpos;
406        std::vector<uint8> _storage;
407};
408
409template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::vector<T> v)
410{
411    b << (uint32)v.size();
412    for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++)
413    {
414        b << *i;
415    }
416    return b;
417}
418
419template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::vector<T> &v)
420{
421    uint32 vsize;
422    b >> vsize;
423    v.clear();
424    while(vsize--)
425    {
426        T t;
427        b >> t;
428        v.push_back(t);
429    }
430    return b;
431}
432
433template <typename T> ByteBuffer &operator<<(ByteBuffer &b, std::list<T> v)
434{
435    b << (uint32)v.size();
436    for (typename std::list<T>::iterator i = v.begin(); i != v.end(); i++)
437    {
438        b << *i;
439    }
440    return b;
441}
442
443template <typename T> ByteBuffer &operator>>(ByteBuffer &b, std::list<T> &v)
444{
445    uint32 vsize;
446    b >> vsize;
447    v.clear();
448    while(vsize--)
449    {
450        T t;
451        b >> t;
452        v.push_back(t);
453    }
454    return b;
455}
456
457template <typename K, typename V> ByteBuffer &operator<<(ByteBuffer &b, std::map<K, V> &m)
458{
459    b << (uint32)m.size();
460    for (typename std::map<K, V>::iterator i = m.begin(); i != m.end(); i++)
461    {
462        b << i->first << i->second;
463    }
464    return b;
465}
466
467template <typename K, typename V> ByteBuffer &operator>>(ByteBuffer &b, std::map<K, V> &m)
468{
469    uint32 msize;
470    b >> msize;
471    m.clear();
472    while(msize--)
473    {
474        K k;
475        V v;
476        b >> k >> v;
477        m.insert(make_pair(k, v));
478    }
479    return b;
480}
481#endif
Note: See TracBrowser for help on using the browser.