【发布时间】:2020-10-26 09:41:51
【问题描述】:
来自官方 gRPC c++ 示例:https://github.com/grpc/grpc/tree/v1.33.1/examples/cpp/route_guide
route_guide_server.cc:
...
Status RouteChat(ServerContext* context,
ServerReaderWriter<RouteNote, RouteNote>* stream) override {
RouteNote note;
while (stream->Read(¬e)) {
std::unique_lock<std::mutex> lock(mu_);
for (const RouteNote& n : received_notes_) {
if (n.location().latitude() == note.location().latitude() &&
n.location().longitude() == note.location().longitude()) {
stream->Write(n);
}
}
received_notes_.push_back(note);
}
return Status::OK;
}
...
服务器使用 while 循环从流中读取。那么,如果流仍然打开并且服务器正在等待来自客户端的消息,它是否会切换到处理来自其他客户端的请求,因为我的服务器旨在同时与多个客户端通信?
如果不是,有什么更好的方法让服务器等待来自流的消息,同时在等待期间能够与其他客户端交互?
【问题讨论】: