Skip to content
  • 0 Votes
    1 Posts
    216 Views
    No one has replied
  • 1 Votes
    4 Posts
    624 Views
    piseprashanthP

    Basically you missed to add addHostSideConnection on server side. Here is below code snippet.

    // server side
    void onNewServerConnection()
    {
    qDebug() << "onNewServerConnection";
    bool newConn = false;
    if(auto tcpServer = qobject_cast<QTcpServer*>(sender()))
    {
    while(tcpServer->hasPendingConnections())
    {
    newConn = true;
    m_pHost->addHostSideConnection(tcpServer->nextPendingConnection());
    }
    }
    }
    // Use standard tcp url for the registry
    const QUrl registryUrl = QUrl(QStringLiteral("tcp://127.0.0.1:65212"));
    // Use "exttcp" for the "external" interface
    const QUrl extUrl = QUrl(QStringLiteral("exttcp://127.0.0.1:65213"));

    // Create the server and listen outside of QtRO // QTcpServer tcpServer; auto tcpServer = new QTcpServer(this); auto host = extUrl.host(); // We only know how to handle tcp:// and local: bool res = false; res = tcpServer->listen(QHostAddress(extUrl.host()), extUrl.port()); if(res) { // m_servers.insert(url, server); connect(tcpServer, &QTcpServer::newConnection, this, &YourClass::onNewServerConnection); } else { qWarning().nospace() << "server could not listen on URL: " << extUrl.toString() << ". Error type: " << tcpServer->serverError() << ", message: " << tcpServer->errorString(); delete tcpServer; } // We need a registry for everyone to connect to QRemoteObjectRegistryHost registry(registryUrl); // Finally, we create our host node and register "exttcp" as our schema. // We need the AllowExternalRegistration parameter to prevent the node from // setting a hostUrlInvalid error. m_pHost = new QRemoteObjectHost(extUrl, registryUrl, QRemoteObjectHost::AllowExternalRegistration); // From now on, when we call enableRemoting() from this node, the registry // will be updated to show the Source object at extUrl.

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    OnClient side
    // Use standard tcp url for the registry
    const QUrl registryUrl = QUrl(QStringLiteral("tcp://127.0.0.1:65212"));

    // This time create the node connected to the registry QRemoteObjectNode repNode(registryUrl); // Create the RemoteObjectSchemaHandler callback QRemoteObjectNode::RemoteObjectSchemaHandler setupTcp = [&repNode](QUrl url) -> void { QTcpSocket *socket = new QTcpSocket(&repNode); connect(socket, &QTcpSocket::connected, [socket, &repNode]() { qDebug() << "Added client side connection"; repNode.addClientSideConnection(socket); }); connect(socket, &QSslSocket::errorOccurred, [socket](QAbstractSocket::SocketError error) { qDebug() << "Deleted socket"; delete socket; }); qDebug() << "Connected to host with URL: " << url.host() <<":" << url.port(); socket->connectToHost(url.host(), url.port()); }; // Once we call registerExternalSchema, the above method will be called // whenever the registry sees an object we are interested in on "exttcp" repNode.registerExternalSchema(QStringLiteral("exttcp"), setupTcp); // local replica // QRemoteObjectNode repNode; // create remote object node // m_pHost = new QRemoteObjectHost(QUrl(ss.str().c_str())); // repNode.connectToNode(QUrl(ss.str().c_str())); //QUrl(QStringLiteral("local:replica"))); auto ptr = repNode.acquireDynamic("something");