diff --git a/services/lib/src/server-base-reactors/ServerBidiReactorBase.h b/services/lib/src/server-base-reactors/ServerBidiReactorBase.h index 729b1d546..b3bcf9bd4 100644 --- a/services/lib/src/server-base-reactors/ServerBidiReactorBase.h +++ b/services/lib/src/server-base-reactors/ServerBidiReactorBase.h @@ -1,172 +1,172 @@ #pragma once #include "BaseReactor.h" #include #include #include #include namespace comm { namespace network { namespace reactor { struct ServerBidiReactorStatus { grpc::Status status; bool sendLastResponse; ServerBidiReactorStatus( grpc::Status status = grpc::Status::OK, bool sendLastResponse = false) : status(status), sendLastResponse(sendLastResponse) { } }; // This is how this type of reactor works: // - repeat: // - read a request from the client // - write a response to the client // - terminate the connection template class ServerBidiReactorBase : public grpc::ServerBidiReactor, public BaseReactor { std::shared_ptr statusHolder = std::make_shared(); Request request; Response response; protected: ServerBidiReactorStatus status; bool readingAborted = false; public: ServerBidiReactorBase(); // these methods come from the BaseReactor(go there for more information) void terminate(const grpc::Status &status) override; void validate() override{}; void doneCallback() override{}; void terminateCallback() override{}; std::shared_ptr getStatusHolder() override; // these methods come from gRPC // https://github.com/grpc/grpc/blob/v1.39.x/include/grpcpp/impl/codegen/client_callback.h#L237 void OnDone() override; void OnReadDone(bool ok) override; void OnWriteDone(bool ok) override; void terminate(ServerBidiReactorStatus status); ServerBidiReactorStatus getStatus() const; void setStatus(const ServerBidiReactorStatus &status); // - argument request - request that was sent by the client and received by // the server in the current cycle // - argument response - response that will be sent to the client in the // current cycle // - returns status - if the connection is about to be // continued, nullptr should be returned. Any other returned value will // terminate the connection with a given status virtual std::unique_ptr handleRequest(Request request, Response *response) = 0; }; template ServerBidiReactorBase::ServerBidiReactorBase() { this->statusHolder->state = ReactorState::RUNNING; this->StartRead(&this->request); } template void ServerBidiReactorBase::terminate( const grpc::Status &status) { this->terminate(ServerBidiReactorStatus(status)); } template void ServerBidiReactorBase::OnDone() { this->statusHolder->state = ReactorState::DONE; this->doneCallback(); // This looks weird but apparently it is okay to do this. More information: // https://phabricator.ashoat.com/D3246#87890 delete this; } template void ServerBidiReactorBase::terminate( ServerBidiReactorStatus status) { this->setStatus(status); try { this->terminateCallback(); this->validate(); - } catch (std::runtime_error &e) { + } catch (std::exception &e) { this->setStatus(ServerBidiReactorStatus( grpc::Status(grpc::StatusCode::INTERNAL, e.what()))); } if (this->statusHolder->state != ReactorState::RUNNING) { return; } if (this->getStatus().sendLastResponse) { this->StartWriteAndFinish( &this->response, grpc::WriteOptions(), this->getStatus().status); } else { this->Finish(this->getStatus().status); } this->statusHolder->state = ReactorState::TERMINATED; } template ServerBidiReactorStatus ServerBidiReactorBase::getStatus() const { return this->status; } template void ServerBidiReactorBase::setStatus( const ServerBidiReactorStatus &status) { this->status = status; } template void ServerBidiReactorBase::OnReadDone(bool ok) { if (!ok) { this->readingAborted = true; // Ending a connection on the other side results in the `ok` flag being set // to false. It makes it impossible to detect a failure based just on the // flag. We should manually check if the data we received is valid this->terminate(ServerBidiReactorStatus(grpc::Status::OK)); return; } try { this->response = Response(); std::unique_ptr status = this->handleRequest(this->request, &this->response); if (status != nullptr) { this->terminate(*status); return; } this->StartWrite(&this->response); - } catch (std::runtime_error &e) { + } catch (std::exception &e) { this->terminate(ServerBidiReactorStatus( grpc::Status(grpc::StatusCode::INTERNAL, e.what()))); } } template void ServerBidiReactorBase::OnWriteDone(bool ok) { if (!ok) { this->terminate(ServerBidiReactorStatus( grpc::Status(grpc::StatusCode::ABORTED, "write failed"))); return; } this->StartRead(&this->request); } template std::shared_ptr ServerBidiReactorBase::getStatusHolder() { return this->statusHolder; } } // namespace reactor } // namespace network } // namespace comm