forked from trpc-group/trpc-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cc
308 lines (272 loc) · 9.59 KB
/
client.cc
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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#include <iostream>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include "gflags/gflags.h"
#include "trpc/client/make_client_context.h"
#include "trpc/client/trpc_client.h"
#include "trpc/common/config/trpc_config.h"
#include "trpc/common/runtime_manager.h"
#include "trpc/common/status.h"
#include "trpc/common/trpc_plugin.h"
#include "trpc/coroutine/fiber.h"
#include "trpc/coroutine/fiber_latch.h"
#include "trpc/util/log/logging.h"
#include "examples/features/trpc_stream/server/stream.trpc.pb.h"
DEFINE_string(service_name, "trpc.test.helloworld.StreamGreeter", "callee service name");
DEFINE_string(client_config, "trpc_cpp_fiber.yaml", "");
DEFINE_string(addr, "127.0.0.1:24757", "ip:port");
DEFINE_string(rpc_method, "ClientStreamSayHello", "RPC method name");
DEFINE_int32(request_count, 3, "count of request");
namespace test::helloworld {
using StreamGreeterServiceProxy = ::trpc::test::helloworld::StreamGreeterServiceProxy;
using StreamGreeterServiceProxyPtr = std::shared_ptr<StreamGreeterServiceProxy>;
int GetRequestCount(int request_count) {
if (request_count > 0) {
return std::min(100, FLAGS_request_count);
}
return std::numeric_limits<int>::max();
}
bool CallClientStreamSayHello(const StreamGreeterServiceProxyPtr& proxy, int request_count) {
auto context = ::trpc::MakeClientContext(proxy);
::trpc::test::helloworld::HelloReply reply;
int send_count{0};
int send_bytes{0};
::trpc::Status status{0, 0, "OK"};
bool ok{true};
do {
auto stream = proxy->ClientStreamSayHello(context, &reply);
if (!stream.GetStatus().OK()) {
std::cerr << "stream error:" << stream.GetStatus().ToString() << std::endl;
ok = false;
break;
}
for (int i = 0; i < request_count; ++i) {
::trpc::test::helloworld::HelloRequest request;
request.set_msg("ClientStreamSayHello#" + std::to_string(i + 1));
status = stream.Write(request);
if (status.OK()) {
++send_count;
send_bytes += request.msg().size();
std::cout << "send request msg:" << request.msg() << std::endl;
continue;
}
break;
}
// Check: last write is ok.
if (status.OK()) {
status = stream.WriteDone();
if (status.OK()) {
// Waits the final status of the RPC calling.
status = stream.Finish();
} else {
std::cerr << "write done error: " << status.ToString() << std::endl;
ok = false;
}
} else {
std::cerr << "write error: " << status.ToString() << std::endl;
ok = false;
}
if (status.OK()) {
std::cout << "stream rpc succeed, send count: " << send_count << ", send bytes: " << send_bytes
<< ", reply: " << reply.msg() << std::endl;
} else {
std::cerr << "stream rpc failed:" << status.ToString() << std::endl;
ok = false;
}
} while (0);
return ok;
}
bool CallServerStreamSayHello(const StreamGreeterServiceProxyPtr& proxy) {
auto context = ::trpc::MakeClientContext(proxy);
::trpc::test::helloworld::HelloRequest request;
request.set_msg("ServerStreamSayHello");
auto stream = proxy->ServerStreamSayHello(context, request);
::trpc::Status status{0, 0, "OK"};
bool ok{true};
do {
if (!stream.GetStatus().OK()) {
std::cout << "stream error:" << stream.GetStatus().ToString() << std::endl;
ok = false;
break;
}
for (;;) {
::trpc::test::helloworld::HelloReply reply;
status = stream.Read(&reply, 2000);
if (status.OK()) {
std::cout << "stream got reply:" << reply.msg() << std::endl;
continue;
}
if (status.StreamEof()) {
std::cout << "got EOF" << std::endl;
// Waits the final status of the RPC calling.
status = stream.Finish();
}
break;
}
if (status.OK()) {
std::cout << "stream rpc succeed" << std::endl;
} else {
std::cerr << "stream rpc failed:" << status.ToString() << std::endl;
ok = false;
}
} while (0);
return ok;
}
bool CallBidiStreamSayHello(const StreamGreeterServiceProxyPtr& proxy, int request_count) {
auto context = ::trpc::MakeClientContext(proxy);
auto stream = proxy->BidiStreamSayHello(context);
::trpc::Status status{0, 0, "OK"};
bool ok{true};
do {
if (!stream.GetStatus().OK()) {
std::cout << "stream error:" << stream.GetStatus().ToString() << std::endl;
ok = false;
break;
}
for (int i = 0; i < request_count; ++i) {
std::stringstream request_msg;
request_msg << "BidiStreamSayHello, " << (i + 1);
::trpc::test::helloworld::HelloRequest request;
request.set_msg(request_msg.str());
status = stream.Write(request);
if (status.OK()) {
continue;
}
break;
}
if (!status.OK()) {
std::cerr << "write error: " << status.ToString() << std::endl;
ok = false;
break;
}
status = stream.WriteDone();
if (!status.OK()) {
std::cerr << "write done error: " << status.ToString() << std::endl;
ok = false;
break;
}
::trpc::test::helloworld::HelloReply reply;
for (;;) {
status = stream.Read(&reply, 2000);
if (status.OK()) {
std::stringstream reply_msg;
reply_msg << "stream got reply:" << reply.msg();
std::cout << reply_msg.str() << std::endl;
continue;
}
if (status.StreamEof()) {
std::cout << "got EOF" << std::endl;
// Waits the final status of the RPC calling.
status = stream.Finish();
}
break;
}
if (status.OK()) {
std::cout << "stream rpc succeed" << std::endl;
} else {
std::cerr << "stream rpc failed:" << status.ToString() << std::endl;
ok = false;
}
} while (0);
return ok;
}
int Run() {
bool final_ok{true};
struct calling_args_t {
std::string calling_name;
std::function<bool()> calling_executor;
bool ok;
};
std::vector<calling_args_t> callings{};
int request_count = GetRequestCount(FLAGS_request_count);
std::string rpc_method = FLAGS_rpc_method;
::trpc::ServiceProxyOption option;
option.name = FLAGS_service_name;
option.codec_name = "trpc";
option.network = "tcp";
option.conn_type = "long";
option.timeout = 1000;
option.selector_name = "direct";
option.target = FLAGS_addr;
auto stream_greeter_proxy = ::trpc::GetTrpcClient()->GetProxy<StreamGreeterServiceProxy>(FLAGS_service_name, option);
std::string calling_name{""};
std::function<bool()> calling_executor{nullptr};
if (rpc_method == "ClientStreamSayHello") {
calling_name = "Streaming RPC, ClientStreamSayHello";
calling_executor = [&stream_greeter_proxy, request_count]() {
return CallClientStreamSayHello(stream_greeter_proxy, request_count);
};
} else if (rpc_method == "ServerStreamSayHello") {
calling_name = "Streaming RPC, ServerStreamSayHello";
calling_executor = [&stream_greeter_proxy]() { return CallServerStreamSayHello(stream_greeter_proxy); };
} else if (rpc_method == "BidiStreamSayHello") {
calling_name = "Streaming RPC, BidiStreamSayHello";
calling_executor = [&stream_greeter_proxy, request_count]() {
return CallClientStreamSayHello(stream_greeter_proxy, request_count);
};
} else {
std::cout << "RPC method is empty, nothing todo" << std::endl;
return 0;
}
// Executing multiple cases is to send concurrent requests.
for (int i = 0; i < 8; i++) {
callings.push_back({calling_name + std::to_string(i + 1), calling_executor, false});
}
auto latch_count = static_cast<std::ptrdiff_t>(callings.size());
::trpc::FiberLatch callings_latch{latch_count};
for (auto& c : callings) {
::trpc::StartFiberDetached([&callings_latch, &c]() {
c.ok = c.calling_executor();
callings_latch.CountDown();
});
}
callings_latch.Wait();
for (const auto& c : callings) {
final_ok &= c.ok;
std::cout << "name: " << c.calling_name << ", ok: " << c.ok << std::endl;
}
std::cout << "final result of streaming RPC calling: " << final_ok << std::endl;
return final_ok ? 0 : -1;
}
} // namespace test::helloworld
bool ParseClientConfig(int argc, char* argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);
google::CommandLineFlagInfo info;
if (GetCommandLineFlagInfo("client_config", &info) && info.is_default) {
std::cerr << "start client with client_config, for example: " << argv[0]
<< " --client_config=/client/client_config/filepath" << std::endl;
return false;
}
std::cout << "FLAGS_service_name: " << FLAGS_service_name << std::endl;
std::cout << "FLAGS_client_config: " << FLAGS_client_config << std::endl;
std::cout << "FLAGS_addr: " << FLAGS_addr << std::endl;
std::cout << "FLAGS_rpc_method: " << FLAGS_rpc_method << std::endl;
std::cout << "FLAGS_request_count: " << FLAGS_request_count << std::endl;
return true;
}
int main(int argc, char* argv[]) {
if (!ParseClientConfig(argc, argv)) {
exit(-1);
}
if (::trpc::TrpcConfig::GetInstance()->Init(FLAGS_client_config) != 0) {
std::cerr << "load client_config failed." << std::endl;
exit(-1);
}
// If the business code is running in trpc pure client mode, the business code needs to be running in the
// `RunInTrpcRuntime` function
return ::trpc::RunInTrpcRuntime([]() { return test::helloworld::Run(); });
}