Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QRemoteObject Tcp examples
Forum Updated to NodeBB v4.3 + New Features

QRemoteObject Tcp examples

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 335 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Addam
    wrote on last edited by Addam
    #1

    I'm trying to follow the External QIODevices examples, but it's missing to demonstrate how the srcNodes are being enabled and the replicas acquired.

    I cant get the first example working, I tried:

    .rep

    class RemoteObject
    {
        SLOT(void test(QString str))
        SIGNAL(signalTest(int x))
    };
    

    remoteobject.h

    #include "rep_remoteobject_source.h"
    class RemoteObject : public RemoteObjectSource
    {
        Q_OBJECT
    public:
    public slots:
        void test(QString str) {
            qDebug() << "str:" << str;
        }
    };
    
    #include <QNetworkReply>
    #include <QRemoteObjectHost>
    #include <QTcpServer>
    #include "remoteobject.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QTcpServer tcpServer;
        tcpServer.listen(QHostAddress(QStringLiteral("127.0.0.1")), 65213);
    
        QRemoteObjectHost srcNode;
        QObject::connect(&tcpServer, &QTcpServer::newConnection, &srcNode, [&] {
            srcNode.addHostSideConnection(tcpServer.nextPendingConnection());
        });
    
        RemoteObject remoteObject;
        bool ret = srcNode.enableRemoting(&remoteObject, "remoteObject");
        qDebug() << "enableRemoting:" << ret << srcNode.lastError();
        return a.exec();
    }
    

    srcNode.enableRemoting is returning false, lastError: QRemoteObjectNode::OperationNotValidOnClientNode.

    In the enableRemoting documentation says: "Returns false if the current node is a client node"

    And in the example there's a comment:

    // Create the host node. We don't need a hostUrl unless we want to take

    // advantage of external schemas (see next example).

    Without a hostUrl set in the srcNode, it's probably being threatened as a client?

    Am I not understanding it correctly or is something missing from the example?




    The second example compile, but some things:

    • The client don't get the signal from the host.
    • When I try to acquire the replica it fails: qt.remoteobjects: connectionToSource is null
    • When the host close, the client connected to it crashes.

    -EDIT-

    After investigating more, I got some points about the crash
    https://doc.qt.io/qt-6/qabstractsocket.html#disconnected

    "Warning: If you need to delete the sender() of this signal in a slot
    connected to it, use the deleteLater() function."

    In the example it call delete socket on error, which also get triggered when the host is closed...

    host

    #include <QNetworkReply>
    #include <QRemoteObjectHost>
    #include <QTcpServer>
    #include "remoteobject.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        const QUrl registryUrl = QUrl(QStringLiteral("tcp://127.0.0.1:65212"));
        const QUrl extUrl = QUrl(QStringLiteral("exttcp://127.0.0.1:65213"));
    
        QTcpServer tcpServer;
        tcpServer.listen(QHostAddress(extUrl.host()), extUrl.port());
        QRemoteObjectRegistryHost registry(registryUrl);
    
        QRemoteObjectHost srcNode(extUrl, registryUrl, QRemoteObjectHost::AllowExternalRegistration);
        RemoteObject remoteObject;
        ret = srcNode.enableRemoting(&remoteObject, "remoteObject");
        qDebug() << "enableRemoting:" << ret;
    
        int x = 0;
        QTimer* timer = new QTimer();
    
        QObject::connect(timer, &QTimer::timeout, [&]() {
            qDebug() << "x:" << x;
            emit remoteObject.signalTest(x);
            x++;
        });
        timer->start(1000);
        return a.exec();
    }
    

    client

    #include <QRemoteObjectNode>
    #include <QTcpSocket>
    #include <QSslSocket>
    #include "rep_remoteobject_replica.h"
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
    
        const QUrl registryUrl = QUrl(QStringLiteral("tcp://127.0.0.1:65212"));
        QRemoteObjectNode repNode(registryUrl);
        QRemoteObjectNode::RemoteObjectSchemaHandler setupTcp = [&repNode](QUrl url)
        {
            QTcpSocket* socket = new QTcpSocket(&repNode);
            QObject::connect(socket, &QTcpSocket::connected, [socket, &repNode]()
            {
                qDebug() << "connected...";
                repNode.addClientSideConnection(socket);
    
                QSharedPointer<RemoteObjectReplica> ptr;
                ptr.reset(repNode.acquire<RemoteObjectReplica>("remoteObject_2"));  // <---- error: qt.remoteobjects: connectionToSource is null
    
                ptr.data()->test("hello world");
    
            });
            QObject::connect(socket, &QSslSocket::errorOccurred, [socket](QAbstractSocket::SocketError error) {
                qDebug() << "error:" << error;
                delete socket;
            });
    
            qDebug() << "setupTcp...";
            socket->connectToHost(url.host(), url.port());
        };
    
        repNode.registerExternalSchema(QStringLiteral("exttcp"), setupTcp);
    
        QSharedPointer<RemoteObjectReplica> ptr;
        ptr.reset(repNode.acquire<RemoteObjectReplica>("remoteObject_2"));   // <---- im probably doing something wrong here?
                                                                             // and in the example dont demonstrate how to acquire the replica
    
        QObject::connect(ptr.data(), &RemoteObjectReplica::signalTest, &a, [](int x) {
            qDebug() << "x:" << x; // <---- never called
        });
    
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • A Addam marked this topic as a regular topic on

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved