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

Function main

example/streaming_batch_echo_c++/client.cpp:56–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54};
55
56int main(int argc, char* argv[]) {
57 // Parse gflags. We recommend you to use gflags as well.
58 GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
59
60 // A Channel represents a communication line to a Server. Notice that
61 // Channel is thread-safe and can be shared by all threads in your program.
62 brpc::Channel channel;
63
64 // Initialize the channel, NULL means using default options.
65 brpc::ChannelOptions options;
66 options.protocol = brpc::PROTOCOL_BAIDU_STD;
67 options.connection_type = FLAGS_connection_type;
68 options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
69 options.max_retry = FLAGS_max_retry;
70 if (channel.Init(FLAGS_server.c_str(), NULL) != 0) {
71 LOG(ERROR) << "Fail to initialize channel";
72 return -1;
73 }
74
75 // Normally, you should not call a Channel directly, but instead construct
76 // a stub Service wrapping it. stub can be shared by all threads as well.
77 example::EchoService_Stub stub(&channel);
78 StreamClientReceiver receiver;
79 brpc::Controller cntl;
80 brpc::StreamIds streams;
81 brpc::StreamOptions stream_options;
82 stream_options.handler = &receiver;
83 if (brpc::StreamCreate(streams, 3, cntl, &stream_options) != 0) {
84 LOG(ERROR) << "Fail to create stream";
85 return -1;
86 }
87 for(size_t i = 0; i < streams.size(); ++i) {
88 LOG(INFO) << "Created Stream=" << streams[i];
89 }
90 example::EchoRequest request;
91 example::EchoResponse response;
92 request.set_message("I'm a RPC to connect stream");
93 stub.Echo(&cntl, &request, &response, NULL);
94 if (cntl.Failed()) {
95 LOG(ERROR) << "Fail to connect stream, " << cntl.ErrorText();
96 return -1;
97 }
98
99 while (!brpc::IsAskedToQuit()) {
100 butil::IOBuf msg1;
101 msg1.append("abcdefghijklmnopqrstuvwxyz");
102 CHECK_EQ(0, brpc::StreamWrite(streams[0], msg1));
103 butil::IOBuf msg2;
104 msg2.append("0123456789");
105 CHECK_EQ(0, brpc::StreamWrite(streams[1], msg2));
106 sleep(1);
107 butil::IOBuf msg3;
108 msg3.append("hello world");
109 CHECK_EQ(0, brpc::StreamWrite(streams[2], msg3));
110 sleep(1);
111 }
112
113 CHECK_EQ(0, brpc::StreamClose(streams[0]));

Callers

nothing calls this directly

Calls 11

StreamCreateFunction · 0.85
IsAskedToQuitFunction · 0.85
StreamWriteFunction · 0.85
StreamCloseFunction · 0.85
ErrorTextMethod · 0.80
InitMethod · 0.45
c_strMethod · 0.45
sizeMethod · 0.45
EchoMethod · 0.45
FailedMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected