TrinityCore
Loading...
Searching...
No Matches
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 TRINITYCORE_MESSAGE_BUFFER_H
19#define TRINITYCORE_MESSAGE_BUFFER_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) = default;
40
41 MessageBuffer(MessageBuffer&& right) noexcept : _wpos(right._wpos), _rpos(right._rpos), _storage(std::move(right).Release()) { }
42
43 ~MessageBuffer() = default;
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
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);
98 WriteCompleted(size);
99 }
100 }
101
102 std::vector<uint8>&& Release() &&
103 {
104 Reset();
105 return std::move(_storage);
106 }
107
108 MessageBuffer& operator=(MessageBuffer const& right) = default;
109
111 {
112 if (this != &right)
113 {
114 _wpos = right._wpos;
115 _rpos = right._rpos;
116 _storage = std::move(right).Release();
117 }
118
119 return *this;
120 }
121
122private:
125 std::vector<uint8> _storage;
126};
127
128#endif /* __MESSAGEBUFFER_H_ */
uint8_t uint8
Definition Define.h:156
std::vector< uint8 > && Release() &&
size_type _rpos
void Resize(size_type bytes)
size_type GetRemainingSpace() const
size_type _wpos
MessageBuffer(MessageBuffer &&right) noexcept
void ReadCompleted(size_type bytes)
void WriteCompleted(size_type bytes)
MessageBuffer(std::size_t initialSize)
MessageBuffer & operator=(MessageBuffer &&right) noexcept
uint8 * GetReadPointer()
std::vector< uint8 > _storage
size_type GetActiveSize() const
uint8 * GetWritePointer()
~MessageBuffer()=default
MessageBuffer(MessageBuffer const &right)=default
void Write(void const *data, std::size_t size)
MessageBuffer & operator=(MessageBuffer const &right)=default
size_type GetBufferSize() const
void EnsureFreeSpace()
std::vector< uint8 >::size_type size_type
uint8 * GetBasePointer()