-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreamFormatter.h
323 lines (278 loc) · 8.77 KB
/
StreamFormatter.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
322
323
/*
* Copyright (c) 2016 Michal Srb <michalsrb@gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef STREAMFORMATER_H
#define STREAMFORMATER_H
#include <type_traits>
#include <string.h>
#include <arpa/inet.h>
#include "Stream.h"
/**
* @brief a class that takes care of reading/writing structures to/from Stream.
*
* It contains several template methods to automatically handle reading and writing of various data types.
*
* Methods types:
* * recv/send/forward: The size is taken from given type. Endianity is appropriatelly converted before/after writing/reading. Class types need to have public ntoh and hton methods that switch the endianity of the members of the object in place.
* * recv_raw/send_raw/forward_raw: The size is taken from given type. Endianity is not handled.
* * forward_direct: Forward given amount of bytes without touching them.
*
* @remark This class is not thread-safe and requires external synchronization if shared between threads.
*/
class StreamFormatter
{
private:
template<class T, typename std::enable_if<std::is_class<T>::value>::type *dummy = nullptr>
static void hton(T &t) {
t.hton();
}
template<class T, typename std::enable_if<std::is_enum<T>::value>::type *dummy = nullptr>
static void hton(T &t) {
auto ti = static_cast<typename std::underlying_type<T>::type>(t);
hton(ti);
t = static_cast<T>(ti);
}
static void hton(uint8_t &t) {}
static void hton(int8_t &t) {}
static void hton(char &t) {}
static void hton(uint16_t &t) {
t = htons(t);
}
static void hton(int16_t &t) {
t = htons(t);
}
static void hton(uint32_t &t) {
t = htonl(t);
}
static void hton(int32_t &t) {
t = htonl(t);
}
template<class T, typename std::enable_if<std::is_class<T>::value>::type *dummy = nullptr>
static void ntoh(T &t) {
t.ntoh();
}
template<class T, typename std::enable_if<std::is_enum<T>::value>::type *dummy = nullptr>
static void ntoh(T &t) {
auto ti = static_cast<typename std::underlying_type<T>::type>(t);
ntoh(ti);
t = static_cast<T>(ti);
}
static void ntoh(uint8_t &t) {}
static void ntoh(int8_t &t) {}
static void ntoh(char &t) {}
static void ntoh(uint16_t &t) {
t = ntohs(t);
}
static void ntoh(int16_t &t) {
t = ntohs(t);
}
static void ntoh(uint32_t &t) {
t = ntohl(t);
}
static void ntoh(int32_t &t) {
t = ntohl(t);
}
public:
StreamFormatter(Stream *stream);
Stream *stream() const {
return m_stream;
}
/**
* Send raw data to the stream as they are.
*/
void send_raw(const void *buf, std::size_t len);
/**
* Send referenced variable to the stream as-is.
* Writes sizeof(T) bytes.
*/
template<typename T>
void send_raw(const T &t) {
send_raw(&t, sizeof(T));
}
/**
* Send referenced array to the stream as-is.
* Writes sizeof(T)*array_size bytes.
*/
template<typename T, std::size_t length>
void send_raw(const T(& array) [length]) {
send_raw(array, length * sizeof(T));
}
/**
* Send content of referenced vector as-is.
* Writes sizeof(T)*vector.size() bytes.
*/
template<typename T, typename A>
void send_raw(std::vector<T, A> const &vector) {
send_raw(vector.data(), vector.size() * sizeof(T));
}
/**
* Send given std::string as-is.
* Writes text.length() bytes.
*/
void send(const std::string &text) {
send_raw(text.data(), text.length());
}
/**
* Send given variable in network endianity.
* Writes sizeof(T) bytes.
*/
template<class T>
void send(T t) {
StreamFormatter::hton(t);
send_raw(t);
}
/**
* Send content of referenced vector in network endianity.
* Writes sizeof(T)*vector.size() bytes.
*/
template<typename T, typename A>
void send(std::vector<T, A> vector) {
for(auto &t : vector) {
StreamFormatter::hton(t);
}
send_raw(vector);
}
/**
* Receives given amount of data without converting.
*/
void recv_raw(void *buf, std::size_t len);
/**
* Receives into referenced variable without converting.
* Reads sizeof(T) bytes.
*/
template<typename T>
void recv_raw(T &t) {
recv_raw(&t, sizeof(T));
}
/**
* Receives into referenced array without converting.
* Reads sizeof(T)*array_size bytes.
*/
template<typename T, std::size_t length>
void recv_raw(T(& array) [length]) {
recv_raw(array, length * sizeof(T));
}
/**
* Receives into referenced vector without converting.
* Reads sizeof(T)*vector.size() bytes.
*/
template<typename T, typename A>
void recv_raw(std::vector<T, A> &vector) {
recv_raw(vector.data(), vector.size() * sizeof(T));
}
/**
* Receives into referenced variable converting to host endianity.
* Reads sizeof(T) bytes.
*/
template<class T>
void recv(T &t) {
recv_raw(t);
StreamFormatter::ntoh(t);
}
/**
* Receives into referenced array converting to host endianity.
* Reads sizeof(T)*array_size bytes.
*/
template<typename T, std::size_t length>
void recv(T(& array) [length]) {
recv_raw(array);
for(auto &t : array) {
StreamFormatter::ntoh(t);
}
}
/**
* Receives into referenced vector converting to host endianity.
* Reads sizeof(T)*vector.size() bytes.
*/
template<typename T, typename A>
void recv(std::vector<T, A> &vector) {
recv_raw(vector);
for(auto &t : vector) {
StreamFormatter::ntoh(t);
}
}
/**
* Receives given amount of bytes and returns them as std::string.
*/
std::string recv_string(std::size_t length);
/**
* Shortcut for recv_raw & send_raw.
*/
void forward_raw(Stream &output, void *buf, std::size_t len);
/**
* Forwards given amount of data directly to given stream without explicit buffer.
*/
void forward_directly(Stream &output, std::size_t len);
/**
* Shortcut for recv_raw & send_raw.
*/
template<typename T>
void forward_raw(Stream &output, T &t) {
forward_raw(output, &t, sizeof(t));
}
/**
* Shortcut for recv_raw & send_raw.
*/
template<typename T, typename A>
void forward_raw(Stream &output, std::vector<T,A> &vector) {
forward_raw(output, vector.data(), vector.size() * sizeof(T));
}
/**
* Effective shortcut for recv & send.
*/
template<typename T>
void forward(Stream &output, T &t) {
forward_raw(output, t);
StreamFormatter::ntoh(t);
}
/**
* Effective shortcut for recv & send.
*/
template<typename T, typename A>
void forward(Stream &output, std::vector<T,A> &vector) {
forward_raw(output, vector);
for(auto &t : vector) {
StreamFormatter::ntoh(t);
}
}
/**
* Push referenced variable back into receiving buffer.
* Next recv or forward will retrieve it first.
*/
template<typename T>
void push_back(const T &t) {
if(bufferSize + sizeof(t) > bufferMaxSize) {
throw std::runtime_error("Stream: Push back buffer overflow.");
}
memcpy(&buffer[bufferSize], &t, sizeof(t));
bufferSize += sizeof(t);
}
private:
Stream *m_stream;
static constexpr std::size_t bufferMaxSize = 1; // TODO: Single byte because that's all we currently we need to push back. But it could change in future.
uint8_t buffer[bufferMaxSize];
std::size_t bufferSize = 0;
};
#endif // STREAMFORMATER_H