diff --git a/services/backup/docker-server/contents/server/src/Reactors/client/base-reactors/ClientBidiReactorBase.h b/services/backup/docker-server/contents/server/src/Reactors/client/base-reactors/ClientBidiReactorBase.h new file mode 100644 --- /dev/null +++ b/services/backup/docker-server/contents/server/src/Reactors/client/base-reactors/ClientBidiReactorBase.h @@ -0,0 +1,93 @@ +#include + +namespace comm { +namespace network { +namespace reactor { + +template +class ClientBidiReactorBase + : public grpc::ClientBidiReactor { + std::shared_ptr response = nullptr; + bool done = false; + bool initialized = 0; + +protected: + Request request; + grpc::Status status; + +public: + grpc::ClientContext context; + + void nextWrite(); + void terminate(const grpc::Status &status); + bool isDone(); + void OnWriteDone(bool ok) override; + void OnReadDone(bool ok) override; + void OnDone(const grpc::Status &status) override; + + virtual std::unique_ptr prepareRequest( + Request &request, + std::shared_ptr previousResponse) = 0; + virtual void doneCallback() { + } +}; + +template +void ClientBidiReactorBase::nextWrite() { + this->request = Request(); + std::unique_ptr status = + this->prepareRequest(this->request, this->response); + if (status != nullptr) { + this->terminate(*status); + return; + } + this->StartWrite(&this->request); + if (!this->initialized) { + this->StartCall(); + this->initialized = true; + } +} + +template +void ClientBidiReactorBase::terminate( + const grpc::Status &status) { + if (this->done) { + return; + } + this->StartWritesDone(); + this->status = status; + this->done = true; +} + +template +bool ClientBidiReactorBase::isDone() { + return this->done; +} + +template +void ClientBidiReactorBase::OnWriteDone(bool ok) { + if (this->response == nullptr) { + this->response = std::make_shared(); + } + this->StartRead(&(*this->response)); +} + +template +void ClientBidiReactorBase::OnReadDone(bool ok) { + if (!ok) { + this->terminate(grpc::Status(grpc::StatusCode::UNKNOWN, "read error")); + return; + } + this->nextWrite(); +} + +template +void ClientBidiReactorBase::OnDone( + const grpc::Status &status) { + this->terminate(status); + this->doneCallback(); +} + +} // namespace reactor +} // namespace network +} // namespace comm