greeter_client.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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::Channel;
  28. using grpc::ClientContext;
  29. using grpc::Status;
  30. using helloworld::Greeter;
  31. using helloworld::HelloReply;
  32. using helloworld::HelloRequest;
  33. class CustomHeaderClient {
  34. public:
  35. CustomHeaderClient(std::shared_ptr<Channel> channel)
  36. : stub_(Greeter::NewStub(channel)) {}
  37. // Assembles the client's payload, sends it and presents the response back
  38. // from the server.
  39. std::string SayHello(const std::string& user) {
  40. // Data we are sending to the server.
  41. HelloRequest request;
  42. request.set_name(user);
  43. // Container for the data we expect from the server.
  44. HelloReply reply;
  45. // Context for the client. It could be used to convey extra information to
  46. // the server and/or tweak certain RPC behaviors.
  47. ClientContext context;
  48. // Setting custom metadata to be sent to the server
  49. context.AddMetadata("custom-header", "Custom Value");
  50. // Setting custom binary metadata
  51. char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', '\7'};
  52. context.AddMetadata("custom-bin", std::string(bytes, 8));
  53. // The actual RPC.
  54. Status status = stub_->SayHello(&context, request, &reply);
  55. // Act upon its status.
  56. if (status.ok()) {
  57. std::cout << "Client received initial metadata from server: "
  58. << context.GetServerInitialMetadata()
  59. .find("custom-server-metadata")
  60. ->second
  61. << std::endl;
  62. std::cout << "Client received trailing metadata from server: "
  63. << context.GetServerTrailingMetadata()
  64. .find("custom-trailing-metadata")
  65. ->second
  66. << std::endl;
  67. return reply.message();
  68. } else {
  69. std::cout << status.error_code() << ": " << status.error_message()
  70. << std::endl;
  71. return "RPC failed";
  72. }
  73. }
  74. private:
  75. std::unique_ptr<Greeter::Stub> stub_;
  76. };
  77. int main(int argc, char** argv) {
  78. // Instantiate the client. It requires a channel, out of which the actual RPCs
  79. // are created. This channel models a connection to an endpoint (in this case,
  80. // localhost at port 50051). We indicate that the channel isn't authenticated
  81. // (use of InsecureChannelCredentials()).
  82. CustomHeaderClient greeter(grpc::CreateChannel(
  83. "localhost:50051", grpc::InsecureChannelCredentials()));
  84. std::string user("world");
  85. std::string reply = greeter.SayHello(user);
  86. std::cout << "Client received message: " << reply << std::endl;
  87. return 0;
  88. }