server.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2021 the gRPC authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <iostream>
  15. #include <memory>
  16. #include <string>
  17. #include "examples/protos/helloworld.grpc.pb.h"
  18. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  19. #include <grpcpp/grpcpp.h>
  20. #include <grpcpp/health_check_service_interface.h>
  21. using grpc::Server;
  22. using grpc::ServerBuilder;
  23. using grpc::ServerContext;
  24. using grpc::Status;
  25. using helloworld::Greeter;
  26. using helloworld::HelloReply;
  27. using helloworld::HelloRequest;
  28. // Logic and data behind the server's behavior.
  29. class GreeterServiceImpl final : public Greeter::Service {
  30. Status SayHello(ServerContext* context, const HelloRequest* request,
  31. HelloReply* reply) override {
  32. reply->set_message(request->name());
  33. std::cout << "Echoing: " << request->name() << std::endl;
  34. return Status::OK;
  35. }
  36. };
  37. void RunServer() {
  38. std::string server_address("unix-abstract:grpc%00abstract");
  39. GreeterServiceImpl service;
  40. grpc::EnableDefaultHealthCheckService(true);
  41. grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  42. ServerBuilder builder;
  43. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  44. builder.RegisterService(&service);
  45. std::unique_ptr<Server> server(builder.BuildAndStart());
  46. std::cout << "Server listening on " << server_address << " ... ";
  47. server->Wait();
  48. }
  49. int main(int argc, char** argv) {
  50. RunServer();
  51. return 0;
  52. }