- enum RPCTypes {
- Server,
- Client,
- Peer
- };
-
- SignalProxy(QObject* parent);
- SignalProxy(RPCTypes type, QObject* parent);
- SignalProxy(RPCTypes type, QIODevice* device, QObject* parent);
- virtual ~SignalProxy();
-
- void setRPCType(RPCTypes type);
- RPCTypes rpcType() const;
-
- bool maxPeersReached();
-
- bool addPeer(QIODevice *iodev);
- void removePeer(QIODevice *iodev = 0);
-
- bool attachSignal(QObject* sender, const char* signal, const QByteArray& rpcFunction = QByteArray());
- bool attachSlot(const QByteArray& rpcFunction, QObject* recv, const char* slot);
-
- void detachObject(QObject *obj);
- void detachSignals(QObject *sender);
- void detachSlots(QObject *receiver);
-
- void call(const char *signal , QVariant p1, QVariant p2, QVariant p3, QVariant p4,
- QVariant p5, QVariant p6, QVariant p7, QVariant p8, QVariant p9);
- void call(const QByteArray &funcName, const QVariantList ¶ms);
-
- static void writeDataToDevice(QIODevice *dev, const QVariant &item);
- static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
-
- const QList<int> &argTypes(QObject* obj, int methodId);
- const QByteArray &methodName(QObject* obj, int methodId);
-
- typedef QHash<int, QList<int> > ArgHash;
- typedef QHash<int, QByteArray> MethodNameHash;
- struct ClassInfo {
- ArgHash argTypes;
- MethodNameHash methodNames;
- // QHash<int, int> syncMap
- };
-
+ enum ProxyMode
+ {
+ Server,
+ Client
+ };
+
+ enum EventType
+ {
+ RemovePeerEvent = QEvent::User
+ };
+
+ SignalProxy(QObject* parent);
+ SignalProxy(ProxyMode mode, QObject* parent);
+ ~SignalProxy() override;
+
+ void setProxyMode(ProxyMode mode);
+ inline ProxyMode proxyMode() const { return _proxyMode; }
+
+ void setHeartBeatInterval(int secs);
+ inline int heartBeatInterval() const { return _heartBeatInterval; }
+ void setMaxHeartBeatCount(int max);
+ inline int maxHeartBeatCount() const { return _maxHeartBeatCount; }
+
+ bool addPeer(Peer* peer);
+
+ /**
+ * Attaches a signal for remote emission.
+ *
+ * After calling this method, whenever the sender emits the given signal, an RpcCall message is sent to connected peers.
+ * On the other end, a slot can be attached to handle this message by calling attachSlot().
+
+ * By default, the signal name being sent is as if the SIGNAL() macro had been used, i.e. the normalized signature prefixed with a '2'.
+ * This can be overridden by explicitly providing the signalName argument.
+ *
+ * @sa attachSlot
+ *
+ * @param sender The sender of the signal
+ * @param signal The signal itself (given as a member function pointer)
+ * @param signalName Optional string to be used instead of the actual signal name. Will be normalized.
+ * @returns true if attaching the signal was successful
+ */
+ template<typename Signal>
+ bool attachSignal(const typename FunctionTraits<Signal>::ClassType* sender, Signal signal, const QByteArray& signalName = {});
+
+ /**
+ * Attaches a slot to a remote signal.
+ *
+ * After calling this method, upon receipt of an RpcCall message with a signalName matching the signalName parameter, the given slot
+ * is called with the parameters contained in the message. This is intended to be used in conjunction with attachSignal() on the other
+ * end of the connection.
+ *
+ * Normally, the signalName should be given using the SIGNAL() macro; it will be normalized automatically.
+ *
+ * @sa attachSignal
+ *
+ * @param signalName Name of the signal as stored in the RpcCall message
+ * @param receiver Receiver of the signal
+ * @param slot Slot to be called (given as a member function pointer)
+ * @returns true if attaching the slot was successful
+ */
+ template<typename Slot, typename = std::enable_if_t<std::is_member_function_pointer<Slot>::value>>
+ bool attachSlot(const QByteArray& signalName, typename FunctionTraits<Slot>::ClassType* receiver, Slot slot);
+
+ /**
+ * @overload
+ *
+ * Attaches a functor to a remote signal.
+ *
+ * After calling this method, upon receipt of an RpcCall message with a signalName matching the signalName parameter, the given functor
+ * is invoked with the parameters contained in the message. This is intended to be used in conjunction with attachSignal() on the other
+ * end of the connection. This overload can be used, for example, with a lambda that accepts arguments matching the RpcCall parameter
+ * list.
+ *
+ * The context parameter controls the lifetime of the connection; if the context is deleted, the functor is deleted as well.
+ *
+ * @sa attachSignal
+ *
+ * @param signalName Name of the signal as stored in the RpcCall message
+ * @param context QObject context controlling the lifetime of the callable
+ * @param slot The functor to be invoked
+ * @returns true if attaching the functor was successful
+ */
+ template<typename Slot, typename = std::enable_if_t<!std::is_member_function_pointer<Slot>::value>>
+ bool attachSlot(const QByteArray& signalName, const QObject* context, Slot slot);
+
+ void synchronize(SyncableObject* obj);
+ void stopSynchronize(SyncableObject* obj);
+
+ class ExtendedMetaObject;
+ ExtendedMetaObject* extendedMetaObject(const QMetaObject* meta) const;
+ ExtendedMetaObject* createExtendedMetaObject(const QMetaObject* meta, bool checkConflicts = false);
+ inline ExtendedMetaObject* extendedMetaObject(const QObject* obj) const { return extendedMetaObject(metaObject(obj)); }
+ inline ExtendedMetaObject* createExtendedMetaObject(const QObject* obj, bool checkConflicts = false)
+ {
+ return createExtendedMetaObject(metaObject(obj), checkConflicts);
+ }
+
+ bool isSecure() const { return _secure; }
+ void dumpProxyStats();
+ void dumpSyncMap(SyncableObject* object);
+
+ static SignalProxy* current();
+
+ /**@{*/
+ /**
+ * This method allows to send a signal only to a limited set of peers
+ * @param peers A list of peers that should receive it
+ * @param closure Code you want to execute within of that restricted environment
+ */
+ void restrictTargetPeers(QSet<Peer*> peers, std::function<void()> closure);
+ void restrictTargetPeers(Peer* peer, std::function<void()> closure)
+ {
+ QSet<Peer*> set;
+ set.insert(peer);
+ restrictTargetPeers(set, std::move(closure));
+ }
+
+ // A better version, but only implemented on Qt5 if Initializer Lists exist
+#ifdef Q_COMPILER_INITIALIZER_LISTS
+ void restrictTargetPeers(std::initializer_list<Peer*> peers, std::function<void()> closure)
+ {
+ restrictTargetPeers(QSet<Peer*>(peers), std::move(closure));
+ }
+#endif
+ /**}@*/
+
+ inline int peerCount() const { return _peerMap.size(); }
+ QVariantList peerData();
+
+ Peer* peerById(int peerId);
+
+ /**
+ * @return If handling a signal, the Peer from which the current signal originates
+ */
+ Peer* sourcePeer();
+ void setSourcePeer(Peer* sourcePeer);
+
+ /**
+ * @return If sending a signal, the Peer to which the current signal is directed
+ */
+ Peer* targetPeer();
+ void setTargetPeer(Peer* targetPeer);
+
+protected:
+ void customEvent(QEvent* event) override;
+ void sync_call__(const SyncableObject* obj, ProxyMode modeType, const char* funcname, va_list ap);
+ void renameObject(const SyncableObject* obj, const QString& newname, const QString& oldname);
+