grpc async stream in C++ client

2,581 views
Skip to first unread message

Julien

unread,
Mar 16, 2017, 1:47:51 PM3/16/17
to grpc.io
Hello,

I am trying to use the async stream in a C++ client.

I made some trials by modifying the route_guide example, with the ListFeatures rpc method from the RouteGuide service.

In the provided client, the ListFeatures() method is called with the sync interface. So I tried to call it with the async interface.

I have thus changed the ListFeatures() function from the route_guide_client.cc file as follow:

  void ListFeatures() {
    routeguide
::Rectangle rect;
   
Feature feature;
   
ClientContext context;

    rect
.mutable_lo()->set_latitude(400000000);
    rect
.mutable_lo()->set_longitude(-750000000);
    rect
.mutable_hi()->set_latitude(420000000);
    rect
.mutable_hi()->set_longitude(-730000000);
    std
::cout << "Looking for features between 40, -75 and 42, -73"
             
<< std::endl;

   
CompletionQueue cq;
    std
::unique_ptr<ClientAsyncReader<Feature> > reader(stub_->AsyncListFeatures(&context, rect,&cq,(void*)1));
   
void* got_tag;
   
bool ok;
   
while (1) {
      ok
= false;
     
bool ret = cq.Next(&got_tag, &ok);
     
if (!ret || !ok) {
       
break;
     
}
      reader
->Read(&feature,got_tag);
     
if (got_tag == (void*)1) {
        std
::cout << "Found feature called "
               
<< feature.name() << " at "
               
<< feature.location().latitude()/kCoordFactor_ << ", "
               
<< feature.location().longitude()/kCoordFactor_ << std::endl;
     
}
   
}
 
}

Unfortunately, this does not exactly work properly.
Strangely, the client receives an additionnal Feature objet at the beginning of the stream. Here is the relevant part of the output of the client:
-------------- ListFeatures --------------
Looking for features between 40, -75 and 42, -73
Found feature called  at 0, 0          <---------- empty feature
Found feature called PatriotsPath,Mendham,NJ07945,USA at 40.7838, -74.6144
Found feature called 101NewJersey10,Whippany,NJ07981,USA at 40.8123, -74.3999
Found feature called U.S.6,Shohola,PA18458,USA at 41.3628, -74.9016
...

This empty feature is not received with the original sync client.

So what am I doing wrong in my above code?

Note: I use grpc 1.1.4 and protobuf 3.2.0.

Thank you in advance for your help.

Julien

Vijay Pai

unread,
Mar 16, 2017, 1:49:59 PM3/16/17
to Julien, grpc.io
Hi there,
The Read function just initiates the read; it doesn't complete until a later Next gets called and returns that tag. You can't use the result in feature until the later Next.


--
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+u...@googlegroups.com.
To post to this group, send email to grp...@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/3c73c2fa-a8d7-49a7-9e1a-f9187aa8b465%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

julien....@laposte.net

unread,
Mar 16, 2017, 2:12:37 PM3/16/17
to grpc.io, julien....@laposte.net
Hi Vijay,

Ok, so I changed the code in the following way:
    CompletionQueue cq;
    std
::unique_ptr<ClientAsyncReader<Feature> > reader(stub_->AsyncListFeatures(&context, rect,&cq,(void*)1));
   
void* got_tag;
   
bool ok;

   
bool ret = cq.Next(&got_tag, &ok);

   
if (ret && ok && got_tag == (void*)1) {
     
while (1) {
        reader
->Read(&feature,(void*)1);
        ok
= false;
        ret
= cq.Next(&got_tag, &ok);
       
if (!ret || !ok || got_tag != (void*)1) {
         
break;

       
}
        std
::cout << "Found feature called "
               
<< feature.name() << " at "
               
<< feature.location().latitude()/kCoordFactor_ << ", "
               
<< feature.location().longitude()/kCoordFactor_ << std::endl;
     
}
   
}
And now it seems to work properly.
Could you please confirm that it is correct now?

Thanks.

Julien


Reply all
Reply to author
Forward
0 new messages