TrinityCore
MessageBuffer.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
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#include <cstring>
24
26{
27 typedef std::vector<uint8>::size_type size_type;
28
29public:
31 {
32 _storage.resize(4096);
33 }
34
35 explicit MessageBuffer(std::size_t initialSize) : _wpos(0), _rpos(0), _storage()
36 {
37 _storage.resize(initialSize);
38 }
39
40 MessageBuffer(MessageBuffer const& right) : _wpos(right._wpos), _rpos(right._rpos), _storage(right._storage)
41 {
42 }
43
44 MessageBuffer(MessageBuffer&& right) noexcept : _wpos(right._wpos), _rpos(right._rpos), _storage(right.Move()) { }
45
46 void Reset()
47 {
48 _wpos = 0;
49 _rpos = 0;
50 }
51
52 void Resize(size_type bytes)
53 {
54 _storage.resize(bytes);
55 }
56
57 uint8* GetBasePointer() { return _storage.data(); }
58
60
62
63 void ReadCompleted(size_type bytes) { _rpos += bytes; }
64
65 void WriteCompleted(size_type bytes) { _wpos += bytes; }
66
67 size_type GetActiveSize() const { return _wpos - _rpos; }
68
69 size_type GetRemainingSpace() const { return _storage.size() - _wpos; }
70
71 size_type GetBufferSize() const { return _storage.size(); }
72
73 // Discards inactive data
74 void Normalize()
75 {
76 if (_rpos)
77 {
78 if (_rpos != _wpos)
80 _wpos -= _rpos;
81 _rpos = 0;
82 }
83 }
84
85 // Ensures there's "some" free space, make sure to call Normalize() before this
87 {
88 // resize buffer if it's already full
89 if (GetRemainingSpace() == 0)
90 _storage.resize(_storage.size() * 3 / 2);
91 }
92
93 void Write(void const* data, std::size_t size)
94 {
95 if (size)
96 {
97 memcpy(GetWritePointer(), data, size);
99 }
100 }
101
102 std::vector<uint8>&& Move()
103 {
104 _wpos = 0;
105 _rpos = 0;
106 return std::move(_storage);
107 }
108
110 {
111 if (this != &right)
112 {
113 _wpos = right._wpos;
114 _rpos = right._rpos;
115 _storage = right._storage;
116 }
117
118 return *this;
119 }
120
122 {
123 if (this != &right)
124 {
125 _wpos = right._wpos;
126 _rpos = right._rpos;
127 _storage = right.Move();
128 }
129
130 return *this;
131 }
132
133private:
136 std::vector<uint8> _storage;
137};
138
139#endif /* __MESSAGEBUFFER_H_ */
uint8_t uint8
Definition: Define.h:144
size_type _rpos
MessageBuffer & operator=(MessageBuffer const &right)
void Resize(size_type bytes)
Definition: MessageBuffer.h:52
size_type GetRemainingSpace() const
Definition: MessageBuffer.h:69
size_type _wpos
MessageBuffer(MessageBuffer &&right) noexcept
Definition: MessageBuffer.h:44
void ReadCompleted(size_type bytes)
Definition: MessageBuffer.h:63
void WriteCompleted(size_type bytes)
Definition: MessageBuffer.h:65
MessageBuffer(std::size_t initialSize)
Definition: MessageBuffer.h:35
MessageBuffer & operator=(MessageBuffer &&right) noexcept
uint8 * GetReadPointer()
Definition: MessageBuffer.h:59
std::vector< uint8 > _storage
size_type GetActiveSize() const
Definition: MessageBuffer.h:67
uint8 * GetWritePointer()
Definition: MessageBuffer.h:61
MessageBuffer(MessageBuffer const &right)
Definition: MessageBuffer.h:40
void Write(void const *data, std::size_t size)
Definition: MessageBuffer.h:93
std::vector< uint8 > && Move()
size_type GetBufferSize() const
Definition: MessageBuffer.h:71
void EnsureFreeSpace()
Definition: MessageBuffer.h:86
void Normalize()
Definition: MessageBuffer.h:74
std::vector< uint8 >::size_type size_type
Definition: MessageBuffer.h:27
uint8 * GetBasePointer()
Definition: MessageBuffer.h:57
constexpr std::size_t size()
Definition: UpdateField.h:796