| 44 | }; |
| 45 | |
| 46 | int main(int argc, char* argv[]) { |
| 47 | // Parse gflags. We recommend you to use gflags as well. |
| 48 | GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); |
| 49 | |
| 50 | // A Channel represents a communication line to a Server. Notice that |
| 51 | // Channel is thread-safe and can be shared by all threads in your program. |
| 52 | brpc::Channel channel; |
| 53 | |
| 54 | // Initialize the channel, NULL means using default options. |
| 55 | brpc::ChannelOptions options; |
| 56 | options.protocol = FLAGS_protocol; |
| 57 | options.connection_type = FLAGS_connection_type; |
| 58 | options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/; |
| 59 | options.max_retry = FLAGS_max_retry; |
| 60 | if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) { |
| 61 | LOG(ERROR) << "Fail to initialize channel"; |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | // Normally, you should not call a Channel directly, but instead construct |
| 66 | // a stub Service wrapping it. stub can be shared by all threads as well. |
| 67 | example::EchoService_Stub stub(&channel); |
| 68 | |
| 69 | // Send a request and wait for the response every 1 second. |
| 70 | int log_id = 0; |
| 71 | while (!brpc::IsAskedToQuit()) { |
| 72 | example::EchoRequest request1; |
| 73 | example::EchoResponse response1; |
| 74 | brpc::Controller cntl1; |
| 75 | |
| 76 | example::EchoRequest request2; |
| 77 | example::EchoResponse response2; |
| 78 | brpc::Controller cntl2; |
| 79 | |
| 80 | request1.set_message("hello1"); |
| 81 | request2.set_message("hello2"); |
| 82 | |
| 83 | cntl1.set_log_id(log_id ++); // set by user |
| 84 | cntl2.set_log_id(log_id ++); |
| 85 | |
| 86 | const brpc::CallId id1 = cntl1.call_id(); |
| 87 | const brpc::CallId id2 = cntl2.call_id(); |
| 88 | CancelRPC done1(id2); |
| 89 | CancelRPC done2(id1); |
| 90 | |
| 91 | butil::Timer tm; |
| 92 | tm.start(); |
| 93 | // Send 2 async calls and join them. They will cancel each other in |
| 94 | // their done which is run before the RPC being Join()-ed. Canceling |
| 95 | // a finished RPC has no effect. |
| 96 | // For example: |
| 97 | // Time RPC1 RPC2 |
| 98 | // 1 response1 comes back. |
| 99 | // 2 running done1. |
| 100 | // 3 cancel RPC2 |
| 101 | // 4 running done2 (NOTE: done also runs) |
| 102 | // 5 cancel RPC1 (no effect) |
| 103 | stub.Echo(&cntl1, &request1, &response1, &done1); |
nothing calls this directly
no test coverage detected