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(initialSize)
36 {
37 }
38
39 MessageBuffer(MessageBuffer const& right) : _wpos(right._wpos), _rpos(right._rpos), _storage(right._storage)
40 {
41 }
42
43 MessageBuffer(MessageBuffer&& right) noexcept : _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);
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
132private:
135 std::vector<uint8> _storage;
136};
137
138#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:51
size_type GetRemainingSpace() const
Definition: MessageBuffer.h:68
size_type _wpos
MessageBuffer(MessageBuffer &&right) noexcept
Definition: MessageBuffer.h:43
void ReadCompleted(size_type bytes)
Definition: MessageBuffer.h:62
void WriteCompleted(size_type bytes)
Definition: MessageBuffer.h:64
MessageBuffer(std::size_t initialSize)
Definition: MessageBuffer.h:35
MessageBuffer & operator=(MessageBuffer &&right) noexcept
uint8 * GetReadPointer()
Definition: MessageBuffer.h:58
std::vector< uint8 > _storage
size_type GetActiveSize() const
Definition: MessageBuffer.h:66
uint8 * GetWritePointer()
Definition: MessageBuffer.h:60
MessageBuffer(MessageBuffer const &right)
Definition: MessageBuffer.h:39
void Write(void const *data, std::size_t size)
Definition: MessageBuffer.h:92
std::vector< uint8 > && Move()
size_type GetBufferSize() const
Definition: MessageBuffer.h:70
void EnsureFreeSpace()
Definition: MessageBuffer.h:85
void Normalize()
Definition: MessageBuffer.h:73
std::vector< uint8 >::size_type size_type
Definition: MessageBuffer.h:27
uint8 * GetBasePointer()
Definition: MessageBuffer.h:56
constexpr std::size_t size()
Definition: UpdateField.h:769