TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MessageBuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __MESSAGEBUFFER_H_
19 #define __MESSAGEBUFFER_H_
20 
21 #include "Define.h"
22 #include <vector>
23 
25 {
26  typedef std::vector<uint8>::size_type size_type;
27 
28 public:
30  {
31  _storage.resize(4096);
32  }
33 
34  explicit MessageBuffer(std::size_t initialSize) : _wpos(0), _rpos(0), _storage()
35  {
36  _storage.resize(initialSize);
37  }
38 
39  MessageBuffer(MessageBuffer const& right) : _wpos(right._wpos), _rpos(right._rpos), _storage(right._storage)
40  {
41  }
42 
43  MessageBuffer(MessageBuffer&& right) : _wpos(right._wpos), _rpos(right._rpos), _storage(right.Move()) { }
44 
45  void Reset()
46  {
47  _wpos = 0;
48  _rpos = 0;
49  }
50 
51  void Resize(size_type bytes)
52  {
53  _storage.resize(bytes);
54  }
55 
56  uint8* GetBasePointer() { return _storage.data(); }
57 
59 
61 
62  void ReadCompleted(size_type bytes) { _rpos += bytes; }
63 
64  void WriteCompleted(size_type bytes) { _wpos += bytes; }
65 
66  size_type GetActiveSize() const { return _wpos - _rpos; }
67 
68  size_type GetRemainingSpace() const { return _storage.size() - _wpos; }
69 
70  size_type GetBufferSize() const { return _storage.size(); }
71 
72  // Discards inactive data
73  void Normalize()
74  {
75  if (_rpos)
76  {
77  if (_rpos != _wpos)
79  _wpos -= _rpos;
80  _rpos = 0;
81  }
82  }
83 
84  // Ensures there's "some" free space, make sure to call Normalize() before this
86  {
87  // resize buffer if it's already full
88  if (GetRemainingSpace() == 0)
89  _storage.resize(_storage.size() * 3 / 2);
90  }
91 
92  void Write(void const* data, std::size_t size)
93  {
94  if (size)
95  {
96  memcpy(GetWritePointer(), data, size);
97  WriteCompleted(size);
98  }
99  }
100 
101  std::vector<uint8>&& Move()
102  {
103  _wpos = 0;
104  _rpos = 0;
105  return std::move(_storage);
106  }
107 
109  {
110  if (this != &right)
111  {
112  _wpos = right._wpos;
113  _rpos = right._rpos;
114  _storage = right._storage;
115  }
116 
117  return *this;
118  }
119 
121  {
122  if (this != &right)
123  {
124  _wpos = right._wpos;
125  _rpos = right._rpos;
126  _storage = right.Move();
127  }
128 
129  return *this;
130  }
131 
132 private:
133  size_type _wpos;
134  size_type _rpos;
135  std::vector<uint8> _storage;
136 };
137 
138 #endif /* __MESSAGEBUFFER_H_ */
MessageBuffer(MessageBuffer const &right)
Definition: MessageBuffer.h:39
std::vector< uint8 > && Move()
Definition: MessageBuffer.h:101
size_type GetRemainingSpace() const
Definition: MessageBuffer.h:68
MessageBuffer & operator=(MessageBuffer const &right)
Definition: MessageBuffer.h:108
MessageBuffer()
Definition: MessageBuffer.h:29
std::vector< uint8 >::size_type size_type
Definition: MessageBuffer.h:26
MessageBuffer & operator=(MessageBuffer &&right)
Definition: MessageBuffer.h:120
uint8 * GetBasePointer()
Definition: MessageBuffer.h:56
uint8 * GetWritePointer()
Definition: MessageBuffer.h:60
std::vector< uint8 > _storage
Definition: MessageBuffer.h:135
void WriteCompleted(size_type bytes)
Definition: MessageBuffer.h:64
void EnsureFreeSpace()
Definition: MessageBuffer.h:85
void ReadCompleted(size_type bytes)
Definition: MessageBuffer.h:62
size_type _wpos
Definition: MessageBuffer.h:133
void Write(void const *data, std::size_t size)
Definition: MessageBuffer.h:92
size_type GetBufferSize() const
Definition: MessageBuffer.h:70
size_type GetActiveSize() const
Definition: MessageBuffer.h:66
void Resize(size_type bytes)
Definition: MessageBuffer.h:51
void Reset()
Definition: MessageBuffer.h:45
MessageBuffer(std::size_t initialSize)
Definition: MessageBuffer.h:34
MessageBuffer(MessageBuffer &&right)
Definition: MessageBuffer.h:43
uint8_t uint8
Definition: Define.h:152
size_type _rpos
Definition: MessageBuffer.h:134
void Normalize()
Definition: MessageBuffer.h:73
uint8 * GetReadPointer()
Definition: MessageBuffer.h:58
Definition: MessageBuffer.h:24