libcopp 2.3.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
13using grpc::Server;
14using grpc::ServerBuilder;
15using grpc::ServerContext;
16using grpc::Status;
17using helloworld::Greeter;
18using helloworld::HelloReply;
19using helloworld::HelloRequest;
20
21// Logic and data behind the server's behavior.
22class 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
30void 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
51int main(int argc, char** argv) {
52 RunServer();
53
54 return 0;
55}
Status SayHello(ServerContext *context, const HelloRequest *request, HelloReply *reply) override
int main()
void RunServer()