github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/examples/cpp/helloworld/hello_world.cc (about) 1 #include <iostream> 2 #include <memory> 3 #include <string> 4 5 #include <grpcpp/ext/proto_server_reflection_plugin.h> 6 #include <grpcpp/grpcpp.h> 7 #include <grpcpp/health_check_service_interface.h> 8 9 #ifdef BAZEL_BUILD 10 #include "examples/proto/helloworld.grpc.pb.h" 11 #else 12 #include "helloworld.grpc.pb.h" 13 #endif 14 15 using grpc::Server; 16 using grpc::ServerBuilder; 17 using grpc::ServerContext; 18 using grpc::Status; 19 using helloworld::Greeter; 20 using helloworld::HelloReply; 21 using helloworld::HelloRequest; 22 23 // Logic and data behind the server's behavior. 24 class GreeterServiceImpl final : public Greeter::Service { 25 Status SayHello(ServerContext *context, const HelloRequest *request, 26 HelloReply *reply) override { 27 std::string prefix("Hello "); 28 reply->set_message(prefix + request->name()); 29 return Status::OK; 30 } 31 }; 32 33 void RunServer() { 34 std::string server_address("0.0.0.0:50051"); 35 GreeterServiceImpl service; 36 37 grpc::EnableDefaultHealthCheckService(true); 38 grpc::reflection::InitProtoReflectionServerBuilderPlugin(); 39 ServerBuilder builder; 40 // Listen on the given address without any authentication mechanism. 41 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); 42 // Register "service" as the instance through which we'll communicate with 43 // clients. In this case it corresponds to an *synchronous* service. 44 builder.RegisterService(&service); 45 // Finally assemble the server. 46 std::unique_ptr<Server> server(builder.BuildAndStart()); 47 std::cout << "Server listening on " << server_address << std::endl; 48 49 // Wait for the server to shutdown. Note that some other thread must be 50 // responsible for shutting down the server for this call to ever return. 51 server->Wait(); 52 } 53 54 int main(int argc, char **argv) { 55 RunServer(); 56 57 return 0; 58 }