-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbuttonrpc.hpp
More file actions
489 lines (410 loc) · 13.7 KB
/
Copy pathbuttonrpc.hpp
File metadata and controls
489 lines (410 loc) · 13.7 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
*
* buttonrpc library
* Copyright 2018-04-28 Button
*
*/
#pragma once
#include <string>
#include <map>
#include <string>
#include <sstream>
#include <functional>
#include <zmq.hpp>
#include "Serializer.hpp"
template<typename T>
struct type_xx{ typedef T type; };
template<>
struct type_xx<void>{ typedef int8_t type; };
class buttonrpc
{
public:
enum rpc_role{
RPC_CLIENT,
RPC_SERVER
};
enum rpc_err_code {
RPC_ERR_SUCCESS = 0,
RPC_ERR_FUNCTIION_NOT_BIND,
RPC_ERR_RECV_TIMEOUT
};
// return value
template<typename T>
class value_t {
public:
typedef typename type_xx<T>::type type;
typedef std::string msg_type;
typedef uint16_t code_type;
value_t() { code_ = 0; msg_.clear(); }
bool valid() { return (code_ == 0 ? true : false); }
int error_code() { return code_; }
std::string error_msg() { return msg_; }
type val() { return val_; }
void set_val(const type& val) { val_ = val; }
void set_code(code_type code) { code_ = code; }
void set_msg(msg_type msg) { msg_ = msg; }
friend Serializer& operator >> (Serializer& in, value_t<T>& d) {
in >> d.code_ >> d.msg_;
if (d.code_ == 0) {
in >> d.val_;
}
return in;
}
friend Serializer& operator << (Serializer& out, value_t<T> d) {
out << d.code_ << d.msg_ << d.val_;
return out;
}
private:
code_type code_;
msg_type msg_;
type val_;
};
buttonrpc();
~buttonrpc();
// network
void as_client(std::string ip, int port);
void as_server(int port);
void send(zmq::message_t& data);
void recv(zmq::message_t& data);
void set_timeout(uint32_t ms);
void run();
public:
// server
template<typename F>
void bind(std::string name, F func);
template<typename F, typename S>
void bind(std::string name, F func, S* s);
// client
template<typename R>
value_t<R> call(std::string name);
template<typename R, typename P1>
value_t<R> call(std::string name, P1);
template<typename R, typename P1, typename P2>
value_t<R> call(std::string name, P1, P2);
template<typename R, typename P1, typename P2, typename P3>
value_t<R> call(std::string name, P1, P2, P3);
template<typename R, typename P1, typename P2, typename P3, typename P4>
value_t<R> call(std::string name, P1, P2, P3, P4);
template<typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
value_t<R> call(std::string name, P1, P2, P3, P4, P5);
private:
Serializer* call_(std::string name, const char* data, int len);
template<typename R>
value_t<R> net_call(Serializer& ds);
template<typename F>
void callproxy(F fun, Serializer* pr, const char* data, int len);
template<typename F, typename S>
void callproxy(F fun, S* s, Serializer* pr, const char* data, int len);
// PROXY FUNCTION POINT
template<typename R>
void callproxy_(R(*func)(), Serializer* pr, const char* data, int len) {
callproxy_(std::function<R()>(func), pr, data, len);
}
template<typename R, typename P1>
void callproxy_(R(*func)(P1), Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1)>(func), pr, data, len);
}
template<typename R, typename P1, typename P2>
void callproxy_(R(*func)(P1, P2), Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1, P2)>(func), pr, data, len);
}
template<typename R, typename P1, typename P2, typename P3>
void callproxy_(R(*func)(P1, P2, P3), Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1, P2, P3)>(func), pr, data, len);
}
template<typename R, typename P1, typename P2, typename P3, typename P4>
void callproxy_(R(*func)(P1, P2, P3, P4), Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1, P2, P3, P4)>(func), pr, data, len);
}
template<typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
void callproxy_(R(*func)(P1, P2, P3, P4, P5), Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1, P2, P3, P4, P5)>(func), pr, data, len);
}
// PROXY CLASS MEMBER
template<typename R, typename C, typename S>
void callproxy_(R(C::* func)(), S* s, Serializer* pr, const char* data, int len) {
callproxy_(std::function<R()>(std::bind(func, s)), pr, data, len);
}
template<typename R, typename C, typename S, typename P1>
void callproxy_(R(C::* func)(P1), S* s, Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1)>(std::bind(func, s, std::placeholders::_1)), pr, data, len);
}
template<typename R, typename C, typename S, typename P1, typename P2>
void callproxy_(R(C::* func)(P1, P2), S* s, Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1, P2)>(std::bind(func, s, std::placeholders::_1, std::placeholders::_2)), pr, data, len);
}
template<typename R, typename C, typename S, typename P1, typename P2, typename P3>
void callproxy_(R(C::* func)(P1, P2, P3), S* s, Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1, P2, P3)>(std::bind(func, s,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), pr, data, len);
}
template<typename R, typename C, typename S, typename P1, typename P2, typename P3, typename P4>
void callproxy_(R(C::* func)(P1, P2, P3, P4), S* s, Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1, P2, P3, P4)>(std::bind(func, s,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)), pr, data, len);
}
template<typename R, typename C, typename S, typename P1, typename P2, typename P3, typename P4, typename P5>
void callproxy_(R(C::* func)(P1, P2, P3, P4, P5), S* s, Serializer* pr, const char* data, int len) {
callproxy_(std::function<R(P1, P2, P3, P4, P5)>(std::bind(func, s,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)), pr, data, len);
}
// PORXY FUNCTIONAL
template<typename R>
void callproxy_(std::function<R()>, Serializer* pr, const char* data, int len);
template<typename R, typename P1>
void callproxy_(std::function<R(P1)>, Serializer* pr, const char* data, int len);
template<typename R, typename P1, typename P2>
void callproxy_(std::function<R(P1, P2)>, Serializer* pr, const char* data, int len);
template<typename R, typename P1, typename P2, typename P3>
void callproxy_(std::function<R(P1, P2, P3)>, Serializer* pr, const char* data, int len);
template<typename R, typename P1, typename P2, typename P3, typename P4>
void callproxy_(std::function<R(P1, P2, P3, P4)>, Serializer* pr, const char* data, int len);
template<typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
void callproxy_(std::function<R(P1, P2, P3, P4, P5)>, Serializer* pr, const char* data, int len);
private:
std::map<std::string, std::function<void(Serializer*, const char*, int)>> m_handlers;
zmq::context_t m_context;
zmq::socket_t* m_socket;
rpc_err_code m_error_code;
int m_role;
};
buttonrpc::buttonrpc() : m_context(1){
m_error_code = RPC_ERR_SUCCESS;
}
buttonrpc::~buttonrpc(){
m_socket->close();
delete m_socket;
m_context.close();
}
// network
void buttonrpc::as_client( std::string ip, int port )
{
m_role = RPC_CLIENT;
m_socket = new zmq::socket_t(m_context, ZMQ_REQ);
ostringstream os;
os << "tcp://" << ip << ":" << port;
m_socket->connect (os.str());
}
void buttonrpc::as_server( int port )
{
m_role = RPC_SERVER;
m_socket = new zmq::socket_t(m_context, ZMQ_REP);
ostringstream os;
os << "tcp://*:" << port;
m_socket->bind (os.str());
}
void buttonrpc::send( zmq::message_t& data )
{
m_socket->send(data);
}
void buttonrpc::recv( zmq::message_t& data )
{
m_socket->recv(&data);
}
inline void buttonrpc::set_timeout(uint32_t ms)
{
// only client can set
if (m_role == RPC_CLIENT) {
m_socket->setsockopt(ZMQ_RCVTIMEO, ms);
}
}
void buttonrpc::run()
{
// only server can call
if (m_role != RPC_SERVER) {
return;
}
while (1){
zmq::message_t data;
recv(data);
StreamBuffer iodev((char*)data.data(), data.size());
Serializer ds(iodev);
std::string funname;
ds >> funname;
Serializer* r = call_(funname, ds.current(), ds.size()- funname.size());
zmq::message_t retmsg (r->size());
memcpy (retmsg.data (), r->data(), r->size());
send(retmsg);
delete r;
}
}
// ´¦Àíº¯ÊýÏà¹Ø
Serializer* buttonrpc::call_(std::string name, const char* data, int len)
{
Serializer* ds = new Serializer();
if (m_handlers.find(name) == m_handlers.end()) {
(*ds) << value_t<int>::code_type(RPC_ERR_FUNCTIION_NOT_BIND);
(*ds) << value_t<int>::msg_type("function not bind: " + name);
return ds;
}
auto fun = m_handlers[name];
fun(ds, data, len);
ds->reset();
return ds;
}
template<typename F>
void buttonrpc::bind( std::string name, F func )
{
m_handlers[name] = std::bind(&buttonrpc::callproxy<F>, this, func, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
}
template<typename F, typename S>
inline void buttonrpc::bind(std::string name, F func, S* s)
{
m_handlers[name] = std::bind(&buttonrpc::callproxy<F, S>, this, func, s, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
}
template<typename F>
void buttonrpc::callproxy( F fun, Serializer* pr, const char* data, int len )
{
callproxy_(fun, pr, data, len);
}
template<typename F, typename S>
inline void buttonrpc::callproxy(F fun, S * s, Serializer * pr, const char * data, int len)
{
callproxy_(fun, s, pr, data, len);
}
// help call return value type is void function
template<typename R, typename F>
typename std::enable_if<std::is_same<R, void>::value, typename type_xx<R>::type >::type call_helper(F f) {
f();
return 0;
}
template<typename R, typename F>
typename std::enable_if<!std::is_same<R, void>::value, typename type_xx<R>::type >::type call_helper(F f) {
return f();
}
template<typename R>
void buttonrpc::callproxy_(std::function<R()> func, Serializer* pr, const char* data, int len)
{
typename type_xx<R>::type r = call_helper<R>(std::bind(func));
value_t<R> val;
val.set_code(RPC_ERR_SUCCESS);
val.set_val(r);
(*pr) << val;
}
template<typename R, typename P1>
void buttonrpc::callproxy_(std::function<R(P1)> func, Serializer* pr, const char* data, int len)
{
Serializer ds(StreamBuffer(data, len));
P1 p1;
ds >> p1;
typename type_xx<R>::type r = call_helper<R>(std::bind(func, p1));
value_t<R> val;
val.set_code(RPC_ERR_SUCCESS);
val.set_val(r);
(*pr) << val;
}
template<typename R, typename P1, typename P2>
void buttonrpc::callproxy_(std::function<R(P1, P2)> func, Serializer* pr, const char* data, int len )
{
Serializer ds(StreamBuffer(data, len));
P1 p1; P2 p2;
ds >> p1 >> p2;
typename type_xx<R>::type r = call_helper<R>(std::bind(func, p1, p2));
value_t<R> val;
val.set_code(RPC_ERR_SUCCESS);
val.set_val(r);
(*pr) << val;
}
template<typename R, typename P1, typename P2, typename P3>
void buttonrpc::callproxy_(std::function<R(P1, P2, P3)> func, Serializer* pr, const char* data, int len)
{
Serializer ds(StreamBuffer(data, len));
P1 p1; P2 p2; P3 p3;
ds >> p1 >> p2 >> p3;
typename type_xx<R>::type r = call_helper<R>(std::bind(func, p1, p2, p3));
value_t<R> val;
val.set_code(RPC_ERR_SUCCESS);
val.set_val(r);
(*pr) << val;
}
template<typename R, typename P1, typename P2, typename P3, typename P4>
void buttonrpc::callproxy_(std::function<R(P1, P2, P3, P4)> func, Serializer* pr, const char* data, int len)
{
Serializer ds(StreamBuffer(data, len));
P1 p1; P2 p2; P3 p3; P4 p4;
ds >> p1 >> p2 >> p3 >> p4;
typename type_xx<R>::type r = call_helper<R>(std::bind(func, p1, p2, p3, p4));
value_t<R> val;
val.set_code(RPC_ERR_SUCCESS);
val.set_val(r);
(*pr) << val;
}
template<typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
void buttonrpc::callproxy_(std::function<R(P1, P2, P3, P4, P5)> func, Serializer* pr, const char* data, int len)
{
Serializer ds(StreamBuffer(data, len));
P1 p1; P2 p2; P3 p3; P4 p4; P5 p5;
ds >> p1 >> p2 >> p3 >> p4 >> p5;
typename type_xx<R>::type r = call_helper<R>(std::bind(func, p1, p2, p3, p4, p5));
value_t<R> val;
val.set_code(RPC_ERR_SUCCESS);
val.set_val(r);
(*pr) << val;
}
template<typename R>
inline buttonrpc::value_t<R> buttonrpc::net_call(Serializer& ds)
{
zmq::message_t request(ds.size() + 1);
memcpy(request.data(), ds.data(), ds.size());
if (m_error_code != RPC_ERR_RECV_TIMEOUT) {
send(request);
}
zmq::message_t reply;
recv(reply);
value_t<R> val;
if (reply.size() == 0) {
// timeout
m_error_code = RPC_ERR_RECV_TIMEOUT;
val.set_code(RPC_ERR_RECV_TIMEOUT);
val.set_msg("recv timeout");
return val;
}
m_error_code = RPC_ERR_SUCCESS;
ds.clear();
ds.write_raw_data((char*)reply.data(), reply.size());
ds.reset();
ds >> val;
return val;
}
template<typename R>
inline buttonrpc::value_t<R> buttonrpc::call(std::string name)
{
Serializer ds;
ds << name;
return net_call<R>(ds);
}
template<typename R, typename P1>
inline buttonrpc::value_t<R> buttonrpc::call(std::string name, P1 p1)
{
Serializer ds;
ds << name << p1;
return net_call<R>(ds);
}
template<typename R, typename P1, typename P2>
inline buttonrpc::value_t<R> buttonrpc::call( std::string name, P1 p1, P2 p2 )
{
Serializer ds;
ds << name << p1 << p2;
return net_call<R>(ds);
}
template<typename R, typename P1, typename P2, typename P3>
inline buttonrpc::value_t<R> buttonrpc::call(std::string name, P1 p1, P2 p2, P3 p3)
{
Serializer ds;
ds << name << p1 << p2 << p3;
return net_call<R>(ds);
}
template<typename R, typename P1, typename P2, typename P3, typename P4>
inline buttonrpc::value_t<R> buttonrpc::call(std::string name, P1 p1, P2 p2, P3 p3, P4 p4)
{
Serializer ds;
ds << name << p1 << p2 << p3 << p4;
return net_call<R>(ds);
}
template<typename R, typename P1, typename P2, typename P3, typename P4, typename P5>
inline buttonrpc::value_t<R> buttonrpc::call(std::string name, P1 p1, P2 p2, P3 p3, P4 p4, P5 p5)
{
Serializer ds;
ds << name << p1 << p2 << p3 << p4 << p5;
return net_call<R>(ds);
}