-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmp_writer.h
299 lines (266 loc) · 8.74 KB
/
mp_writer.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
#ifndef MP_WRITER_H
#define MP_WRITER_H
#include <string_view>
#include <stdexcept>
#include <optional>
#include <array>
#include <map>
#include <cmath>
#include "msgpuck/msgpuck.h"
#include "wtf_buffer.h"
namespace tnt {
class connection;
enum class request_type : uint8_t;
}
/** msgpuck wrapper.
*
* A caller must ensure there is enough free space in the buffer.
*/
class mp_writer
{
public:
/// hack to serialize small unsigned integers into bigger messagepack values
template<typename T>
struct strict_uint
{
T value;
strict_uint(const T &val) : value(val) {}
};
/// Wrap writer object around specified buffer.
mp_writer(wtf_buffer &buf);
/// Put array header with <max_size> cardinality and move current position over it.
/// A caller must call finalize() to close the array and actualize its initial size.
void begin_array(uint32_t max_cardinality);
/// Put map header with <max_size> cardinality and move current position over it.
/// A caller must call finalize() to close the map and actualize its initial size.
void begin_map(uint32_t max_cardinality);
/// Replace initial cardinality settled with begin_array(), begin_map() with actual
/// value (counted untill now).
void finalize();
/// Finalize all non-finalized containers (if exists).
void finalize_all();
/// Function to be used in custom serializers (e.g. operator << overload).
void increment_container_counter(size_t items_added = 1);
/// Append msgpack buffer.
void write(const char *begin, const char *end, size_t cardinality = 0);
/// Append msgpack buffer.
inline void write(const wtf_buffer &data, size_t cardinality = 0)
{
write(data.data(), data.end, cardinality);
}
inline operator const wtf_buffer&() const noexcept { return _buf; }
inline wtf_buffer& buf() const noexcept { return _buf; }
mp_writer& operator<< (std::nullptr_t);
mp_writer& operator<< (const std::string_view &val);
inline mp_writer& operator<< (const wtf_buffer &data)
{
write(data);
return *this;
}
template <typename T>
mp_writer& operator<< (const std::optional<T> &val) noexcept
{
if (!val.has_value())
{
_buf.end = mp_encode_nil(_buf.end);
if (!_opened_containers.empty())
++_opened_containers.top().items_count;
return *this;
}
*this << val.value();
return *this;
}
template <typename T, typename = std::enable_if_t<
(std::is_integral_v<T> && sizeof(T) < 16) ||
std::is_floating_point_v<T>
>>
mp_writer& operator<< (const T &val)
{
if constexpr (std::is_same_v<T, bool>)
{
_buf.end = mp_encode_bool(_buf.end, val);
}
else if constexpr (std::is_floating_point_v<T>)
{
if constexpr (sizeof(T) <= 4)
_buf.end = mp_encode_float(_buf.end, val);
else if (!std::isfinite(val) || (val <= std::numeric_limits<double>::max() && val >= std::numeric_limits<double>::min()))
_buf.end = mp_encode_double(_buf.end, static_cast<double>(val));
else
throw std::overflow_error("unable to fit floating point value into msgpack");
}
else
{
_buf.end = (val >= 0 ?
mp_encode_uint(_buf.end, static_cast<uint64_t>(val)) :
mp_encode_int(_buf.end, val));
}
if (!_opened_containers.empty())
++_opened_containers.top().items_count;
return *this;
}
template <typename T>
mp_writer& operator<< (const strict_uint<T> &val)
{
if constexpr (sizeof(T) == 2)
{
_buf.end = mp_store_u8(_buf.end, 0xcd);
_buf.end = mp_store_u16(_buf.end, uint16_t(val.value));
}
else if constexpr (sizeof(T) == 4)
{
_buf.end = mp_store_u8(_buf.end, 0xce);
_buf.end = mp_store_u32(_buf.end, uint32_t(val.value));
}
else if constexpr (sizeof(T) == 8)
{
_buf.end = mp_store_u8(_buf.end, 0xcf);
_buf.end = mp_store_u64(_buf.end, uint64_t(val.value));
}
else
static_assert(!sizeof(T), "unsupported size");
if (!_opened_containers.empty())
++_opened_containers.top().items_count;
return *this;
}
template<typename... Args>
mp_writer& operator<< (const std::tuple<Args...> &val)
{
begin_array(std::tuple_size_v<std::tuple<Args...>>);
std::apply(
[this](const auto&... item)
{
((*this << item), ...);
},
val
);
finalize();
return *this;
}
template <typename T>
mp_writer& operator<< (const std::vector<T> &val)
{
begin_array(val.size());
for (const auto& elem: val)
*this << elem;
finalize();
return *this;
}
template <typename KeyT, typename ValueT>
mp_writer& operator<< (const std::map<KeyT, ValueT> &val)
{
begin_map(val.size());
for (const auto& elem: val)
{
*this << elem.first;
*this << elem.second;
}
finalize();
return *this;
}
protected:
template <typename T, std::size_t N = 16>
class wtf_stack
{
private:
std::array<T, N> _items;
size_t _size = 0;
public:
wtf_stack() = default;
void push(T &&value)
{
_items[_size++] = std::move(value);
}
T& pop() noexcept // undefined if empty
{
if (!_size)
return _items[_size];
return _items[--_size];
}
T& top() noexcept // undefined if empty
{
return _items[_size ? _size - 1 : _size];
}
size_t size() const noexcept
{
return _size;
}
bool empty() const noexcept
{
return _size == 0;
}
};
struct container_meta
{
size_t head_offset; // we'll detect container type by the first byte
uint32_t max_cardinality;
uint32_t items_count = 0; // map will have 2x items
};
wtf_stack<container_meta> _opened_containers;
wtf_buffer &_buf;
};
/** Helper to compose iproto messages.
*
* A caller must ensure there is enough free space in the underlying buffer.
*/
class iproto_writer : public mp_writer
{
public:
/// Wrap writer object around specified buffer.
iproto_writer(wtf_buffer &buf);
/// Initiate request or response. A caller must compose header and body afterwards and call finalize()
/// to finalize the message.
void start_message();
/// Finalize topmost container or finalize a message (by fixing its final size).
void finalize();
/// Finalize all non-finalized containers, call, eval, etc (if exists).
void finalize_all();
using mp_writer::operator<<;
};
/** Helper to compose requests.
*
* A caller must ensure there is enough free space in the underlying buffer.
*/
class iproto_client : public iproto_writer
{
public:
/// Wrap writer object around cn.output_buffer().
iproto_client(tnt::connection &cn);
iproto_client(tnt::connection &cn, wtf_buffer &buf);
/// Initiate request of specified type. A caller must compose a body afterwards and call finalize()
/// to finalize request.
void encode_header(tnt::request_type req_type);
/// Put authentication request into the underlying buffer. User and password
/// from connection string are used.
void encode_auth_request();
/// Put authentication request into the underlying buffer.
void encode_auth_request(std::string_view user, std::string_view password);
/// Put ping request into the underlying buffer.
void encode_ping_request();
/// Initiate call request. A caller must pass an array of arguments afterwards
/// and call finalize() to finalize request.
void begin_call(std::string_view fn_name);
void begin_eval(std::string_view script);
/// Call request all-in-one wrapper.
template <typename ...Ts>
void call(std::string_view fn_name, Ts const&... args)
{
begin_call(fn_name);
begin_array(sizeof...(args));
((*this << args), ...);
finalize_all();
}
/// Call request all-in-one wrapper.
template <typename ...Ts>
void eval(std::string_view script, Ts const&... args)
{
begin_eval(script);
begin_array(sizeof...(args));
((*this << args), ...);
finalize_all();
}
using mp_writer::operator<<;
private:
tnt::connection &_cn; ///< request id, initial user name and password source
};
#endif // MP_WRITER_H