MCPcopy Create free account
hub / github.com/apache/brpc / main

Function main

example/rpcz_echo_c++/client.cpp:38–95  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36DEFINE_int32(interval_ms, 1000, "Milliseconds between consecutive requests");
37
38int main(int argc, char* argv[]) {
39 // Parse gflags. We recommend you to use gflags as well.
40 GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
41
42 // brpc::FLAGS_enable_rpcz = true;
43 // A Channel represents a communication line to a Server. Notice that
44 // Channel is thread-safe and can be shared by all threads in your program.
45 brpc::Channel channel;
46
47 // Initialize the channel, NULL means using default options.
48 brpc::ChannelOptions options;
49 options.protocol = FLAGS_protocol;
50 options.connection_type = FLAGS_connection_type;
51 options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
52 options.max_retry = FLAGS_max_retry;
53 if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
54 LOG(ERROR) << "Fail to initialize channel";
55 return -1;
56 }
57
58 // Normally, you should not call a Channel directly, but instead construct
59 // a stub Service wrapping it. stub can be shared by all threads as well.
60 example::EchoService_Stub stub(&channel);
61
62 // Send a request and wait for the response every 1 second.
63 int log_id = 0;
64 while (!brpc::IsAskedToQuit()) {
65 // We will receive response synchronously, safe to put variables
66 // on stack.
67 example::EchoRequest request;
68 example::EchoResponse response;
69 brpc::Controller cntl;
70
71 request.set_message("hello world");
72
73 cntl.set_log_id(log_id ++); // set by user
74 // Set attachment which is wired to network directly instead of
75 // being serialized into protobuf messages.
76 cntl.request_attachment().append(FLAGS_attachment);
77
78 // Because `done'(last parameter) is NULL, this function waits until
79 // the response comes back or error occurs(including timedout).
80 stub.Echo(&cntl, &request, &response, NULL);
81 if (!cntl.Failed()) {
82 LOG(INFO) << "Received response from " << cntl.remote_side()
83 << " to " << cntl.local_side()
84 << ": " << response.message() << " (attached="
85 << cntl.response_attachment() << ")"
86 << " latency=" << cntl.latency_us() << "us";
87 } else {
88 LOG(WARNING) << cntl.ErrorText();
89 }
90 usleep(FLAGS_interval_ms * 1000L);
91 }
92
93 LOG(INFO) << "EchoClient is going to quit";
94 return 0;
95}

Callers

nothing calls this directly

Calls 12

IsAskedToQuitFunction · 0.85
latency_usMethod · 0.80
ErrorTextMethod · 0.80
InitMethod · 0.45
c_strMethod · 0.45
set_log_idMethod · 0.45
appendMethod · 0.45
EchoMethod · 0.45
FailedMethod · 0.45
remote_sideMethod · 0.45
local_sideMethod · 0.45
messageMethod · 0.45

Tested by

no test coverage detected