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

Revision 2, 12.5 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

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