1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-06 19:12:30 +01:00

Add single shot test if connected after 1 second

There can be a rare case where disconnected is never fired because it was never connected in the first place.
For this we wait 1 second and check the state. If not connected we abort.
This commit is contained in:
Elias Steurer 2020-05-16 22:50:12 +02:00
parent 2df1d751cb
commit 6900bccb7c
2 changed files with 13 additions and 27 deletions

View File

@ -29,10 +29,20 @@ void ScreenPlaySDK::init()
m_socket.setServerName("ScreenPlay");
connect(&m_socket, &QLocalSocket::connected, this, &ScreenPlaySDK::connected);
connect(&m_socket, &QLocalSocket::disconnected, this, &ScreenPlaySDK::disconnected);
connect(&m_socket, &QLocalSocket::bytesWritten, this, &ScreenPlaySDK::bytesWritten);
connect(&m_socket, &QLocalSocket::readyRead, this, &ScreenPlaySDK::readyRead);
connect(&m_socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error), this, &ScreenPlaySDK::error);
m_socket.connectToServer();
// If the wallpaper never connects it will never get the
// disconnect event. We can savely assume no connection will
// be made after 1 second timeout.
QTimer::singleShot(10000,[this](){
if(m_socket.state() != QLocalSocket::ConnectedState){
m_socket.disconnectFromServer();
emit sdkDisconnected();
}
});
}
ScreenPlaySDK::~ScreenPlaySDK()
@ -56,14 +66,9 @@ void ScreenPlaySDK::disconnected()
emit sdkDisconnected();
}
void ScreenPlaySDK::bytesWritten(qint64 bytes)
{
}
void ScreenPlaySDK::readyRead()
{
QString tmp = m_socket.readAll();
//qDebug() << "SDK MESSAGE RECEIVED: " << tmp;
QJsonParseError err;
auto doc = QJsonDocument::fromJson(QByteArray::fromStdString(tmp.toStdString()), &err);
@ -95,32 +100,14 @@ void ScreenPlaySDK::redirectMessage(QByteArray& msg)
void ScreenPlaySDK::ScreenPlaySDK::redirectMessageOutputToMainWindow(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
Q_UNUSED(type)
if (global_sdkPtr == nullptr)
return;
QByteArray localMsg = msg.toLocal8Bit();
QByteArray file = "File: " + QByteArray(context.file) + ", ";
QByteArray line = "in line " + QByteArray::number(context.line) + ", ";
//QByteArray function = "function " + QByteArray(context.function) + ", Message: ";
//localMsg = file + line + localMsg;
global_sdkPtr->redirectMessage(localMsg);
switch (type) {
case QtDebugMsg:
//localMsg = " SDK START: " /*+ QByteArray::fromStdString(global_sdkPtr->contentType().toStdString()) + " "*/ + localMsg;
break;
case QtInfoMsg:
//fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtWarningMsg:
//fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtCriticalMsg:
//fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
case QtFatalMsg:
//(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function);
break;
}
}

View File

@ -45,7 +45,6 @@ public:
public slots:
void connected();
void disconnected();
void bytesWritten(qint64 bytes);
void readyRead();
void error(QLocalSocket::LocalSocketError socketError);
void redirectMessage(QByteArray& msg);