-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
138 lines (129 loc) · 3.46 KB
/
client.cpp
File metadata and controls
138 lines (129 loc) · 3.46 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
///*示例1*/
// #include <iostream>
// #include <thread>
// #include "rpc_client.h"
//
// using namespace std;
//
//
//// 封装客户端逻辑的函数
// void clientFunction(std::string name, int a, int b) {
// rpc_client client("127.0.0.1", 9000);// IP 地址,端口号
// std::cout << "Address of rpc_client instance: " << &client << std::endl;
// bool has_connected = client.connect(5);
// /*没有建立连接则退出程序*/
// if (!has_connected) {
// std::cout << "connect timeout" << std::endl;
// return;
// }
//
// auto asy_ = client.async_call<std::string>("GreetFun",
//"async_call_hello_tycrpc");
//
// std::string ret = client.call<std::string>("GreetFun", name);
// int ans = client.call<int>("calcFun", a, b);
//
// cout << "GreetFun = :" << ret << endl;
// cout << "calcFun = :" << ans << endl;
//
// std::string aaaa = asy_->get();
// cout << "GreetFun_async = :" << aaaa << endl;
//
// printf("===========================\n");
// }
//
//
//
// int main() {
// clientFunction("tycRPC", 100, 55);
// return 0;
// }
///*示例2*/
// #include <iostream>
// #include <thread>
// #include "rpc_client.h"
//
// using namespace std;
//
// static rpc_client client("127.0.0.1", 9000);// IP 地址,端口号
//
//// 封装客户端逻辑的函数
// void clientFunction(std::string name, int a, int b) {
// std::string ret = client.call<std::string>("GreetFun", name);
// int ans = client.call<int>("calcFun", a, b);
//
// cout << "GreetFun = :" << ret << endl;
// cout << "calcFun = :" << ans << endl;
// }
//
//
// int main() {
// /*设定超时 5s(不填默认为 3s),connect 超时返回 false,成功返回 true*/
// bool has_connected = client.connect(5);
// /*没有建立连接则退出程序*/
// if (!has_connected) {
// std::cout << "connect timeout" << std::endl;
// exit(-1);
// }
//
// // 创建6个线程
// std::thread threads[6];
//
// // 使用clientFunction启动每个线程
// for (int i = 0; i < 6; ++i) {
// string tmp = "num calc:";
// tmp += to_string(i + 2);
// tmp += " + ";
// tmp += to_string(i * 9);
// tmp += " = ";
// threads[i] = std::thread(clientFunction, tmp, i + 2, i * 9);
// }
//
// // 等待所有线程完成
// for (int i = 0; i < 6; ++i) {
// threads[i].join();
// }
//
//
// return 0;
// }
/*示例3*/
#include <iostream>
#include <thread>
#include "rpc_client.h"
using namespace std;
// 封装客户端逻辑的函数
void clientFunction(std::string name, int a, int b) {
Sleep((a - 2) * 1000);
rpc_client client("127.0.0.1", 9000); // IP 地址,端口号
std::cout << "Address of rpc_client instance: " << &client << std::endl;
bool has_connected = client.connect(5);
/*没有建立连接则退出程序*/
if (!has_connected) {
std::cout << "connect timeout" << std::endl;
return;
}
std::string ret = client.call<std::string>("GreetFun", name);
int ans = client.call<int>("calcFun", a, b);
cout << "GreetFun = :" << ret << endl;
cout << "calcFun = :" << ans << endl;
printf("===========================\n");
}
int main() {
// 创建6个线程
std::thread threads[6];
// 使用clientFunction启动每个线程
for (int i = 0; i < 6; ++i) {
string tmp = "num calc:";
tmp += to_string(i + 2);
tmp += " + ";
tmp += to_string(i * 9);
tmp += " = ";
threads[i] = std::thread(clientFunction, tmp, i + 2, i * 9);
}
// 等待所有线程完成
for (int i = 0; i < 6; ++i) {
threads[i].join();
}
return 0;
}