-
Notifications
You must be signed in to change notification settings - Fork 5
/
ByteBuffer.h
321 lines (275 loc) · 6.81 KB
/
ByteBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// ByteBuffer.h
//
// 2015-1-22.
//
//
#ifndef __ByteBuffer__
#define __ByteBuffer__
// If defined, utility functions within the class are enabled
#define BB_UTILITY
#include <iostream>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <vector>
#include <list>
#include <map>
//字节流缓存,用于打包、序列化各种数据类型
class ByteBuffer
{
public:
static const uint32_t DEFAULT_SIZE = 0x1000;
ByteBuffer(uint32_t size = DEFAULT_SIZE);
ByteBuffer(uint8_t* data, uint32_t size);
~ByteBuffer();
template <typename T>
T get()
{
return read<T>();
}
template <typename T>
void put(T data)
{
append(data);
}
template <typename T>
T read()
{
T data = read<T>(_rpos);
_rpos += sizeof(T);
return data;
}
template <typename T>
T read(uint32_t index) const
{
T data;
if(index + sizeof(T) <= _buf.size())
{
memcpy(&data, (uint8_t*)&_buf[index], sizeof(T));
return data;
}
return 0;
}
template <typename T>
void append(T data)
{
uint32_t s = sizeof(data);
if (size() < (_wpos + s))
_buf.resize(_wpos + s);
memcpy(&_buf[_wpos], (uint8_t*)&data, s);
_wpos += s;
}
template <typename T>
void insert(T data, uint32_t index)
{
if((index + sizeof(data)) > size())
return;
memcpy(&_buf[index], (uint8_t*)&data, sizeof(data));
_wpos = index+sizeof(data);
}
void append(const std::string &str);
void append(const char *data, uint32_t size);
void append(const uint8_t *data, uint32_t size);
void append(const ByteBuffer &buffer);
//将多个数据打包
template <typename... Args>
ByteBuffer& package(Args &...args)
{
return _package(args...);
}
//解包数据包指定类型
template <typename... Args>
ByteBuffer& unpacked(Args &...args)
{
return _unpacked(args...);
}
private:
template <typename V, typename... Args>
ByteBuffer& _package(V &v, Args &...args);
template <typename V, typename... Args>
ByteBuffer& _unpacked(V &v, Args &...args);
//变参模板终止操作
template <typename V>
ByteBuffer& _package(V &v);
template <typename V>
ByteBuffer& _unpacked(V &v);
public:
uint32_t bytesRemaining();
void clear();
bool equals(const ByteBuffer &other);
void resize(uint32_t newSize);
uint32_t size() const;
void setReadPos(uint32_t r)
{
_rpos = r;
}
uint32_t getReadPos()
{
return _rpos;
}
void setWritePos(uint32_t w)
{
_wpos = w;
}
uint32_t getWritePos()
{
return _wpos;
}
//流操作写数据
ByteBuffer & operator<<(bool value);
ByteBuffer & operator<<(uint8_t value);
ByteBuffer & operator<<(uint16_t value);
ByteBuffer & operator<<(uint32_t value);
ByteBuffer & operator<<(uint64_t value);
ByteBuffer & operator<<(int8_t value);
ByteBuffer & operator<<(int16_t value);
ByteBuffer & operator<<(int32_t value);
ByteBuffer & operator<<(int64_t value);
ByteBuffer & operator<<(float value);
ByteBuffer & operator<<(double value);
ByteBuffer & operator<<(const std::string &value);
ByteBuffer & operator<<(const char* str);
//流操作读数据
ByteBuffer & operator>>(bool &value);
ByteBuffer & operator>>(uint8_t &value);
ByteBuffer & operator>>(uint16_t &value);
ByteBuffer & operator>>(uint32_t &value);
ByteBuffer & operator>>(uint64_t &value);
ByteBuffer & operator>>(int8_t &value);
ByteBuffer & operator>>(int16_t &value);
ByteBuffer & operator>>(int32_t &value);
ByteBuffer & operator>>(int64_t &value);
ByteBuffer & operator>>(float &value);
ByteBuffer & operator>>(double &value);
ByteBuffer & operator>>(std::string &value);
ByteBuffer & operator>>(char &value);
//下标操作读数据
uint8_t operator[](uint32_t pos);
std::vector<uint8_t> getBuffer();
private:
uint32_t _rpos = 0;
uint32_t _wpos = 0;
std::vector<uint8_t> _buf;
#ifdef ARM_PLATFORM
//避免arm平台内存地址对齐异常
void* memcpy(void *dst, const void *src, size_t len) const
{
char *_dst = (char*)dst;
const char *_src = (const char*)src;
while(len--) *_dst++ = *_src++;
return dst;
}
#endif
#ifdef BB_UTILITY
std::string name = "";
#endif
public:
#ifdef BB_UTILITY
void setName(std::string n);
std::string getName();
void printInfo();
void printAH();
void printAscii();
void printHex();
void printPosition();
#endif
};
template<typename T>
inline ByteBuffer& operator<<(ByteBuffer & b, std::vector<T> v)
{
b << (uint32_t) v.size();
for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); i++)
{
b << *i;
}
return b;
}
template<typename T>
inline ByteBuffer & operator>>(ByteBuffer & b, std::vector<T> &v)
{
uint32_t vsize;
b >> vsize;
v.clear();
while (vsize--)
{
T t;
b >> t;
v.push_back(t);
}
return b;
}
template<typename T>
inline ByteBuffer & operator<<(ByteBuffer & b, std::list<T> v)
{
b << (uint32_t) v.size();
for (typename std::list<T>::iterator i = v.begin(); i != v.end(); i++)
{
b << *i;
}
return b;
}
template<typename T>
inline ByteBuffer & operator>>(ByteBuffer & b, std::list<T> &v)
{
uint32_t vsize;
b >> vsize;
v.clear();
while (vsize--)
{
T t;
b >> t;
v.push_back(t);
}
return b;
}
template<typename K, typename V>
inline ByteBuffer & operator<<(ByteBuffer & b, std::map<K, V> &m)
{
b << (uint32_t) m.size();
for (typename std::map<K, V>::iterator i = m.begin(); i != m.end(); i++)
{
b << i->first << i->second;
}
return b;
}
template<typename K, typename V>
inline ByteBuffer & operator>>(ByteBuffer & b, std::map<K, V> &m)
{
uint32_t msize;
b >> msize;
m.clear();
while (msize--)
{
K k;
V v;
b >> k >> v;
m.insert(make_pair(k, v));
}
return b;
}
template <typename V, typename... Args>
ByteBuffer& ByteBuffer::_package(V &v, Args &...args)
{
*this << v;
return _package(args...);
}
template <typename V, typename... Args>
ByteBuffer& ByteBuffer::_unpacked(V &v, Args &...args)
{
*this >> v;
return _unpacked(args...);
}
template <typename V>
ByteBuffer& ByteBuffer::_package(V &v)
{
*this << v;
return *this;
}
template <typename V>
ByteBuffer& ByteBuffer::_unpacked(V &v)
{
*this >> v;
return *this;
}
#endif /* defined(__ByteBuffer__) */