examples/cpp/metadata/greeter_server.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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 
23 #include <grpcpp/grpcpp.h>
24 
25 #ifdef BAZEL_BUILD
26 #include "examples/protos/helloworld.grpc.pb.h"
27 #else
28 #include "helloworld.grpc.pb.h"
29 #endif
30 
31 using grpc::Server;
34 using grpc::Status;
38 
39 // Logic and data behind the server's behavior.
40 class GreeterServiceImpl final : public Greeter::Service {
42  HelloReply* reply) override {
43  std::string prefix("Hello ");
44 
45  // Get the client's initial metadata
46  std::cout << "Client metadata: " << std::endl;
47  const std::multimap<grpc::string_ref, grpc::string_ref> metadata =
48  context->client_metadata();
49  for (auto iter = metadata.begin(); iter != metadata.end(); ++iter) {
50  std::cout << "Header key: " << iter->first << ", value: ";
51  // Check for binary value
52  size_t isbin = iter->first.find("-bin");
53  if ((isbin != std::string::npos) && (isbin + 4 == iter->first.size())) {
54  std::cout << std::hex;
55  for (auto c : iter->second) {
56  std::cout << static_cast<unsigned int>(c);
57  }
58  std::cout << std::dec;
59  } else {
60  std::cout << iter->second;
61  }
62  std::cout << std::endl;
63  }
64 
65  context->AddInitialMetadata("custom-server-metadata",
66  "initial metadata value");
67  context->AddTrailingMetadata("custom-trailing-metadata",
68  "trailing metadata value");
69  reply->set_message(prefix + request->name());
70  return Status::OK;
71  }
72 };
73 
74 void RunServer() {
75  std::string server_address("0.0.0.0:50051");
77 
79  // Listen on the given address without any authentication mechanism.
81  // Register "service" as the instance through which we'll communicate with
82  // clients. In this case it corresponds to an *synchronous* service.
83  builder.RegisterService(&service);
84  // Finally assemble the server.
85  std::unique_ptr<Server> server(builder.BuildAndStart());
86  std::cout << "Server listening on " << server_address << std::endl;
87 
88  // Wait for the server to shutdown. Note that some other thread must be
89  // responsible for shutting down the server for this call to ever return.
90  server->Wait();
91 }
92 
93 int main(int argc, char** argv) {
94  RunServer();
95 
96  return 0;
97 }
grpc::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
metadata
Definition: cq_verifier.cc:48
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
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
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
GreeterServiceImpl::SayHello
Status SayHello(ServerContext *context, const HelloRequest *request, HelloReply *reply) override
Definition: examples/cpp/metadata/greeter_server.cc:41
grpc::ServerBuilder
A builder class for the creation and startup of grpc::Server instances.
Definition: grpcpp/server_builder.h:86
grpcpp.h
helloworld.Greeter
Definition: helloworld.py:32
main
int main(int argc, char **argv)
Definition: examples/cpp/metadata/greeter_server.cc:93
RunServer
void RunServer()
Definition: examples/cpp/metadata/greeter_server.cc:74
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::Status
Definition: include/grpcpp/impl/codegen/status.h:35
grpc::InsecureServerCredentials
std::shared_ptr< ServerCredentials > InsecureServerCredentials()
Definition: insecure_server_credentials.cc:52
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
iter
Definition: test_winkernel.cpp:47
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25


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