UI: Try to reconnect to service by timer

This commit is contained in:
Nodir Temirkhodjaev 2023-02-05 13:05:16 +03:00
parent 8d888159d6
commit 8ee654fc41
2 changed files with 53 additions and 7 deletions

View File

@ -3,6 +3,7 @@
#include <QDataStream>
#include <QLocalSocket>
#include <QLoggingCategory>
#include <QTimer>
namespace {
@ -86,6 +87,7 @@ ControlWorker::ControlWorker(QLocalSocket *socket, QObject *parent) :
m_isServiceClient(false),
m_isClientValidated(false),
m_isTryReconnect(false),
m_isReconnecting(false),
m_id(nextWorkerId()),
m_socket(socket)
{
@ -110,7 +112,7 @@ void ControlWorker::setupForAsync()
qCWarning(LC) << "Client error:" << id() << socketError << errorString();
close();
});
connect(socket(), &QLocalSocket::connected, this, &ControlWorker::connected);
connect(socket(), &QLocalSocket::connected, this, &ControlWorker::onConnected);
connect(socket(), &QLocalSocket::disconnected, this, &ControlWorker::onDisconnected);
connect(socket(), &QLocalSocket::readyRead, this, &ControlWorker::processRequest);
}
@ -134,18 +136,23 @@ bool ControlWorker::connectToServer()
bool ControlWorker::reconnectToServer()
{
if (m_isReconnecting)
return false;
m_isReconnecting = true;
close();
if (isTryReconnect() && isConnected())
return true;
bool connectedToServer;
int reconnectCount = 2;
do {
if (connectToServer())
return true;
} while (--reconnectCount > 0);
connectedToServer = connectToServer();
} while (!connectedToServer && --reconnectCount > 0);
return false;
m_isReconnecting = false;
return connectedToServer;
}
void ControlWorker::close()
@ -154,12 +161,43 @@ void ControlWorker::close()
socket()->close();
}
void ControlWorker::onConnected()
{
stopReconnectTimer();
emit connected();
}
void ControlWorker::onDisconnected()
{
if (isTryReconnect() && reconnectToServer())
return;
emit disconnected();
startReconnectTimer();
}
void ControlWorker::startReconnectTimer()
{
if (!isTryReconnect())
return;
if (!m_reconnectTimer) {
m_reconnectTimer = new QTimer(this);
m_reconnectTimer->setInterval(1000);
connect(m_reconnectTimer, &QTimer::timeout, this, &ControlWorker::reconnectToServer);
}
m_reconnectTimer->start();
}
void ControlWorker::stopReconnectTimer()
{
if (m_reconnectTimer) {
m_reconnectTimer->stop();
}
}
QByteArray ControlWorker::buildCommandData(Control::Command command, const QVariantList &args)

View File

@ -7,6 +7,7 @@
#include "control.h"
QT_FORWARD_DECLARE_CLASS(QLocalSocket)
QT_FORWARD_DECLARE_CLASS(QTimer)
class ControlWorker : public QObject
{
@ -58,7 +59,12 @@ public slots:
void close();
private slots:
void onConnected();
void onDisconnected();
void startReconnectTimer();
void stopReconnectTimer();
void processRequest();
private:
@ -97,6 +103,7 @@ private:
bool m_isServiceClient : 1;
bool m_isClientValidated : 1;
bool m_isTryReconnect : 1;
bool m_isReconnecting : 1;
const quint32 m_id = 0;
@ -105,6 +112,7 @@ private:
QString m_serverName;
QLocalSocket *m_socket = nullptr;
QTimer *m_reconnectTimer = nullptr;
};
#endif // CONTROLWORKER_H