greeter_callback_server.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <iostream>
  19. #include <memory>
  20. #include <string>
  21. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  22. #include <grpcpp/grpcpp.h>
  23. #include <grpcpp/health_check_service_interface.h>
  24. #ifdef BAZEL_BUILD
  25. #include "examples/protos/helloworld.grpc.pb.h"
  26. #else
  27. #include "helloworld.grpc.pb.h"
  28. #endif
  29. using grpc::CallbackServerContext;
  30. using grpc::Server;
  31. using grpc::ServerBuilder;
  32. using grpc::ServerUnaryReactor;
  33. using grpc::Status;
  34. using helloworld::Greeter;
  35. using helloworld::HelloReply;
  36. using helloworld::HelloRequest;
  37. // Logic and data behind the server's behavior.
  38. class GreeterServiceImpl final : public Greeter::CallbackService {
  39. ServerUnaryReactor* SayHello(CallbackServerContext* context,
  40. const HelloRequest* request,
  41. HelloReply* reply) override {
  42. std::string prefix("Hello ");
  43. reply->set_message(prefix + request->name());
  44. ServerUnaryReactor* reactor = context->DefaultReactor();
  45. reactor->Finish(Status::OK);
  46. return reactor;
  47. }
  48. };
  49. void RunServer() {
  50. std::string server_address("0.0.0.0:50051");
  51. GreeterServiceImpl service;
  52. grpc::EnableDefaultHealthCheckService(true);
  53. grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  54. ServerBuilder builder;
  55. // Listen on the given address without any authentication mechanism.
  56. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  57. // Register "service" as the instance through which we'll communicate with
  58. // clients. In this case it corresponds to an *synchronous* service.
  59. builder.RegisterService(&service);
  60. // Finally assemble the server.
  61. std::unique_ptr<Server> server(builder.BuildAndStart());
  62. std::cout << "Server listening on " << server_address << std::endl;
  63. // Wait for the server to shutdown. Note that some other thread must be
  64. // responsible for shutting down the server for this call to ever return.
  65. server->Wait();
  66. }
  67. int main(int argc, char** argv) {
  68. RunServer();
  69. return 0;
  70. }