Hi,
You are incorrectly using the async streaming API on the server side.
In the following code, stream_.Read(), stream_.Write() and stream_.Finish()
are three *Async* calls -and would return immediately. You should have done
a cq_.Next() after each call to make sure the async operations actually
completed.
---
std::string prefix("Hello ");
stream_.Read(&request_, this);
//*** SREE: You should wait for cq_.Next() to return the tag (i.e
'this') before proceeding ***
// It is not safe to proceed without that
std::cout << "Greeter server received: " << request_.message() <<
std::endl;
reply_.set_message(prefix + request_.message());
std::cout << "Greeter server replied: " << reply_.message() <<
std::endl;
stream_.Write(reply_, this);
//*** SREE: You should wait for cq_.Next() to return the tag (i.e
'this') before proceeding ***
// And we are done! Let the gRPC runtime know we've finished, using
the
// memory address of this instance as the uniquely identifying tag
for
// the event.
status_ = FINISH;
stream_.Finish(Status::OK, this);
---
The correct way to do it is:
1) Expand your state-machine in CallData. i.e change enum CallStatus {
CREATE, PROCESS, FINISH };
to enum CallStatus { CREATE, PROCESS, READ_CALLED, WRITE_CALLED, FINISH };
2) Change your Proceed() function to something like below:
void Proceed() {
switch(status_) {
case CREATE: {
service_->RequestSayHello(&ctx_, &stream_, cq_, cq_, this);
status_ = PROCESS;
break;
}
case PROCESS: {
new CallData(service_, cq_);
std::string prefix("Hello ");
stream_.Read(&request_, this);
status_ = READ_CALLED;
break;
}
case READ_CALLED: {
std::cout << "Greeter server received: " << request_.message() <<
std::endl;
reply_.set_message(prefix + request_.message());
std::cout << "Greeter server replied: " << reply_.message() <<
std::endl;
stream_.Write(reply_, this);
status_ = WRITE_CALLED;
break;
}
case WRITE_CALLED: {
stream_.Finish(Status::OK, this);
status_ = FINISH;
break;
}
case FINISH: {
delete this;
}
}
Hope this helps,
thanks,
Sree
On Tue, Jan 24, 2017 at 8:44 PM, <[email protected]> wrote:
> Mt client side uses sync API, as shown below.
>
> class GreeterClient {
> public:
> 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.
> HelloMessage request;
> request.set_message(user);
>
> // Container for the data we expect from the server.
> HelloMessage reply;
>
> // Context for the client. It could be used to convey extra
> information to
> // the server and/or tweak certain RPC behaviors.
> ClientContext context;
> std::shared_ptr<ClientReaderWriter<HelloMessage, HelloMessage> >
> stream(stub_->SayHello(&context));
>
> // The actual RPC.
> stream->Write(request);
> stream->Read(&reply);
> return reply.message();
> }
>
> private:
> std::unique_ptr<Greeter::Stub> stub_;
> };
>
> 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 InsecureChannelCredentials()).
> GreeterClient greeter(grpc::CreateChannel(
> "localhost:50051", grpc::InsecureChannelCredentials()));
> std::string user("world");
> std::string reply = greeter.SayHello(user);
> std::cout << "Greeter received: " << reply << std::endl;
>
> return 0;
> }
>
>
> Vijay Pai於 2017年1月24日星期二 UTC-8下午8時25分33秒寫道:
>>
>> What does your Client code look like?
>>
>> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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/808939a7-9335-4194-82af-4e92fadf04e9%40googlegroups.com
> <https://groups.google.com/d/msgid/grpc-io/808939a7-9335-4194-82af-4e92fadf04e9%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To post to this group, send email to [email protected].
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/CALRi9Qd67DyvW-0rnTfao3ZsFJuZ%2B5j70A6uQEQAHOwBi_Wr3w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.