X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fsignalproxy.cpp;h=f6f75d65ca1f3d76ee5079d4aba6b4eb1cb2ba27;hp=204c197e3e4fa60a8cda4602f9d696f670ab2604;hb=72d25daa2c6cc00c17e79f756ad70bc00b41f9a8;hpb=d1b6499b0b848d4287efae89107576548533502c diff --git a/src/common/signalproxy.cpp b/src/common/signalproxy.cpp index 204c197e..f6f75d65 100644 --- a/src/common/signalproxy.cpp +++ b/src/common/signalproxy.cpp @@ -30,8 +30,8 @@ #include #include #include -#include #include +#include #include "syncableobject.h" #include "util.h" @@ -68,7 +68,7 @@ SignalRelay::SignalRelay(SignalProxy* parent, QObject* source) caller(source), _sync(false) { - QObject::connect(source, SIGNAL(destroyed()), parent, SLOT(detachSender()), Qt::DirectConnection); + QObject::connect(source, SIGNAL(destroyed()), parent, SLOT(detachSender())); } int SignalRelay::qt_metacall(QMetaObject::Call _c, int _id, void **_a) { @@ -273,7 +273,6 @@ void SignalProxy::objectRenamed(QString oldname, QString newname) { } void SignalProxy::objectRenamed(QByteArray classname, QString oldname, QString newname) { - QMutexLocker locker(&slaveMutex); if(_syncSlave.contains(classname) && _syncSlave[classname].contains(oldname) && oldname != newname) _syncSlave[classname][newname] = _syncSlave[classname].take(oldname); } @@ -338,6 +337,21 @@ const QList &SignalProxy::argTypes(QObject *obj, int methodId) { return _classInfo[obj->metaObject()]->argTypes[methodId]; } +void SignalProxy::setMinArgCount(QObject *obj, int methodId) { + const QMetaObject *meta = obj->metaObject(); + QString signature(meta->method(methodId).signature()); + int minCount = meta->method(methodId).parameterTypes().count() - signature.count("="); + Q_ASSERT(!_classInfo[meta]->minArgCount.contains(methodId)); + _classInfo[meta]->minArgCount[methodId] = minCount; +} + +const int &SignalProxy::minArgCount(QObject *obj, int methodId) { + Q_ASSERT(_classInfo.contains(obj->metaObject())); + if(!_classInfo[obj->metaObject()]->minArgCount.contains(methodId)) + setMinArgCount(obj, methodId); + return _classInfo[obj->metaObject()]->minArgCount[methodId]; +} + void SignalProxy::setMethodName(QObject *obj, int methodId) { const QMetaObject *meta = obj->metaObject(); QByteArray method(::methodName(meta->method(methodId))); @@ -395,12 +409,24 @@ const QHash &SignalProxy::syncMap(SyncableObject *obj) { return _classInfo[obj->metaObject()]->syncMap; } +void SignalProxy::setUpdatedRemotelyId(QObject *obj) { + const QMetaObject *meta = obj->metaObject(); + Q_ASSERT(_classInfo.contains(meta)); + _classInfo[meta]->updatedRemotelyId = meta->indexOfSignal("updatedRemotely()"); +} + +int SignalProxy::updatedRemotelyId(SyncableObject *obj) { + Q_ASSERT(_classInfo.contains(obj->metaObject())); + return _classInfo[obj->metaObject()]->updatedRemotelyId; +} + void SignalProxy::createClassInfo(QObject *obj) { if(_classInfo.contains(obj->metaObject())) return; ClassInfo *classInfo = new ClassInfo(); _classInfo[obj->metaObject()] = classInfo; + setUpdatedRemotelyId(obj); } bool SignalProxy::attachSignal(QObject* sender, const char* signal, const QByteArray& sigName) { @@ -440,7 +466,7 @@ bool SignalProxy::attachSlot(const QByteArray& sigName, QObject* recv, const cha _attachedSlots.insert(funcName, qMakePair(recv, methodId)); QObject::disconnect(recv, SIGNAL(destroyed()), this, SLOT(detachSender())); - QObject::connect(recv, SIGNAL(destroyed()), this, SLOT(detachSender()), Qt::DirectConnection); + QObject::connect(recv, SIGNAL(destroyed()), this, SLOT(detachSender())); return true; } @@ -462,7 +488,7 @@ void SignalProxy::synchronize(SyncableObject *obj) { if(proxyMode() == Server) { if(obj->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("renameObject(QString, QString)")) != -1) - connect(obj, SIGNAL(renameObject(QString, QString)), this, SLOT(objectRenamed(QString, QString)), Qt::DirectConnection); + connect(obj, SIGNAL(renameObject(QString, QString)), this, SLOT(objectRenamed(QString, QString))); setInitialized(obj); } else { @@ -490,47 +516,22 @@ void SignalProxy::requestInit(SyncableObject *obj) { } void SignalProxy::detachSender() { - // this is a private slot. the reason for that is, that we enfoce Qt::DirectConnection. - // the result is, that we can be sure, that a registered receiver is removed immediately when it's destroyed. - // since we're guarding the the internal datastructures with mutexes this means, - // that the object destruction is deffered until we're finished delivering our sync/init/whatever request. - // all in all: thread safety! *sigh* detachObject(sender()); } -// detachObject/Signals/Slots() can be called as a result of an incoming call -// this might destroy our the iterator used for delivery -// thus we wrap the actual disconnection by using QueuedConnections void SignalProxy::detachObject(QObject* obj) { - QMutexLocker locker(&slaveMutex); - _detachSignals(obj); - _detachSlots(obj); - SyncableObject *syncobj = qobject_cast(obj); - if(syncobj) _stopSync(syncobj); + detachSignals(obj); + detachSlots(obj); + stopSync(static_cast(obj)); } void SignalProxy::detachSignals(QObject* sender) { - QMutexLocker locker(&slaveMutex); - _detachSignals(sender); -} - -void SignalProxy::detachSlots(QObject* receiver) { - QMutexLocker locker(&slaveMutex); - _detachSlots(receiver); -} - -void SignalProxy::stopSync(SyncableObject* obj) { - QMutexLocker locker(&slaveMutex); - _stopSync(obj); -} - -void SignalProxy::_detachSignals(QObject* sender) { if(!_relayHash.contains(sender)) return; _relayHash.take(sender)->deleteLater(); } -void SignalProxy::_detachSlots(QObject* receiver) { +void SignalProxy::detachSlots(QObject* receiver) { SlotHash::iterator slotIter = _attachedSlots.begin(); while(slotIter != _attachedSlots.end()) { if(slotIter.value().first == receiver) { @@ -540,7 +541,7 @@ void SignalProxy::_detachSlots(QObject* receiver) { } } -void SignalProxy::_stopSync(SyncableObject* obj) { +void SignalProxy::stopSync(SyncableObject* obj) { if(_relayHash.contains(obj)) _relayHash[obj]->setSynchronize(false); @@ -579,11 +580,6 @@ void SignalProxy::receivePeerSignal(QIODevice *sender, const QVariant &packedFun if(call.type() != QVariant::Int) return handleSignal(call.toByteArray(), params); - // well yes we are locking code here and not only data. - // otherwise each handler would have to lock the data anyway. this leaves us on the safe side - // unable to forget a lock - QMutexLocker locker(&slaveMutex); - switch(call.toInt()) { case Sync: return handleSync(params); @@ -625,7 +621,7 @@ void SignalProxy::handleSync(QVariantList params) { qWarning("SignalProxy::handleSync(): invokeMethod for \"%s\" failed ", methodName(receiver, slotId).constData()); return; } - QMetaObject::invokeMethod(receiver, "updatedRemotely"); + invokeSlot(receiver, updatedRemotelyId(receiver)); } void SignalProxy::handleInitRequest(QIODevice *sender, const QVariantList ¶ms) { @@ -663,7 +659,7 @@ void SignalProxy::handleInitRequest(QIODevice *sender, const QVariantList ¶m void SignalProxy::handleInitData(QIODevice *sender, const QVariantList ¶ms) { Q_UNUSED(sender) if(params.count() != 3) { - qWarning() << "SignalProxy::handleInitRequest() received initRequest with invalid param Count:" + qWarning() << "SignalProxy::handleInitData() received initData with invalid param Count:" << params; return; } @@ -673,13 +669,13 @@ void SignalProxy::handleInitData(QIODevice *sender, const QVariantList ¶ms) QVariantMap propertyMap(params[2].toMap()); if(!_syncSlave.contains(className)) { - qWarning() << "SignalProxy::handleInitRequest() received initRequest for unregistered Class:" + qWarning() << "SignalProxy::handleInitData() received initData for unregistered Class:" << className; return; } if(!_syncSlave[className].contains(objectName)) { - qWarning() << "SignalProxy::handleInitRequest() received initRequest for unregistered Object:" + qWarning() << "SignalProxy::handleInitData() received initData for unregistered Object:" << className << objectName; return; } @@ -702,11 +698,41 @@ void SignalProxy::handleSignal(const QByteArray &funcName, const QVariantList &p } bool SignalProxy::invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms) { - int numParams = argTypes(receiver, methodId).count(); - QGenericArgument args[9]; - for(int i = 0; i < numParams; i++) - args[i] = QGenericArgument(params[i].typeName(), params[i].constData()); - return QMetaObject::invokeMethod(receiver, methodName(receiver, methodId), args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]); + const QList args = argTypes(receiver, methodId); + const int numArgs = params.count() < args.count() + ? params.count() + : args.count(); + + if(minArgCount(receiver, methodId) < params.count()) { + qWarning() << "SignalProxy::invokeSlot(): not enough params to invoke" << methodName(receiver, methodId); + return false; + } + + void *_a[] = {0, // return type + 0, 0, 0, 0 , 0, // and 10 args - that's the max size qt can handle with signals and slots + 0, 0, 0, 0 , 0}; + // check for argument compatibility and build params array + for(int i = 0; i < numArgs; i++) { + if(args[i] != QMetaType::type(params[i].typeName())) { + qWarning() << "SignalProxy::invokeSlot(): incompatible param types to invoke" << methodName(receiver, methodId); + return false; + } + _a[i+1] = const_cast(params[i].constData()); + } + + + Qt::ConnectionType type = QThread::currentThread() == receiver->thread() + ? Qt::DirectConnection + : Qt::QueuedConnection; + + if (type == Qt::DirectConnection) { + return receiver->qt_metacall(QMetaObject::InvokeMetaMethod, methodId, _a) < 0; + } else { + qWarning() << "Queued Connections are not implemented yet"; + // not to self: qmetaobject.cpp:990 ff + return false; + } + } void SignalProxy::dataAvailable() { @@ -792,6 +818,7 @@ void SignalProxy::setInitData(SyncableObject *obj, const QVariantMap &properties return; obj->fromVariantMap(properties); setInitialized(obj); + invokeSlot(obj, updatedRemotelyId(obj)); } void SignalProxy::dumpProxyStats() {