为每个单个副本信号调用 QtRemoteObjects 副本

QtRemoteObjects replicas being called for every single replica signal

我搭建了一个最小的source/replica直连QtRo系统: https://codeshare.io/29xj4V
我想要实现的是唯一标识来自 source 的消息是对每个 replicas 发送的消息的回复。 如果 rep1 发送 "hello source, I'm rep1",我希望 source 回复 "hello rep1 this is source"。我想以非常高且不同的速度用 3 replicas 运行 实现相同的效果。问题是每个 signalsource source 回复所有 3 replicas;我过滤它的方式是通过以文本格式发送 replica 的 "id" 然后将该 id 附加到消息中以在 replica 本身内部检查它,如果它是匹配然后它接受消息,否则它丢弃它。虽然这似乎可行,但这里有很大的开销。我的问题是:有没有更好的方法来处理这个问题而不是来回发送标识符?谢谢

副本接收相同的信号,无法从源端过滤该数据。因此过滤必须在客户端完成,为此您可以使用作为标识符的属性并将该数据作为信号中的新参数发送,而不是在消息中发送。因此,尽管有很多调用,但由于 if 语句不会消耗太多时间,因此成本会很小。

remoteminimal.rep

class RemoteMinimal
{
    SIGNAL(sendData(const QString &, const QString &));
    SLOT(void printData(const QString &));
    SLOT(void process(const QString &, const QString &));
    SLOT(void triggerSendData());
};

minimalsource.h

#ifndef MINIMALSOURCE_H
#define MINIMALSOURCE_H

#include "rep_remoteminimal_source.h"
#include <QObject>

class MinimalSource : public RemoteMinimalSource
{
    Q_OBJECT
public:
    MinimalSource(QObject *parent = nullptr);
    ~MinimalSource() override;

public slots:
    void printData(const QString& data) override;
    void process(const QString& data, const QString & id) override;
    void triggerSendData() override;

private:
    int m_global_server;
};

#endif // MINIMALSOURCE_H

minimalsource.cpp

#include "minimalsource.h"

MinimalSource::MinimalSource(QObject* parent):
    RemoteMinimalSource (parent), m_global_server(0)
{
}

MinimalSource::~MinimalSource(){}

void MinimalSource::printData(const QString& data)
{
    Q_UNUSED(data);
}

void MinimalSource::process(const QString& data, const QString &id)
{
    m_global_server++;
    emit sendData(data+":"+QString::number(m_global_server), id);
}

void MinimalSource::triggerSendData()
{

}

objects.h

#ifndef OBJECTS_H
#define OBJECTS_H

#include <QObject>
#include "rep_remoteminimal_replica.h"

class QTimer;

class Objects: public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString id READ id WRITE setId)
    Q_PROPERTY(double rate READ rate WRITE setRate)
public:
    explicit Objects(QSharedPointer<RemoteMinimalReplica> ptr);
    ~Objects();
    double rate() const;
    void setRate(double rate);

    QString id() const;
    void setId(const QString &id);

signals:
    void sendData(const QString&, const QString &);

public slots:
    void printData(const QString&, const QString &);
    void process(const QString&);
    void triggerSendData();
    void run();
private:
    int m_global_server;
    QTimer *m_loop;
    double m_rate;
    QString m_id;
    QSharedPointer<RemoteMinimalReplica> reptr;
};

#endif // OBJECTS_H

objects.cpp

#include "objects.h"

#include <QTimer>
#include <QDebug>

Objects::Objects(QSharedPointer<RemoteMinimalReplica> ptr): QObject (Q_NULLPTR),
    m_global_server(0), reptr(ptr)
{
    m_loop = new QTimer(this);
}
Objects::~Objects(){}

void Objects::printData(const QString& data, const QString& id)
{
    if(id == m_id)
        qDebug() << "[" << m_id << "]:" << data;
}

void Objects::process(const QString& data)
{
    emit sendData(data+":processed", m_id);
}

void Objects::run()
{
    connect(m_loop, &QTimer::timeout, this, &Objects::triggerSendData);
    m_loop->setInterval(static_cast<int>(1000/m_rate));
    m_loop->start();
}

QString Objects::id() const
{
    return m_id;
}

void Objects::setId(const QString &id)
{
    m_id = id;
}

double Objects::rate() const
{
    return m_rate;
}

void Objects::setRate(double rate)
{
    m_rate = rate;
}

void Objects::triggerSendData()
{
    m_global_server++;
    emit sendData(QString("test:%1:%2").arg(m_id).arg(m_global_server), m_id);
}

您找到的完整示例here