Hello folks,

I am new to GRPC, and I feel the documentation lacks info on using an async 
streaming client and server.  I could get the async streaming server work 
with the help of a thread in this group.

When it comes to an async stgreaming client, I am unable to get it working. 
 The server crashes saying assertion failed.  Not sure if the client is 
sending the right way.  Here is my async streaming client code.  Please 
advise.  Not sure if I am handling the tags and completion queue properly.  

class SampleClient {

 public:

  // Assembles the client's payload, sends it and presents the response back

  // from the server.

    // Data we are sending to the server.


    std::unique_ptr<SampleService::Stub> stub_;

   // grpc::ClientAsyncReaderWriter<Sample::SampleMessage, 
Sample::SampleResponse > stream;


     void CreateCh() {

         std::shared_ptr<Channel> channel = CreateChannel(

         “0.0.0.0:50051" , grpc::InsecureChannelCredentials());

         stub_ = SampleService::NewStub(channel);

     }


    std::string SendSample() {

        SampleMessage request;

        Sample::AppDeepAnalysisThreshold* appDeepAnalysis;

        request.set_time("15:00");

        request.set_message_description("RISE");



    // Container for the data we expect from the server.

        SampleResponse 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 producer-consumer queue we use to communicate asynchronously 
with the

    // gRPC runtime.

        CompletionQueue cq;


    // Storage for the status of the RPC upon completion.

 


    Status status;


    void* got_tag_read;


    // stub_->AsyncSendSample() performs the RPC call, returning an 
instance we

    // store in "rpc". Because we are using the asynchronous API, we need to

    // hold on to the "rpc" instance in order to get updates on the ongoing 
RPC.


    std::unique_ptr<grpc::ClientAsyncReaderWriter<Sample::SampleMessage, 
Sample::SampleResponse>> stream(

        stub_->AsyncSendSample(&context, &cq, got_tag_read));


        void* got_tag_write;

        bool ok = false;

        stream->Write(request, got_tag_write);

        std::string req = request.message_description();

        std::cout << "The req sent is " << req << std::endl;

        cq.Next(&got_tag_write, &ok);

            if (ok && got_tag_write == (void*)1) {

                printf("\n CLient sent the req");

                stream->Read(&reply, got_tag_read);

            }


        cq.Next(&got_tag_read, &ok);

            if (ok && got_tag_read == (void*)1) {

                stream->Finish(&status, got_tag_read);

                GPR_ASSERT(ok);

            } 

        if (status.ok()) {

            return reply.message_description();

        } else {

              return "RPC failed";

        } 

      }          

      




-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to grpc-io+unsubscr...@googlegroups.com.
To post to this group, send email to grpc-io@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/27d89af1-d0a8-47fc-aca7-28dcf4f44d47%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to