dimanche 3 octobre 2021

grpc client usage async call requires locking and waiting on std::condition_variable

I am using grpc with protobuf to implement my RPC calls. My proto file is given below.

syntax = "proto3";

package helloworld;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;  
}
message HelloReply {
  bool result = 1
  string message = 2;
}

I have the below code, where I am using the client (using functions generated by grpc)

//Using the client

::grpc::Status result_status;
helloworld::HelloReply result_reply;
helloworld::HelloRequest req_msg;
req_msg.set_name("REQUEST");

auto clientCb = [&](::grpc::Status const & status, helloworld::HelloReply const & reply)
{
    {          
        result_status = status;
        result_reply = reply;
    }            
}

auto result = rpc_client_helper::helloworld::Greeter::SayHello(HOSTNAME,req_msg,clientCb,2000));

if(result_reply.result == true && result_reply.message == "REPLY")
{
   //do something
}

My question is as follows

do I need to have a contition_variable, and wait on it before I check result_reply.result and result_reply.message.

If the callback doesn't immideately return the value of result_reply.result and result_reply.message won't be valid.

Also I am assuming that if I don't hear from the server in 2000ms (2 seconds), the call will return with the appropriate value set to result_status. Is this correct ?

Aucun commentaire:

Enregistrer un commentaire