QtService和QtRemoteObject如何使工作togather

时间:2017-08-02 18:13:35

标签: qt service

我正在尝试开发一个远程一些自定义QObject的服务(实际上这个QObject访问一些数据库并提供一些统计信息)。为此我使用了QtService和QtRemoteObject,当我在一个简单的QCoreApplication中托管远程对象时一切都很好,我可以连接到主机并运行插槽或获取信号,但是当我在QtService上托管远程对象时应用程序,我无法连接到主机,起初我考虑过防火墙,所以我为应用程序添加了入站 - 出站规则,但仍然无效。 如果你需要它的任何部分,代码有点大(serval文件),我会提供它。

服务类:

#include <QtService>
#include <QCoreApplication>
#include <QRemoteObjectHost>
#include "affairstatero.h"

class AffairStateService : public QtService<QCoreApplication>
{
public:
    explicit AffairStateService(int argc, char** argv, const QString& name = "affair_state");

    // QtServiceBase interface
protected:
    void start() override;
    void stop() override;
    void pause() override;
    void resume() override;

private:
    AffairStateRemoteObject* _affairStateRO;
    QRemoteObjectHost* _remotingHost;
};

CPP:

#include "affairstateservice.h"
#include <QDebug>
#include <QSqliteSettings>

AffairStateService::AffairStateService(int argc, char **argv, const QString &name):
    QtService(argc,argv,name)
{
    this->setServiceDescription("report affair states.");
    this->setServiceFlags(QtService::CanBeSuspended);
    qDebug() << "service starting ...";
}

void AffairStateService::start()
{
    _affairStateRO = new AffairStateRemoteObject();
    _remotingHost = new QRemoteObjectHost();
    qInfo() << "Starting the service";
    QSqliteSettings settings;
    qInfo() << "Settings filename " << settings.fileName() << settings.organizationName() << settings.applicationName();
    _affairStateRO->startTimer();
    _remotingHost->setHostUrl(QUrl(settings.value("endpoint").toString()));
    _remotingHost->enableRemoting(_affairStateRO, "AffairStateRO");
    qInfo() << "Remoting on " << _remotingHost->hostUrl().toString();
    qInfo() << "Done";
}

void AffairStateService::stop()
{
    _affairStateRO->stopTimer();
    _remotingHost->disableRemoting(_affairStateRO);
}

void AffairStateService::pause()
{
    _remotingHost->disableRemoting(_affairStateRO);
}

void AffairStateService::resume()
{
    _remotingHost->enableRemoting(_affairStateRO);
}

信息:Windows 10 / x64 - Qt 5.9.1 MCVS2017

1 个答案:

答案 0 :(得分:1)

好的,我将回答我自己的问题,问题是在服务器端我通过执行QRemoteObjectHost::enableRemoting(&object,"Object");为远程对象命名,但在客户端,我只是在没有提供源的情况下获取副本名称如QRemoteObjectNode::acquire<ObjReplica>();

我不确切知道,但我猜QtRemoteObject将没有名称的远程对象设置为没有名称的默认对象,但是在命名远程对象时,我们需要获取具有相同名称的副本,即使来源只是远程控制一个对象。

当在服务器端将对象远程设置为QRemoteObjectHost::enableRemoting(&obj, "Object");时,在客户端,我们需要QRemoteObjectNode::acquire<ObjReplica>("Object");

这与QtService无关。