1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-09-14 22:42:34 +02:00

Refactor socket connection

No longer send quit command
Make sure to not set any values via the new m_isExiting
when not connection aka when closing the wallpaper
This commit is contained in:
Elias Steurer 2021-12-28 16:16:36 +01:00
parent 33841a93e2
commit 513525e7d9
6 changed files with 42 additions and 39 deletions

View File

@ -382,7 +382,7 @@ ScreenPlayWallpaper* ScreenPlayManager::getWallpaperByAppID(const QString& appID
*/
void ScreenPlayManager::newConnection()
{
qInfo() << "[1/3] SDKConnection incomming";
qInfo() << "[1/4] SDKConnection incomming";
auto connection = std::make_unique<SDKConnection>(m_server->nextPendingConnection());
QObject::connect(connection.get(), &SDKConnection::requestRaise, this, &ScreenPlayManager::requestRaise);
@ -409,7 +409,7 @@ void ScreenPlayManager::newConnection()
for (int i = 0; i < m_screenPlayWallpapers.size(); ++i) {
if (m_screenPlayWallpapers.at(i)->appID() == matchingConnection->appID()) {
qInfo() << "Matching Wallpaper found!";
qInfo() << "[3/4] Matching Wallpaper found!";
m_screenPlayWallpapers.at(i)->setSDKConnection(std::move(matchingConnection));
return;
}
@ -417,7 +417,7 @@ void ScreenPlayManager::newConnection()
for (int i = 0; i < m_screenPlayWidgets.size(); ++i) {
if (m_screenPlayWidgets.at(i)->appID() == matchingConnection->appID()) {
qInfo() << "Matching Widget found!";
qInfo() << "[3/4] Matching Widget found!";
m_screenPlayWidgets.at(i)->setSDKConnection(std::move(matchingConnection));
return;
}
@ -444,7 +444,7 @@ bool ScreenPlayManager::removeWallpaper(const QString& appID)
if (wallpaper->appID() != appID) {
return false;
}
wallpaper->messageQuit();
qInfo() << "Remove wallpaper " << wallpaper->file() << "at monitor " << wallpaper->screenNumber();
@ -452,6 +452,8 @@ bool ScreenPlayManager::removeWallpaper(const QString& appID)
// for shared_ptr to release the object.
m_monitorListModel->setWallpaperMonitor({}, wallpaper->screenNumber());
wallpaper->close();
decreaseActiveWallpaperCounter();
return true;
@ -481,7 +483,7 @@ bool ScreenPlayManager::removeWidget(const QString& appID)
return false;
}
widget->messageQuit();
widget->close();
qInfo() << "Remove widget " << appID;

View File

@ -137,7 +137,7 @@ QJsonObject ScreenPlayWallpaper::getActiveSettingsJson()
/*!
\brief Sends command quit to the wallpaper.
*/
void ScreenPlayWallpaper::messageQuit()
void ScreenPlayWallpaper::close()
{
// When the wallpaper never connected, this is invalid
if (!m_connection) {
@ -145,9 +145,10 @@ void ScreenPlayWallpaper::messageQuit()
return;
}
QJsonObject obj;
obj.insert("command", "quit");
m_connection->sendMessage(QJsonDocument(obj).toJson(QJsonDocument::Compact));
if(m_connection->close()){
m_isExiting = true;
}
}
/*!
\brief Prints the exit code if != 0.
@ -174,6 +175,9 @@ void ScreenPlayWallpaper::processError(QProcess::ProcessError error)
*/
bool ScreenPlayWallpaper::setWallpaperValue(const QString& key, const QString& value, const bool save)
{
if(m_isExiting)
return false;
if (!m_connection) {
qWarning() << "Cannot set value for unconnected wallpaper!";
return false;
@ -207,13 +211,12 @@ bool ScreenPlayWallpaper::setWallpaperValue(const QString& key, const QString& v
void ScreenPlayWallpaper::setSDKConnection(std::unique_ptr<SDKConnection> connection)
{
m_connection = std::move(connection);
qInfo() << "[3/3] SDKConnection (Wallpaper) saved!";
qInfo() << "[4/4] SDKConnection (Wallpaper) saved!";
setIsConnected(true);
QObject::connect(m_connection.get(), &SDKConnection::disconnected, this, [this]() {
setIsConnected(false);
qInfo() << "disconnecetd;";
});
QTimer::singleShot(1000, this, [this]() {
if (playbackRate() != 1.0) {
@ -245,6 +248,10 @@ void ScreenPlayWallpaper::replace(
const InstalledType::InstalledType type,
const bool checkWallpaperVisible)
{
if(m_isExiting)
return;
if (!m_connection) {
qWarning() << "Cannot replace for unconnected wallpaper!";
return;

View File

@ -132,7 +132,7 @@ signals:
void isConnectedChanged(bool isConnected);
public slots:
void messageQuit();
void close();
void processExit(int exitCode, QProcess::ExitStatus exitStatus);
void processError(QProcess::ProcessError error);
bool setWallpaperValue(const QString& key, const QString& value, const bool save = false);
@ -256,6 +256,9 @@ private:
float m_playbackRate { 1.0f };
QTimer m_pingAliveTimer;
QStringList m_appArgumentsList;
bool m_isConnected;
bool m_isConnected { false };
// There are still cases where we can access the current item
// while exiting. This flag is to ignore all setWallpaperValue calls
bool m_isExiting { false };
};
}

View File

@ -73,11 +73,15 @@ bool ScreenPlayWidget::start()
/*!
\brief Sends command quit to the widget.
*/
void ScreenPlayWidget::messageQuit()
void ScreenPlayWidget::close()
{
QJsonObject obj;
obj.insert("command", "quit");
m_connection->sendMessage(QJsonDocument(obj).toJson(QJsonDocument::Compact));
// When the wallpaper never connected, this is invalid
if (!m_connection) {
qCritical() << "Cannot request quit, widget never connected!";
return;
}
m_connection->close();
}
/*!

View File

@ -85,7 +85,7 @@ public:
ProjectSettingsListModel* getProjectSettingsListModel() { return &m_projectSettingsListModel; }
public slots:
void messageQuit();
void close();
QJsonObject getActiveSettingsJson();
void setPreviewImage(QString previewImage)

View File

@ -64,7 +64,7 @@ void ScreenPlay::SDKConnection::readyRead()
qCritical() << "Wallpaper type not found. Expected: " << ScreenPlayUtil::getAvailableTypes() << " got: " << msg;
}
qInfo() << "[2/3] SDKConnection parsed with type: " << m_type << " connected with AppID:" << m_appID;
qInfo() << "[2/4] SDKConnection parsed with type: " << m_type << " connected with AppID:" << m_appID;
emit appConnected(this);
@ -108,28 +108,15 @@ bool ScreenPlay::SDKConnection::sendMessage(const QByteArray& message)
*/
bool ScreenPlay::SDKConnection::close()
{
if (!m_socket){
qWarning() << "Cannot close invalid socket.";
return false;
}
qInfo() << "Close " << m_type << m_appID << m_socket->state();
m_socket->disconnectFromServer();
m_socket->close();
QJsonObject obj;
obj.insert("command", QJsonValue("quit"));
QByteArray command = QJsonDocument(obj).toJson();
m_socket->write(command);
if (!m_socket->waitForBytesWritten()) {
qWarning("Faild to send quit command to app");
return false;
}
if (m_socket->state() == QLocalSocket::ConnectedState) {
m_socket->disconnectFromServer();
m_socket->close();
qInfo() << "### Destroy APPID:\t " << m_appID << " State: " << m_socket->state();
} else {
qWarning() << "Cannot disconnect app " << m_appID << " with the state:" << m_socket->state();
return false;
}
return true;
return m_socket->state() == QLocalSocket::UnconnectedState;
}
}