1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-07 03:22:33 +01:00

Add socket closing mechanism

This commit is contained in:
kelteseth 2018-03-12 16:21:03 +01:00
parent 6302f3d1e5
commit b5b6210b39
2 changed files with 96 additions and 31 deletions

View File

@ -1,5 +1,8 @@
#include "sdkconnector.h"
#include <QJsonDocument>
#include <QJsonObject>
SDKConnector::SDKConnector(QObject* parent)
: QObject(parent)
{
@ -7,34 +10,13 @@ SDKConnector::SDKConnector(QObject* parent)
connect(m_server.data(), &QLocalServer::newConnection, this, &SDKConnector::newConnection);
if (!m_server.data()->listen("ScreenPlay")) {
qDebug() << "SERVER: Server could not start";
} else {
qDebug() << "SERVER: Server started!";
//TODO
}
}
void SDKConnector::newConnection()
{
QLocalSocket* socket = new QLocalSocket(this);
socket = m_server.data()->nextPendingConnection();
m_clients.append(socket);
// connect(socket, &QLocalSocket::readyRead, [&]() {
// qDebug() << socket->readAll();
// });
// connect(socket,&QLocalSocket::stateChanged, [&]() {
// switch (socket->state()) {
// case QLocalSocket::UnconnectedState:;
// break;
// case QLocalSocket::ConnectingState:;
// break;
// case QLocalSocket::ConnectedState:;
// break;
// case QLocalSocket::ClosingState:;
// break;
// }
// });
m_clients.append(QSharedPointer<SDKConnection>(new SDKConnection(m_server.data()->nextPendingConnection())));
}
void SDKConnector::closeAllWallpapers()
@ -43,3 +25,29 @@ void SDKConnector::closeAllWallpapers()
m_clients.at(i)->close();
}
}
void SDKConnector::setWallpaperValue(QString appID, QString key, QString value)
{
for (int i = 0; i < m_clients.count(); ++i) {
if (m_clients.at(i).data()->appID() == appID) {
QJsonObject obj;
obj.insert(key, QJsonValue(value));
QByteArray send = QJsonDocument(obj).toJson();
m_clients.at(i).data()->socket()->write(send);
m_clients.at(i).data()->socket()->waitForBytesWritten();
}
}
}
void SDKConnection::setSocket(QLocalSocket* socket)
{
m_socket = socket;
}
QLocalSocket* SDKConnection::socket() const
{
return m_socket;
}

View File

@ -1,33 +1,90 @@
#pragma once
#include <QObject>
#include <QLocalSocket>
#include <QLocalServer>
#include <QLocalSocket>
#include <QObject>
#include <QSharedPointer>
#include <QVector>
#include <QTimer>
#include <QJsonValue>
#include <QVector>
/*!
\class SDKConnector
\brief Used for every Wallpaper, Scene or Widget communication via Windows pipes/QLocalSocket
*/
class SDKConnection;
class SDKConnector : public QObject
{
class SDKConnector : public QObject {
Q_OBJECT
public:
explicit SDKConnector(QObject *parent = nullptr);
explicit SDKConnector(QObject* parent = nullptr);
signals:
public slots:
void newConnection();
void closeAllWallpapers();
void setWallpaperValue(QString appID, QString key, QString value);
private:
QSharedPointer<QLocalServer> m_server;
QVector<QLocalSocket*> m_clients;
QVector<QSharedPointer<SDKConnection>> m_clients;
};
class SDKConnection : public QObject {
Q_OBJECT
Q_PROPERTY(QString appID READ appID WRITE setAppID NOTIFY appIDChanged)
public:
explicit SDKConnection(QLocalSocket* socket, QObject* parent = nullptr)
{
m_socket = socket;
connect(m_socket, &QLocalSocket::readyRead, this, &SDKConnection::readyRead);
connect(m_socket, &QLocalSocket::disconnected, this, &SDKConnection::disconnected);
}
void setSocket(QLocalSocket* socket);
QString appID() const
{
return m_appID;
}
QLocalSocket *socket() const;
signals:
void requestCloseAt(int at);
void appIDChanged(QString appID);
public slots:
void readyRead()
{
QString msg = QString(m_socket->readAll());
msg.contains("appID=");
m_appID = msg;
}
void disconnected(){
close();
}
void close()
{
m_socket->close();
}
void setAppID(QString appID)
{
if (m_appID == appID)
return;
m_appID = appID;
emit appIDChanged(m_appID);
}
private:
QLocalSocket* m_socket;
QString m_appID;
};