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

Function main

example/echo_c++/client.cpp:36–97  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 13

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

Tested by

no test coverage detected