RpcManager: Try to reconnect to service on error, Part 2

This commit is contained in:
Nodir Temirkhodjaev 2022-05-20 15:41:42 +03:00
parent ec3a58a301
commit 12d4bb0116
3 changed files with 27 additions and 12 deletions

View File

@ -114,28 +114,39 @@ void ControlWorker::setupForAsync()
connect(socket(), &QLocalSocket::readyRead, this, &ControlWorker::processRequest);
}
void ControlWorker::setServerName(const QString &name)
void ControlWorker::setServerName(const QString &v)
{
socket()->setServerName(name);
m_serverName = v;
}
bool ControlWorker::connectToServer()
{
socket()->connectToServer();
socket()->connectToServer(serverName());
if (!socket()->waitForConnected(150)) {
if (!socket()->waitForConnected(100)) {
qCWarning(LC) << "Connection error:" << socket()->state() << socket()->errorString();
return false;
}
if (socket()->state() != QLocalSocket::ConnectedState) {
qCWarning(LC) << "Connection state error:" << socket()->state();
return false;
}
return true;
}
bool ControlWorker::reconnectToServer()
{
close();
if (isTryReconnect() && isConnected())
return true;
int reconnectCount = 2;
do {
if (connectToServer())
return true;
} while (--reconnectCount > 0);
return false;
}
void ControlWorker::close()
{
socket()->abort();
@ -144,7 +155,7 @@ void ControlWorker::close()
void ControlWorker::onDisconnected()
{
if (isTryReconnect() && connectToServer())
if (isTryReconnect() && reconnectToServer())
return;
emit disconnected();

View File

@ -26,6 +26,9 @@ public:
quint32 id() const { return m_id; }
QString serverName() const { return m_serverName; }
void setServerName(const QString &v);
QLocalSocket *socket() const { return m_socket; }
bool isConnected() const;
@ -33,8 +36,8 @@ public:
void setupForAsync();
void setServerName(const QString &name);
bool connectToServer();
bool reconnectToServer();
static QByteArray buildCommandData(Control::Command command, const QVariantList &args = {});
bool sendCommandData(const QByteArray &commandData);
@ -99,6 +102,7 @@ private:
RequestHeader m_requestHeader;
QByteArray m_requestBuffer;
QString m_serverName;
QLocalSocket *m_socket = nullptr;
};

View File

@ -401,7 +401,7 @@ void RpcManager::sendResult(ControlWorker *w, bool ok, const QVariantList &args)
bool RpcManager::invokeOnServer(Control::Command cmd, const QVariantList &args)
{
if (!client()->isConnected()) {
if (!client()->isConnected() && !client()->reconnectToServer()) {
IoC<WindowManager>()->showErrorBox(tr("Service isn't available."));
return false;
}