C ++ / gRPC-IsCancelled无法正常工作

时间:2020-10-16 13:30:25

标签: c++ grpc

我在C ++中使用gRPC同步api。

这是我在服务器端检查客户端是否已停止流的方式。

grpc::Status AuthServer::ConnectServiceImpl::HearthBeat(grpc::ServerContext *context,
grpc::ServerReaderWriter<Pulse, Pulse> *stream) {
Pulse note;
if(ctx_.IsCancelled()){
std::cout << "DISCONNECT" << std::endl;
    }
while (stream->Read(&note)) {
Pulse reply;
reply.set_rate(note.rate()+1);
std::cout << "RECEIVED: " << note.rate() << std::endl;
stream->Write(reply);
}

return grpc::Status::OK;
}

这是bidi流,由于杀死了客户端应用程序而在客户端被强制停止,但仍未显示“ DISCONNECT”消息。

那是为什么,我使用IsCancelled()的方式不正确?

1 个答案:

答案 0 :(得分:0)

我想我已经在GRPC/C++ - How to detect client disconnected in Async Server中回答了这个问题。

您的代码似乎正在IsCancelled()上检查ctx_。我不确定该对象是什么,但是您要检查的上下文是作为context传递到请求处理程序方法中的上下文。