提示错误:pure virtual method called
由于GRPC采用的是回调方式,StartWrite 写数据时,需要为成员变量,避免数据被析构
![](https://i-blog.csdnimg.cn/blog_migrate/89e29cb450d8bafe4ccc70641a34c644.png)
class Lister : public grpc::ServerWriteReactor<SubsReply> {
public:
Lister(const SubsArgs* request) {
LOG(INFO) << "SubsArgs " << request->type();
NextWrite();
}
void OnDone() override { delete this; }
void OnWriteDone(bool ok) override {
NextWrite();
}
private:
void NextWrite() {
LOG(INFO) << "OnWriteDone";
while (1)
{
replay.set_encoding(1);
replay.set_message("hello world");
replay.set_type(1);
StartWrite(&replay);
return;
}
// Didn't write anything, all is done.
Finish(Status::OK);
}
SubsReply replay;
};
class AsynRemoteMethodsServerImp final : public pos::AsynRemoteMethods::CallbackService {
::grpc::ServerWriteReactor<SubsReply>* Subscribe(
::grpc::CallbackServerContext* context, const ::pos::SubsArgs* request) override {
return new Lister(request);
}
};