• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2024 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <grpc/grpc.h>
20 #include <grpcpp/ext/proto_server_reflection_plugin.h>
21 #include <grpcpp/grpcpp.h>
22 
23 #include <iostream>
24 #include <string>
25 
26 #include "absl/flags/flag.h"
27 #include "absl/flags/parse.h"
28 
29 #ifdef BAZEL_BUILD
30 #include "examples/protos/helloworld.grpc.pb.h"
31 #else
32 #include "helloworld.grpc.pb.h"
33 #endif
34 
35 ABSL_FLAG(std::string, target, "localhost:50051", "Server address");
36 ABSL_FLAG(size_t, quota, 20,
37           "Resource quota (in megabytes) that defines how much memory gRPC has "
38           "available for buffers");
39 
40 namespace {
41 
42 class Reader final : public grpc::ClientReadReactor<helloworld::HelloReply> {
43  public:
Start()44   void Start() {
45     StartRead(&res_);
46     StartCall();
47   }
48 
WaitForDone()49   grpc::Status WaitForDone() {
50     absl::MutexLock lock(&mu_);
51     mu_.Await(absl::Condition(
52         +[](Reader* reader) { return reader->result_.has_value(); }, this));
53     return *result_;
54   }
55 
OnReadDone(bool ok)56   void OnReadDone(bool ok) override {
57     if (!ok) {
58       std::cout << "Done reading\n";
59       return;
60     }
61     std::cout << "Read " << res_.message().length() << " bytes.\n";
62     res_.set_message("");
63     // A delay to slow down the client so it can't read responses quick enough
64     sleep(1);
65     StartRead(&res_);
66   }
67 
OnDone(const grpc::Status & status)68   void OnDone(const grpc::Status& status) override {
69     absl::MutexLock lock(&mu_);
70     result_ = status;
71   }
72 
73  private:
74   absl::Mutex mu_;
75   absl::optional<grpc::Status> result_;
76   helloworld::HelloReply res_;
77 };
78 
79 }  // namespace
80 
main(int argc,char * argv[])81 int main(int argc, char* argv[]) {
82   absl::ParseCommandLine(argc, argv);
83   grpc::ChannelArguments channel_arguments;
84   grpc::ResourceQuota quota;
85   quota.Resize(absl::GetFlag(FLAGS_quota) * 1024 * 1024);
86   channel_arguments.SetResourceQuota(quota);
87   auto channel = grpc::CreateCustomChannel(absl::GetFlag(FLAGS_target),
88                                            grpc::InsecureChannelCredentials(),
89                                            channel_arguments);
90   auto greeter = helloworld::Greeter::NewStub(channel);
91   grpc::ClientContext ctx;
92   helloworld::HelloRequest req;
93   req.set_name("World");
94   Reader reader;
95   greeter->async()->SayHelloStreamReply(&ctx, &req, &reader);
96   reader.Start();
97   auto status = reader.WaitForDone();
98   if (status.ok()) {
99     std::cout << "Success\n";
100   } else {
101     std::cerr << "Failed with error: " << status.error_message() << "\n";
102   }
103   return 0;
104 }
105