QDBusConnectionManager: Use invokeMethod() to create servers

Use QMetaObject::invokeMethod() with a lambda instead of setting
up a permanent signal/slot connections with BlockingQueuedConnection
type. This makes the code flow easier to follow.

Change-Id: Ib6566e7a4694ecbd69900b645d020b3331fb3462
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ievgenii Meshcheriakov 2023-08-15 13:14:57 +02:00
parent 338de68395
commit 7c9e61a3fe
3 changed files with 16 additions and 11 deletions

View File

@ -74,8 +74,6 @@ QDBusConnectionManager::QDBusConnectionManager()
connect(this, &QDBusConnectionManager::connectionRequested,
this, &QDBusConnectionManager::executeConnectionRequest, Qt::BlockingQueuedConnection);
connect(this, &QDBusConnectionManager::serverRequested,
this, &QDBusConnectionManager::createServer, Qt::BlockingQueuedConnection);
moveToThread(this); // ugly, don't do this in other projects
#ifdef Q_OS_WIN
@ -223,12 +221,17 @@ void QDBusConnectionManager::executeConnectionRequest(QDBusConnectionManager::Co
}
}
void QDBusConnectionManager::createServer(const QString &address, void *server)
void QDBusConnectionManager::createServer(const QString &address, QDBusServer *server)
{
QDBusErrorInternal error;
QDBusConnectionPrivate *d = new QDBusConnectionPrivate;
d->setServer(static_cast<QDBusServer *>(server),
q_dbus_server_listen(address.toUtf8().constData(), error), error);
QMetaObject::invokeMethod(
this,
[&address, server] {
QDBusErrorInternal error;
QDBusConnectionPrivate *d = new QDBusConnectionPrivate;
d->setServer(server, q_dbus_server_listen(address.toUtf8().constData(), error),
error);
},
Qt::BlockingQueuedConnection);
}
QT_END_NAMESPACE

View File

@ -25,6 +25,8 @@
QT_BEGIN_NAMESPACE
class QDBusServer;
class QDBusConnectionManager : public QDaemonThread
{
Q_OBJECT
@ -42,18 +44,18 @@ public:
QDBusConnectionPrivate *connectToBus(const QString &address, const QString &name);
QDBusConnectionPrivate *connectToPeer(const QString &address, const QString &name);
void createServer(const QString &address, QDBusServer *server);
mutable QMutex mutex;
signals:
void connectionRequested(ConnectionRequestData *);
void serverRequested(const QString &address, void *server);
protected:
void run() override;
private:
void executeConnectionRequest(ConnectionRequestData *data);
void createServer(const QString &address, void *server);
QHash<QString, QDBusConnectionPrivate *> connectionHash;

View File

@ -38,7 +38,7 @@ QDBusServer::QDBusServer(const QString &address, QObject *parent)
if (!instance)
return;
emit instance->serverRequested(address, this);
instance->createServer(address, this);
Q_ASSERT(d != nullptr);
QObject::connect(d, SIGNAL(newServerConnection(QDBusConnectionPrivate*)),
@ -67,7 +67,7 @@ QDBusServer::QDBusServer(QObject *parent)
if (!instance)
return;
emit instance->serverRequested(address, this);
instance->createServer(address, this);
Q_ASSERT(d != nullptr);
QObject::connect(d, SIGNAL(newServerConnection(QDBusConnectionPrivate*)),