diff options
author | 2015-09-03 09:37:02 -0700 | |
---|---|---|
committer | 2015-09-03 09:37:02 -0700 | |
commit | d6599a39e48b152eeb1b55d427fbe5f22817b663 (patch) | |
tree | fdf2f1466f394008aebd93ee6e5dc18d6d9049c0 /examples/cpp/helloworld/greeter_client.cc | |
parent | aca3211b0f41aa862a424327de6d3a36e455a3c4 (diff) |
Replicate C++ docs from master to beta branch
Original PR #3074 by @dgquintas
Diffstat (limited to 'examples/cpp/helloworld/greeter_client.cc')
-rw-r--r-- | examples/cpp/helloworld/greeter_client.cc | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/examples/cpp/helloworld/greeter_client.cc b/examples/cpp/helloworld/greeter_client.cc index bfb7c12724..6cd8353a9f 100644 --- a/examples/cpp/helloworld/greeter_client.cc +++ b/examples/cpp/helloworld/greeter_client.cc @@ -35,11 +35,8 @@ #include <memory> #include <string> -#include <grpc/grpc.h> -#include <grpc++/channel.h> -#include <grpc++/client_context.h> -#include <grpc++/create_channel.h> -#include <grpc++/security/credentials.h> +#include <grpc++/grpc++.h> + #include "helloworld.grpc.pb.h" using grpc::Channel; @@ -54,17 +51,28 @@ class GreeterClient { GreeterClient(std::shared_ptr<Channel> channel) : stub_(Greeter::NewStub(channel)) {} + // Assambles the client's payload, sends it and presents the response back + // from the server. std::string SayHello(const std::string& user) { + // Data we are sending to the server. HelloRequest request; request.set_name(user); + + // Container for the data we expect from the server. HelloReply reply; + + // Context for the client. It could be used to convey extra information to + // the server and/or tweak certain RPC behaviors. ClientContext context; + // The actual RPC. Status status = stub_->SayHello(&context, request, &reply); + + // Act upon its status. if (status.ok()) { return reply.message(); } else { - return "Rpc failed"; + return "RPC failed"; } } @@ -73,6 +81,10 @@ class GreeterClient { }; int main(int argc, char** argv) { + // Instantiate the client. It requires a channel, out of which the actual RPCs + // are created. This channel models a connection to an endpoint (in this case, + // localhost at port 50051). We indicate that the channel isn't authenticated + // (use of InsecureCredentials()). GreeterClient greeter( grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials())); std::string user("world"); |