Page MenuHomePhabricator

D3465.diff
No OneTemporary

D3465.diff

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 <grpcpp/grpcpp.h>
+
+namespace comm {
+namespace network {
+namespace reactor {
+
+template <class Request, class Response>
+class ClientBidiReactorBase
+ : public grpc::ClientBidiReactor<Request, Response> {
+ std::shared_ptr<Response> 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<grpc::Status> prepareRequest(
+ Request &request,
+ std::shared_ptr<Response> previousResponse) = 0;
+ virtual void doneCallback() {
+ }
+};
+
+template <class Request, class Response>
+void ClientBidiReactorBase<Request, Response>::nextWrite() {
+ this->request = Request();
+ std::unique_ptr<grpc::Status> 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 <class Request, class Response>
+void ClientBidiReactorBase<Request, Response>::terminate(
+ const grpc::Status &status) {
+ if (this->done) {
+ return;
+ }
+ this->StartWritesDone();
+ this->status = status;
+ this->done = true;
+}
+
+template <class Request, class Response>
+bool ClientBidiReactorBase<Request, Response>::isDone() {
+ return this->done;
+}
+
+template <class Request, class Response>
+void ClientBidiReactorBase<Request, Response>::OnWriteDone(bool ok) {
+ if (this->response == nullptr) {
+ this->response = std::make_shared<Response>();
+ }
+ this->StartRead(&(*this->response));
+}
+
+template <class Request, class Response>
+void ClientBidiReactorBase<Request, Response>::OnReadDone(bool ok) {
+ if (!ok) {
+ this->terminate(grpc::Status(grpc::StatusCode::UNKNOWN, "read error"));
+ return;
+ }
+ this->nextWrite();
+}
+
+template <class Request, class Response>
+void ClientBidiReactorBase<Request, Response>::OnDone(
+ const grpc::Status &status) {
+ this->terminate(status);
+ this->doneCallback();
+}
+
+} // namespace reactor
+} // namespace network
+} // namespace comm

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 1:59 PM (2 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2571065
Default Alt Text
D3465.diff (2 KB)

Event Timeline