- enum ProxyMode {
- Server,
- Client
- };
-
- enum RequestType {
- Sync = 1,
- RpcCall,
- InitRequest,
- InitData,
- HeartBeat,
- HeartBeatReply
- };
-
- enum ClientConnectionType {
- SignalProxyConnection,
- IODeviceConnection
- };
-
- enum CustomEvents {
- PeerSignal = QEvent::User,
- RemovePeer
- };
-
- SignalProxy(QObject *parent);
- SignalProxy(ProxyMode mode, QObject *parent);
- SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
- virtual ~SignalProxy();
-
- void setProxyMode(ProxyMode mode);
- inline ProxyMode proxyMode() const { return _proxyMode; }
-
- bool addPeer(QIODevice *iodev);
- bool addPeer(SignalProxy *proxy);
- void removePeer(QObject *peer);
- void removeAllPeers();
-
- bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
- bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
-
- void synchronize(SyncableObject *obj);
-
- //! Writes a QVariant to a device.
- /** The data item is prefixed with the resulting blocksize,
- * so the corresponding function readDataFromDevice() can check if enough data is available
- * at the device to reread the item.
- */
- static void writeDataToDevice(QIODevice *dev, const QVariant &item, bool compressed = false);
-
- //! Reads a data item from a device that has been written by writeDataToDevice().
- /** If not enough data bytes are available, the function returns false and the QVariant reference
- * remains untouched.
- */
- static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item, bool compressed = false);
-
- class ExtendedMetaObject;
- ExtendedMetaObject *extendedMetaObject(const QMetaObject *meta) const;
- ExtendedMetaObject *createExtendedMetaObject(const QMetaObject *meta);
- inline ExtendedMetaObject *extendedMetaObject(const QObject *obj) const { return extendedMetaObject(metaObject(obj)); }
- inline ExtendedMetaObject *createExtendedMetaObject(const QObject *obj) { return createExtendedMetaObject(metaObject(obj)); }
-
- bool isSecure() const { return _secure; }
- void dumpProxyStats();
-
-public slots:
- void detachObject(QObject *obj);
- void detachSignals(QObject *sender);
- void detachSlots(QObject *receiver);
- void stopSync(QObject *obj);
+ 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);