From 6900bccb7c431e7b9e8d187f974b02b7c2838963 Mon Sep 17 00:00:00 2001 From: Elias Steurer Date: Sat, 16 May 2020 22:50:12 +0200 Subject: [PATCH] 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. --- ScreenPlaySDK/screenplaysdk.cpp | 39 +++++++++++---------------------- ScreenPlaySDK/screenplaysdk.h | 1 - 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/ScreenPlaySDK/screenplaysdk.cpp b/ScreenPlaySDK/screenplaysdk.cpp index 09901c5f..c034d4cb 100644 --- a/ScreenPlaySDK/screenplaysdk.cpp +++ b/ScreenPlaySDK/screenplaysdk.cpp @@ -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::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; - } } diff --git a/ScreenPlaySDK/screenplaysdk.h b/ScreenPlaySDK/screenplaysdk.h index 315b3d0c..2b4da6c1 100644 --- a/ScreenPlaySDK/screenplaysdk.h +++ b/ScreenPlaySDK/screenplaysdk.h @@ -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);