greeter_server.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include <iostream>
  19. #include <memory>
  20. #include <string>
  21. #include <grpcpp/grpcpp.h>
  22. #ifdef BAZEL_BUILD
  23. #include "examples/protos/helloworld.grpc.pb.h"
  24. #else
  25. #include "helloworld.grpc.pb.h"
  26. #endif
  27. using grpc::Server;
  28. using grpc::ServerBuilder;
  29. using grpc::ServerContext;
  30. using grpc::Status;
  31. using helloworld::Greeter;
  32. using helloworld::HelloReply;
  33. using helloworld::HelloRequest;
  34. // Logic and data behind the server's behavior.
  35. class GreeterServiceImpl final : public Greeter::Service {
  36. Status SayHello(ServerContext* context, const HelloRequest* request,
  37. HelloReply* reply) override {
  38. std::string prefix("Hello ");
  39. // Get the client's initial metadata
  40. std::cout << "Client metadata: " << std::endl;
  41. const std::multimap<grpc::string_ref, grpc::string_ref> metadata =
  42. context->client_metadata();
  43. for (auto iter = metadata.begin(); iter != metadata.end(); ++iter) {
  44. std::cout << "Header key: " << iter->first << ", value: ";
  45. // Check for binary value
  46. size_t isbin = iter->first.find("-bin");
  47. if ((isbin != std::string::npos) && (isbin + 4 == iter->first.size())) {
  48. std::cout << std::hex;
  49. for (auto c : iter->second) {
  50. std::cout << static_cast<unsigned int>(c);
  51. }
  52. std::cout << std::dec;
  53. } else {
  54. std::cout << iter->second;
  55. }
  56. std::cout << std::endl;
  57. }
  58. context->AddInitialMetadata("custom-server-metadata",
  59. "initial metadata value");
  60. context->AddTrailingMetadata("custom-trailing-metadata",
  61. "trailing metadata value");
  62. reply->set_message(prefix + request->name());
  63. return Status::OK;
  64. }
  65. };
  66. void RunServer() {
  67. std::string server_address("0.0.0.0:50051");
  68. GreeterServiceImpl service;
  69. ServerBuilder builder;
  70. // Listen on the given address without any authentication mechanism.
  71. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  72. // Register "service" as the instance through which we'll communicate with
  73. // clients. In this case it corresponds to an *synchronous* service.
  74. builder.RegisterService(&service);
  75. // Finally assemble the server.
  76. std::unique_ptr<Server> server(builder.BuildAndStart());
  77. std::cout << "Server listening on " << server_address << std::endl;
  78. // Wait for the server to shutdown. Note that some other thread must be
  79. // responsible for shutting down the server for this call to ever return.
  80. server->Wait();
  81. }
  82. int main(int argc, char** argv) {
  83. RunServer();
  84. return 0;
  85. }