-
Notifications
You must be signed in to change notification settings - Fork 6
/
marshal.h
397 lines (308 loc) · 8.82 KB
/
marshal.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#pragma once
#include <list>
#include <vector>
#include <string>
#include <map>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include "utils.h"
namespace rpc {
class Marshal;
/**
* Not thread safe, for better performance.
*/
class Chunk: public NoCopy {
friend class Marshal;
char* data_;
int size_;
// Content range [read_idx_, write_idx_)
int read_idx_;
int write_idx_;
static const int min_size;
public:
Chunk(int size = Chunk::min_size)
: read_idx_(0), write_idx_(0) {
size_ = std::max(Chunk::min_size, size);
data_ = new char[size_];
}
/**
* Construct by copying a buffer.
*/
Chunk(const void* p, int n)
: read_idx_(0), write_idx_(n) {
size_ = std::max(Chunk::min_size, n);
data_ = new char[size_];
memcpy(data_, p, n);
}
~Chunk() {
delete[] data_;
}
const char* content_ptr() const {
return &data_[read_idx_];
}
int content_size() const {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
return write_idx_ - read_idx_;
}
char* set_bookmark() {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
char* p = &data_[write_idx_];
write_idx_++;
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
return p;
}
int write(const void* p, int n) {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
int n_write = std::min(n, size_ - write_idx_);
memcpy(data_ + write_idx_, p, n_write);
write_idx_ += n_write;
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
return n_write;
}
int read(void* p, int n) {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
int n_read = std::min(n, write_idx_ - read_idx_);
memcpy(p, data_ + read_idx_, n_read);
read_idx_ += n_read;
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
return n_read;
}
int peek(void* p, int n) const {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
int n_peek = std::min(n, write_idx_ - read_idx_);
memcpy(p, data_ + read_idx_, n_peek);
return n_peek;
}
int write_to_fd(int fd) {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
int r = ::write(fd, data_ + read_idx_, write_idx_ - read_idx_);
if (r > 0) {
read_idx_ += r;
}
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
return r;
}
int read_from_fd(int fd) {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
int r = ::read(fd, data_ + write_idx_, size_ - write_idx_);
if (r > 0) {
write_idx_ += r;
}
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
return r;
}
int read_from_chunk(Chunk& chnk, int n) {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
assert(chnk.write_idx_ <= chnk.size_);
assert(chnk.read_idx_ <= chnk.write_idx_);
int writable_size = size_ - write_idx_;
int readable_size = chnk.write_idx_ - chnk.read_idx_;
int n_read = std::min(std::min(writable_size, readable_size), n);
memcpy(data_ + write_idx_, chnk.data_ + chnk.read_idx_, n_read);
write_idx_ += n_read;
chnk.read_idx_ += n_read;
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
assert(chnk.write_idx_ <= chnk.size_);
assert(chnk.read_idx_ <= chnk.write_idx_);
return n_read;
}
/**
* Check if it is not possible to write to the chunk anymore.
*/
bool fully_written() const {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
return write_idx_ == size_;
}
/**
* Check if it is not possible to read from the chunk anymore, even if
* we retry later.
*/
bool fully_read() const {
assert(write_idx_ <= size_);
assert(read_idx_ <= write_idx_);
return read_idx_ == size_;
}
};
/**
* Not thread safe, for better performance.
*/
class Marshal: public NoCopy {
std::list<Chunk*> chunk_;
i32 write_counter_;
public:
class Bookmark: public NoCopy {
friend class Marshal;
int size_;
char** ptr_;
public:
Bookmark()
: size_(-1), ptr_(NULL) {
}
~Bookmark() {
delete[] ptr_;
}
};
Marshal()
: write_counter_(0) {
}
Marshal(const std::string& data)
: write_counter_(0) {
chunk_.push_back(new Chunk(&data[0], data.length()));
}
~Marshal();
/**
* Note: Need to delete the bookmark manually.
*/
Bookmark* set_bookmark(int size);
void write_bookmark(Bookmark*, const void*);
i32 get_write_counter_and_reset() {
i32 value = write_counter_;
write_counter_ = 0;
return value;
}
int write(const void* p, int n);
int read(void* p, int n);
int peek(void* p, int n) const;
int read_from_marshal(Marshal&, int n);
int read_from_fd(int fd);
int write_to_fd(int fd);
std::string dump() const;
/**
* Faster way to check if content_size > size.
*/
bool content_size_gt(int size) const;
int content_size() const;
bool empty() const {
return !content_size_gt(0);
}
void write_i32(const rpc::i32& v) {
verify(write(&v, sizeof(v)) == sizeof(v));
}
void write_string(const std::string& v) {
rpc::i32 len = (rpc::i32) v.length();
write_i32(len);
if (len > 0) {
verify(write(v.c_str(), len) == len);
}
}
};
} // namespace rpc
// define marshaling operators in default namespace, so we can use them without using namespace rpc
inline rpc::Marshal& operator <<(rpc::Marshal& m, const rpc::i32& v) {
m.write_i32(v);
return m;
}
inline rpc::Marshal& operator <<(rpc::Marshal& m, const rpc::i64& v) {
verify(m.write(&v, sizeof(v)) == sizeof(v));
return m;
}
inline rpc::Marshal& operator <<(rpc::Marshal& m, const double& v) {
verify(m.write(&v, sizeof(v)) == sizeof(v));
return m;
}
inline rpc::Marshal& operator <<(rpc::Marshal& m, const std::string& v) {
m.write_string(v);
return m;
}
template<class T>
inline rpc::Marshal& operator <<(rpc::Marshal& m, const std::vector<T>& v) {
rpc::i32 len = (rpc::i32) v.size();
m << len;
for (typename std::vector<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
m << *it;
}
return m;
}
template<class T>
inline rpc::Marshal& operator <<(rpc::Marshal& m, const std::list<T>& v) {
rpc::i32 len = (rpc::i32) v.size();
m << len;
for (typename std::list<T>::const_iterator it = v.begin(); it != v.end(); ++it) {
m << *it;
}
return m;
}
template<class K, class V>
inline rpc::Marshal& operator <<(rpc::Marshal& m, const std::map<K, V>& v) {
rpc::i32 len = (rpc::i32) v.size();
m << len;
for (typename std::map<K, V>::const_iterator it = v.begin(); it != v.end(); ++it) {
m << it->first << it->second;
}
return m;
}
inline rpc::Marshal& operator >>(rpc::Marshal& m, rpc::i32& v) {
verify(m.read(&v, sizeof(v)) == sizeof(v));
return m;
}
inline rpc::Marshal& operator >>(rpc::Marshal& m, rpc::i64& v) {
verify(m.read(&v, sizeof(v)) == sizeof(v));
return m;
}
inline rpc::Marshal& operator >>(rpc::Marshal& m, double& v) {
verify(m.read(&v, sizeof(v)) == sizeof(v));
return m;
}
inline rpc::Marshal& operator >>(rpc::Marshal& m, std::string& v) {
rpc::i32 len;
m >> len;
v.resize(len);
if (len > 0) {
verify(m.read(&v[0], len) == len);
}
return m;
}
template<class T>
inline rpc::Marshal& operator >>(rpc::Marshal& m, std::vector<T>& v) {
rpc::i32 len;
verify(m.read(&len, sizeof(len)) == sizeof(len));
v.clear();
v.reserve(len);
for (rpc::i32 i = 0; i < len; i++) {
T elem;
m >> elem;
v.push_back(elem);
}
return m;
}
template<class T>
inline rpc::Marshal& operator >>(rpc::Marshal& m, std::list<T>& v) {
rpc::i32 len;
verify(m.read(&len, sizeof(len)) == sizeof(len));
v.clear();
for (rpc::i32 i = 0; i < len; i++) {
T elem;
m >> elem;
v.push_back(elem);
}
return m;
}
template<class K, class V>
inline rpc::Marshal& operator >>(rpc::Marshal& m, std::map<K, V>& v) {
rpc::i32 len;
verify(m.read(&len, sizeof(len)) == sizeof(len));
v.clear();
for (rpc::i32 i = 0; i < len; i++) {
K key;
V value;
m >> key >> value;
v[key] = value;
}
return m;
}