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