-
Notifications
You must be signed in to change notification settings - Fork 373
/
main.cpp
655 lines (567 loc) · 16 KB
/
main.cpp
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#include <chrono>
#include <fstream>
#include <iostream>
#include <rest_rpc.hpp>
using namespace rest_rpc;
using namespace rest_rpc::rpc_service;
void test_add() {
try {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
{
auto result = client.call<int>("add", 1, 2);
std::cout << result << std::endl;
}
{
auto result = client.call<2000, int>("add", 1, 2);
std::cout << result << std::endl;
}
} catch (const std::exception &e) {
std::cout << e.what() << std::endl;
}
}
void test_translate() {
try {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
auto result = client.call<std::string>("translate", "hello");
std::cout << result << std::endl;
} catch (const std::exception &e) {
std::cout << e.what() << std::endl;
}
}
void test_hello() {
try {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
client.call("hello", "purecpp");
} catch (const std::exception &e) {
std::cout << e.what() << std::endl;
}
}
struct person {
int id;
std::string name;
int age;
MSGPACK_DEFINE(id, name, age);
};
void test_get_person_name() {
try {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
auto result =
client.call<std::string>("get_person_name", person{1, "tom", 20});
std::cout << result << std::endl;
} catch (const std::exception &e) {
std::cout << e.what() << std::endl;
}
}
void test_get_person() {
try {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
auto result = client.call<person>("get_person");
std::cout << result.name << std::endl;
} catch (const std::exception &e) {
std::cout << e.what() << std::endl;
}
}
void test_async_client() {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
client.set_error_callback(
[](asio::error_code ec) { std::cout << ec.message() << std::endl; });
auto f = client.async_call<FUTURE>("get_person");
if (f.wait_for(std::chrono::milliseconds(50)) ==
std::future_status::timeout) {
std::cout << "timeout" << std::endl;
} else {
auto p = f.get().as<person>();
std::cout << p.name << std::endl;
}
auto fu = client.async_call<FUTURE>("hello", "purecpp");
fu.get().as(); // no return
// sync call
client.call("hello", "purecpp");
auto p = client.call<person>("get_person");
std::string str;
std::cin >> str;
}
void test_upload() {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect(1);
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
std::ifstream file("E:/acl.7z", std::ios::binary);
file.seekg(0, std::ios::end);
size_t file_len = file.tellg();
file.seekg(0, std::ios::beg);
std::string conent;
conent.resize(file_len);
file.read(&conent[0], file_len);
std::cout << file_len << std::endl;
{
auto f = client.async_call<FUTURE>("upload", "test", conent);
if (f.wait_for(std::chrono::milliseconds(500)) ==
std::future_status::timeout) {
std::cout << "timeout" << std::endl;
} else {
f.get().as();
std::cout << "ok" << std::endl;
}
}
{
auto f = client.async_call<FUTURE>("upload", "test1", conent);
if (f.wait_for(std::chrono::milliseconds(500)) ==
std::future_status::timeout) {
std::cout << "timeout" << std::endl;
} else {
f.get().as();
std::cout << "ok" << std::endl;
}
}
}
void test_download() {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect(1);
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
auto f = client.async_call<FUTURE>("download", "test");
if (f.wait_for(std::chrono::milliseconds(500)) ==
std::future_status::timeout) {
std::cout << "timeout" << std::endl;
} else {
auto content = f.get().as<std::string>();
std::cout << content.size() << std::endl;
std::ofstream file("test", std::ios::binary);
file.write(content.data(), content.size());
}
}
struct dummy1 {
size_t id;
std::wstring str;
MSGPACK_DEFINE(id, str);
};
void test_echo() {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
{
dummy1 d1{42, L"test"};
auto result = client.call<dummy1>("get_dummy", d1);
std::cout << result.id << std::endl;
}
{
auto result = client.call<std::string>("echo", "test");
std::cout << result << std::endl;
}
{
auto result = client.call<std::string>("delay_echo", "test");
std::cout << result << std::endl;
}
}
void test_sync_client() {
test_add();
test_translate();
test_hello();
test_get_person_name();
test_get_person();
}
void test_performance1() {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
person p{1, "tom", 20};
for (size_t i = 0; i < 100000000; i++) {
auto future = client.async_call<FUTURE>("get_name", p);
auto status = future.wait_for(std::chrono::seconds(2));
if (status == std::future_status::deferred) {
std::cout << "deferred\n";
} else if (status == std::future_status::timeout) {
std::cout << "timeout\n";
} else if (status == std::future_status::ready) {
}
}
std::cout << "finish\n";
std::string str;
std::cin >> str;
}
void multi_client_performance(size_t n) {
std::vector<std::shared_ptr<std::thread>> group;
for (int i = 0; i < n; ++i) {
group.emplace_back(std::make_shared<std::thread>(
test_performance1)); // []{test_performance1();});
}
for (auto &p : group) {
p->join();
}
}
void test_performance2() {
rpc_client client("127.0.0.1", 9000);
bool r = client.connect();
if (!r) {
std::cout << "connect timeout" << std::endl;
return;
}
person p{1, "tom", 20};
try {
for (size_t i = 0; i < 100000000; i++) {
client.call<std::string>("get_name", p);
}
std::cout << "finish\n";
} catch (const std::exception &ex) {
std::cout << ex.what() << '\n';
}
std::string str;
std::cin >> str;
}
void test_call_with_timeout() {
rpc_client client;
client.async_connect("127.0.0.1", 9000);
try {
auto result = client.call<50, person>("get_person");
std::cout << result.name << std::endl;
client.close();
bool r = client.connect();
result = client.call<50, person>("get_person");
std::cout << result.name << std::endl;
} catch (const std::exception &ex) {
std::cout << ex.what() << std::endl;
}
std::string str;
std::cin >> str;
}
void test_connect() {
rpc_client client;
client.enable_auto_reconnect(); // automatic reconnect
client.enable_auto_heartbeat(); // automatic heartbeat
bool r = client.connect("127.0.0.1", 9000);
int count = 0;
while (true) {
if (client.has_connected()) {
std::cout << "connected ok\n";
break;
} else {
std::cout << "connected failed: " << count++ << "\n";
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
//{
// rpc_client client;
// bool r = client.connect("127.0.0.1", 9000);
// int count = 0;
// while (true) {
// if (client.connect()) {
// std::cout << "connected ok\n";
// break;
// }
// else {
// std::cout << "connected failed: " << count++ << "\n";
// }
// }
//}
std::string str;
std::cin >> str;
}
void test_callback() {
rpc_client client;
bool r = client.connect("127.0.0.1", 9000);
for (size_t i = 0; i < 10; i++) {
std::string test = "test" + std::to_string(i + 1);
// set timeout 100ms
client.async_call<10000>(
"delay_echo",
[](const asio::error_code &ec, string_view data) {
if (ec) {
std::cout << ec.value() << " timeout" << std::endl;
return;
}
auto str = as<std::string>(data);
std::cout << "delay echo " << str << '\n';
},
test);
std::string test1 = "test" + std::to_string(i + 2);
// zero means no timeout check, no param means using default timeout(5s)
client.async_call<0>(
"echo",
[](const asio::error_code &ec, string_view data) {
auto str = as<std::string>(data);
std::cout << "echo " << str << '\n';
},
test1);
}
client.run();
}
void wait_for_notification(rpc_client &client) {
client.async_call<0>("sub",
[&client](const asio::error_code &ec, string_view data) {
auto str = as<std::string>(data);
std::cout << str << '\n';
wait_for_notification(client);
});
}
void test_sub1() {
rpc_client client;
client.enable_auto_reconnect();
client.enable_auto_heartbeat();
bool r = client.connect("127.0.0.1", 9000);
if (!r) {
return;
}
client.subscribe("key", [](string_view data) { std::cout << data << "\n"; });
client.subscribe(
"key", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1",
[](string_view data) {
msgpack_codec codec;
person p = codec.unpack<person>(data.data(), data.size());
std::cout << p.name << "\n";
});
client.subscribe(
"key1",
"048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1",
[](string_view data) { std::cout << data << "\n"; });
bool stop = false;
std::thread thd1([&client, &stop] {
while (true) {
try {
if (client.has_connected()) {
int r = client.call<int>("add", 2, 3);
std::cout << "add result: " << r << "\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} catch (const std::exception &ex) {
std::cout << ex.what() << "\n";
}
}
});
/*rpc_client client1;
bool r1 = client1.connect("127.0.0.1", 9000);
if (!r1) {
return;
}
person p{10, "jack", 21};
client1.publish("key", "hello subscriber");
client1.publish_by_token("key", "sub_key", p);
std::thread thd([&client1, p] {
while (true) {
try {
client1.publish("key", "hello subscriber");
client1.publish_by_token("key", "unique_token", p);
}
catch (const std::exception& ex) {
std::cout << ex.what() << "\n";
}
}
});
*/
std::string str;
std::cin >> str;
}
void test_multiple_thread() {
std::vector<std::shared_ptr<rpc_client>> cls;
std::vector<std::shared_ptr<std::thread>> v;
for (int j = 0; j < 4; ++j) {
auto client = std::make_shared<rpc_client>();
cls.push_back(client);
bool r = client->connect("127.0.0.1", 9000);
if (!r) {
return;
}
for (size_t i = 0; i < 2; i++) {
person p{1, "tom", 20};
v.emplace_back(std::make_shared<std::thread>([client] {
person p{1, "tom", 20};
for (size_t i = 0; i < 1000000; i++) {
client->async_call<0>(
"get_name",
[](const asio::error_code &ec, string_view data) {
if (ec) {
std::cout << ec.message() << '\n';
}
},
p);
// auto future = client->async_call<FUTURE>("get_name", p);
// auto status = future.wait_for(std::chrono::seconds(2));
// if (status == std::future_status::deferred) {
// std::cout << "deferred\n";
//}
// else if (status == std::future_status::timeout) {
// std::cout << "timeout\n";
//}
// else if (status == std::future_status::ready) {
//}
// client->call<std::string>("get_name", p);
}
}));
}
}
std::string str;
std::cin >> str;
}
void test_threads() {
rpc_client client;
bool r = client.connect("127.0.0.1", 9000);
if (!r) {
return;
}
std::thread thd1([&client] {
for (size_t i = 0; i < 1000000; i++) {
auto future = client.async_call<FUTURE>("echo", "test");
auto status = future.wait_for(std::chrono::seconds(2));
if (status == std::future_status::timeout) {
std::cout << "timeout\n";
} else if (status == std::future_status::ready) {
std::string content = future.get().as<std::string>();
}
std::this_thread::sleep_for(std::chrono::microseconds(2));
}
std::cout << "thread2 finished" << '\n';
});
std::thread thd2([&client] {
for (size_t i = 1000000; i < 2 * 1000000; i++) {
client.async_call(
"get_int",
[i](asio::error_code ec, string_view data) {
if (ec) {
std::cout << ec.message() << '\n';
return;
}
int r = as<int>(data);
if (r != i) {
std::cout << "error not match" << '\n';
}
},
i);
std::this_thread::sleep_for(std::chrono::microseconds(2));
}
std::cout << "thread2 finished" << '\n';
});
for (size_t i = 2 * 1000000; i < 3 * 1000000; i++) {
auto future = client.async_call<FUTURE>("echo", "test");
auto status = future.wait_for(std::chrono::seconds(2));
if (status == std::future_status::timeout) {
std::cout << "timeout\n";
} else if (status == std::future_status::ready) {
std::string content = future.get().as<std::string>();
}
std::this_thread::sleep_for(std::chrono::microseconds(2));
}
std::cout << "thread finished" << '\n';
std::string str;
std::cin >> str;
}
void test_ssl() {
bool is_ssl = true;
rpc_client client;
client.set_error_callback(
[](asio::error_code ec) { std::cout << ec.message() << "\n"; });
#ifdef CINATRA_ENABLE_SSL
client.set_ssl_context_callback([](asio::ssl::context &ctx) {
ctx.set_verify_mode(asio::ssl::context::verify_peer);
ctx.load_verify_file("server.crt");
});
#endif
bool r = client.connect("127.0.0.1", 9000, is_ssl);
if (!r) {
return;
}
for (size_t i = 0; i < 100; i++) {
try {
auto result = client.call<std::string>("echo", "purecpp");
std::cout << result << " sync\n";
} catch (const std::exception &e) {
std::cout << e.what() << " sync\n";
}
auto future = client.async_call<CallModel::future>("echo", "purecpp");
auto status = future.wait_for(std::chrono::milliseconds(5000));
if (status == std::future_status::timeout) {
std::cout << "timeout future\n";
} else {
auto result1 = future.get();
std::cout << result1.as<std::string>() << " future\n";
}
client.async_call(
"echo",
[](asio::error_code ec, string_view data) {
if (ec) {
std::cout << ec.message() << " " << data << "\n";
return;
}
auto result = as<std::string>(data);
std::cout << result << " async\n";
},
"purecpp");
}
std::getchar();
}
void benchmark_test() {
rpc_client client;
bool r = client.connect("127.0.0.1", 9000);
if (!r) {
return;
}
for (size_t i = 0; i < 1000000; i++) {
client.async_call(
"echo",
[i](asio::error_code ec, string_view data) {
if (ec) {
return;
}
},
"hello wolrd");
}
std::getchar();
std::cout << "benchmark test finished\n";
}
int main() {
// benchmark_test();
test_connect();
test_callback();
test_echo();
test_sync_client();
test_async_client();
test_threads();
test_sub1();
test_call_with_timeout();
test_connect();
test_upload();
test_download();
multi_client_performance(20);
test_performance1();
test_multiple_thread();
return 0;
}