• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2021 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 <grpcpp/ext/proto_server_reflection_plugin.h>
20 #include <grpcpp/grpcpp.h>
21 #include <grpcpp/health_check_service_interface.h>
22 
23 #include <cstddef>
24 #include <cstdint>
25 #include <iostream>
26 #include <string>
27 
28 #include "absl/flags/flag.h"
29 #include "absl/flags/parse.h"
30 #include "absl/strings/str_format.h"
31 
32 #ifdef BAZEL_BUILD
33 #include "examples/protos/helloworld.grpc.pb.h"
34 #else
35 #include "helloworld.grpc.pb.h"
36 #endif
37 
38 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
39 ABSL_FLAG(size_t, quota, 20, "Resource quota, in megabytes");
40 
41 namespace {
42 
43 //
44 // Server reactor that is slow to read incoming messages, causing the buffers
45 // to fill.
46 //
47 class SlowReadingBidiReactor final
48     : public grpc::ServerBidiReactor<helloworld::HelloRequest,
49                                      helloworld::HelloReply> {
50  public:
SlowReadingBidiReactor()51   SlowReadingBidiReactor() { StartRead(&req_); }
52 
OnReadDone(bool ok)53   void OnReadDone(bool ok) override {
54     std::cout << "Recieved request with " << req_.name().length()
55               << " bytes name\n";
56     if (!ok) {
57       Finish(grpc::Status::OK);
58       return;
59     }
60     sleep(1);
61     StartRead(&req_);
62   }
63 
OnDone()64   void OnDone() override {
65     std::cout << "Done\n";
66     delete this;
67   }
68 
69  private:
70   absl::Mutex mu_;
71   helloworld::HelloRequest req_;
72 };
73 
74 // Logic and data behind the server's behavior.
75 class GreeterServiceImpl final : public helloworld::Greeter::CallbackService {
76   grpc::ServerBidiReactor<helloworld::HelloRequest, helloworld::HelloReply>*
SayHelloBidiStream(grpc::CallbackServerContext *)77   SayHelloBidiStream(grpc::CallbackServerContext* /* context */) override {
78     return new SlowReadingBidiReactor();
79   }
80 };
81 
82 }  // namespace
83 
RunServer(uint16_t port)84 void RunServer(uint16_t port) {
85   std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
86   GreeterServiceImpl service;
87 
88   grpc::EnableDefaultHealthCheckService(true);
89   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
90   grpc::ServerBuilder builder;
91   // Listen on the given address without any authentication mechanism.
92   builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
93   // Register "service" as the instance through which we'll communicate with
94   // clients. In this case it corresponds to an *synchronous* service.
95   builder.RegisterService(&service);
96   grpc::ResourceQuota quota;
97   quota.Resize(absl::GetFlag(FLAGS_quota) * 1024 * 1024);
98   // Finally assemble the server.
99   auto server = builder.BuildAndStart();
100   std::cout << "Server listening on " << server_address << std::endl;
101 
102   // Wait for the server to shutdown. Note that some other thread must be
103   // responsible for shutting down the server for this call to ever return.
104   server->Wait();
105 }
106 
main(int argc,char ** argv)107 int main(int argc, char** argv) {
108   absl::ParseCommandLine(argc, argv);
109   RunServer(absl::GetFlag(FLAGS_port));
110   return 0;
111 }
112