greeter_callback_server.cc
Go to the documentation of this file.
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 
19 #include <iostream>
20 #include <memory>
21 #include <string>
22 
24 #include <grpcpp/grpcpp.h>
26 
27 #ifdef BAZEL_BUILD
28 #include "examples/protos/helloworld.grpc.pb.h"
29 #else
30 #include "helloworld.grpc.pb.h"
31 #endif
32 
34 using grpc::Server;
37 using grpc::Status;
41 
42 // Logic and data behind the server's behavior.
43 class GreeterServiceImpl final : public Greeter::CallbackService {
45  const HelloRequest* request,
46  HelloReply* reply) override {
47  std::string prefix("Hello ");
48  reply->set_message(prefix + request->name());
49 
50  ServerUnaryReactor* reactor = context->DefaultReactor();
51  reactor->Finish(Status::OK);
52  return reactor;
53  }
54 };
55 
56 void RunServer() {
57  std::string server_address("0.0.0.0:50051");
59 
63  // Listen on the given address without any authentication mechanism.
65  // Register "service" as the instance through which we'll communicate with
66  // clients. In this case it corresponds to an *synchronous* service.
67  builder.RegisterService(&service);
68  // Finally assemble the server.
69  std::unique_ptr<Server> server(builder.BuildAndStart());
70  std::cout << "Server listening on " << server_address << std::endl;
71 
72  // Wait for the server to shutdown. Note that some other thread must be
73  // responsible for shutting down the server for this call to ever return.
74  server->Wait();
75 }
76 
77 int main(int argc, char** argv) {
78  RunServer();
79 
80  return 0;
81 }
main
int main(int argc, char **argv)
Definition: greeter_callback_server.cc:77
grpc::EnableDefaultHealthCheckService
void EnableDefaultHealthCheckService(bool enable)
Definition: health_check_service.cc:30
proto_server_reflection_plugin.h
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
framework.rpc.grpc_channelz.Server
Server
Definition: grpc_channelz.py:42
health_check_service_interface.h
hellostreamingworld_pb2.HelloRequest
HelloRequest
Definition: hellostreamingworld_pb2.py:102
OK
@ OK
Definition: cronet_status.h:43
server_address
std::string server_address("0.0.0.0:10000")
server
std::unique_ptr< Server > server
Definition: channelz_service_test.cc:330
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
GreeterServiceImpl::SayHello
ServerUnaryReactor * SayHello(CallbackServerContext *context, const HelloRequest *request, HelloReply *reply) override
Definition: greeter_callback_server.cc:44
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
grpc::ServerUnaryReactor::Finish
void Finish(grpc::Status s) ABSL_LOCKS_EXCLUDED(call_mu_)
Definition: impl/codegen/server_callback.h:720
grpcpp.h
helloworld.Greeter
Definition: helloworld.py:32
grpc::CallbackServerContext
Definition: grpcpp/impl/codegen/server_context.h:606
RunServer
void RunServer()
Definition: greeter_callback_server.cc:56
GreeterServiceImpl
Definition: grpc-helloworld.cc:39
hellostreamingworld_pb2.HelloReply
HelloReply
Definition: hellostreamingworld_pb2.py:109
server
Definition: examples/python/async_streaming/server.py:1
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::ServerUnaryReactor
Definition: impl/codegen/server_callback.h:699
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
grpc::reflection::InitProtoReflectionServerBuilderPlugin
void InitProtoReflectionServerBuilderPlugin()
Definition: proto_server_reflection_plugin.cc:66


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:47