diff --git a/src/ui/control/control.h b/src/ui/control/control.h index 855e8df3..3ba846af 100644 --- a/src/ui/control/control.h +++ b/src/ui/control/control.h @@ -9,6 +9,8 @@ enum Command : qint8 { CommandNone = 0, Conf, Prog, + Rpc_Result_Ok, + Rpc_Result_Error, Rpc_RpcManager_initClient, Rpc_AppInfoManager_lookupAppInfo, Rpc_AppInfoManager_checkLookupFinished, diff --git a/src/ui/control/controlworker.cpp b/src/ui/control/controlworker.cpp index b5bb3220..ebf35abb 100644 --- a/src/ui/control/controlworker.cpp +++ b/src/ui/control/controlworker.cpp @@ -99,6 +99,8 @@ bool ControlWorker::sendCommand(Control::Command command, const QVariantList &ar socket()->write(buffer); } + socket()->flush(); + return true; } @@ -107,6 +109,11 @@ bool ControlWorker::waitForSent(int msecs) const return socket()->waitForBytesWritten(msecs); } +bool ControlWorker::waitForRead(int msecs) const +{ + return socket()->waitForReadyRead(msecs); +} + void ControlWorker::processRequest() { if (!readRequest()) { diff --git a/src/ui/control/controlworker.h b/src/ui/control/controlworker.h index 04ac91e8..60e2151d 100644 --- a/src/ui/control/controlworker.h +++ b/src/ui/control/controlworker.h @@ -25,9 +25,10 @@ public: void setupForAsync(); - bool sendCommand(Control::Command command, const QVariantList &args); + bool sendCommand(Control::Command command, const QVariantList &args = {}); bool waitForSent(int msecs = 1000) const; + bool waitForRead(int msecs = 1000) const; static QVariantList buildArgs(const QStringList &list); diff --git a/src/ui/rpc/confmanagerrpc.cpp b/src/ui/rpc/confmanagerrpc.cpp index f1f49bec..2dd53bd5 100644 --- a/src/ui/rpc/confmanagerrpc.cpp +++ b/src/ui/rpc/confmanagerrpc.cpp @@ -33,8 +33,17 @@ void ConfManagerRpc::setupAppEndTimer() { } bool ConfManagerRpc::saveToDbIni(FirewallConf &newConf, bool onlyFlags) { rpcManager()->invokeOnServer(Control::Rpc_ConfManager_save, - { newConf.toVariant(onlyFlags), onlyFlags, confVersion() }); - // TODO: get result - // showErrorMessage("Save Conf: Service error."); + { newConf.toVariant(onlyFlags), confVersion(), onlyFlags }); + + if (!rpcManager()->waitResult()) { + showErrorMessage("Save Conf: Service isn't responding."); + return false; + } + + if (rpcManager()->resultCommand() != Control::Rpc_Result_Ok) { + showErrorMessage("Save Conf: Service error."); + return false; + } + return true; } diff --git a/src/ui/rpc/rpcmanager.cpp b/src/ui/rpc/rpcmanager.cpp index 297c2ed8..05e1eb3a 100644 --- a/src/ui/rpc/rpcmanager.cpp +++ b/src/ui/rpc/rpcmanager.cpp @@ -117,6 +117,23 @@ void RpcManager::invokeOnServer(Control::Command cmd, const QVariantList &args) client()->sendCommand(cmd, args); } +bool RpcManager::waitResult() +{ + m_resultCommand = Control::CommandNone; + + do { + if (!client()->waitForRead()) + return false; + } while (m_resultCommand == Control::CommandNone); + + return true; +} + +void RpcManager::sendResult(ControlWorker *w, bool ok) +{ + w->sendCommand(ok ? Control::Rpc_Result_Ok : Control::Rpc_Result_Error); +} + void RpcManager::invokeOnClients(Control::Command cmd, const QVariantList &args) { const auto clients = controlManager()->clients(); @@ -149,6 +166,10 @@ bool RpcManager::processCommandRpc( ControlWorker *w, Control::Command cmd, const QVariantList &args, QString &errorMessage) { switch (cmd) { + case Control::Rpc_Result_Ok: + case Control::Rpc_Result_Error: + m_resultCommand = cmd; + return true; case Control::Rpc_RpcManager_initClient: initClientOnServer(w); return true; @@ -166,7 +187,7 @@ bool RpcManager::processCommandRpc( case Control::Rpc_ConfManager_save: { const bool ok = confManager()->saveVariant( args.value(0), args.value(1).toInt(), args.value(2).toBool()); - w->sendCommand(Control::Rpc_ConfManager_saveResult, { ok }); + sendResult(w, ok); return true; } case Control::Rpc_DriverManager_updateState: diff --git a/src/ui/rpc/rpcmanager.h b/src/ui/rpc/rpcmanager.h index 77678f84..938d4964 100644 --- a/src/ui/rpc/rpcmanager.h +++ b/src/ui/rpc/rpcmanager.h @@ -24,6 +24,8 @@ class RpcManager : public QObject public: explicit RpcManager(FortManager *fortManager, QObject *parent = nullptr); + Control::Command resultCommand() const { return m_resultCommand; } + FortManager *fortManager() const { return m_fortManager; } FortSettings *settings() const; ControlManager *controlManager() const; @@ -39,6 +41,9 @@ public: void invokeOnServer(Control::Command cmd, const QVariantList &args = {}); + bool waitResult(); + void sendResult(ControlWorker *w, bool ok); + bool processCommandRpc(ControlWorker *w, Control::Command cmd, const QVariantList &args, QString &errorMessage); @@ -59,6 +64,8 @@ private: QVariantList driverManager_updateState_args() const; private: + Control::Command m_resultCommand = Control::CommandNone; + FortManager *m_fortManager = nullptr; ControlWorker *m_client = nullptr;