Ref T173, unregister a DBus service before connecting

* In case there is a crash and we reconnect, the old service might be still registered
* "private slots" -> "private"
This commit is contained in:
Klaus Basan
2017-10-14 02:40:39 +02:00
parent 5dd83a16c4
commit 807ceb16ac
2 changed files with 6 additions and 5 deletions

View File

@@ -32,6 +32,7 @@ namespace BlackMisc
case SERVERMODE_SESSIONBUS: case SERVERMODE_SESSIONBUS:
{ {
QDBusConnection connection = QDBusConnection::connectToBus(QDBusConnection::SessionBus, coreServiceName()); QDBusConnection connection = QDBusConnection::connectToBus(QDBusConnection::SessionBus, coreServiceName());
connection.unregisterService(service); // allow reconnecting by removing still registered service
if (! connection.isConnected() || ! connection.registerService(service)) if (! connection.isConnected() || ! connection.registerService(service))
{ {
// registration fails can either mean something wrong with DBus or service already exists // registration fails can either mean something wrong with DBus or service already exists
@@ -43,6 +44,7 @@ namespace BlackMisc
case SERVERMODE_SYSTEMBUS: case SERVERMODE_SYSTEMBUS:
{ {
QDBusConnection connection = QDBusConnection::connectToBus(QDBusConnection::SystemBus, coreServiceName()); QDBusConnection connection = QDBusConnection::connectToBus(QDBusConnection::SystemBus, coreServiceName());
connection.unregisterService(service); // allow reconnecting by removing still registered service
if (! connection.isConnected() || ! connection.registerService(service)) if (! connection.isConnected() || ! connection.registerService(service))
{ {
// registration fails can either mean something wrong with DBus or service already exists // registration fails can either mean something wrong with DBus or service already exists
@@ -70,7 +72,7 @@ namespace BlackMisc
{ {
CLogMessage(this).warning("DBus P2P connection failed: %1") << lastQDBusServerError().message(); CLogMessage(this).warning("DBus P2P connection failed: %1") << lastQDBusServerError().message();
} }
connect(m_busServer.data(), &QDBusServer::newConnection, this, &CDBusServer::ps_registerObjectsWithP2PConnection); connect(m_busServer.data(), &QDBusServer::newConnection, this, &CDBusServer::registerObjectsWithP2PConnection);
} }
break; break;
} }
@@ -154,7 +156,7 @@ namespace BlackMisc
return ""; return "";
} }
bool CDBusServer::ps_registerObjectsWithP2PConnection(QDBusConnection connection) bool CDBusServer::registerObjectsWithP2PConnection(QDBusConnection connection)
{ {
Q_ASSERT(! m_objects.isEmpty()); Q_ASSERT(! m_objects.isEmpty());
m_connections.insert(connection.name(), connection); m_connections.insert(connection.name(), connection);
@@ -236,7 +238,7 @@ namespace BlackMisc
bool CDBusServer::hasQDBusServer() const bool CDBusServer::hasQDBusServer() const
{ {
return ! m_busServer.isNull(); return !m_busServer.isNull();
} }
void CDBusServer::removeAllObjects() void CDBusServer::removeAllObjects()

View File

@@ -145,9 +145,8 @@ namespace BlackMisc
return QDBusConnection::ExportAdaptors | QDBusConnection::ExportAllSignals | QDBusConnection::ExportAllSlots; return QDBusConnection::ExportAdaptors | QDBusConnection::ExportAllSignals | QDBusConnection::ExportAllSlots;
} }
private slots:
//! Called when a new DBus client has connected in P2P mode //! Called when a new DBus client has connected in P2P mode
bool ps_registerObjectsWithP2PConnection(QDBusConnection connection); bool registerObjectsWithP2PConnection(QDBusConnection connection);
}; };
} }