Qt Remote Objects - Externe QIODevices
Externe QIODevices
Qt Remote Objects unterstützt mehrere Kommunikationskanäle, wie z.B. das Paar QTcpServer und QTcpSocket. Mit dem gewünschten QUrl für tcp oder dem gewünschten Namen (für die Paare QLocalServer und QLocalSocket ) ist der Code, der zum Abhören und Verbinden benötigt wird, ein Boilerplate und wird intern von Qt gehandhabt. Qt Remote Objects unterstützt auch andere Typen von QIODevice, und die QRemoteObjectNode Klassen bieten zusätzliche Methoden, um Fälle zu unterstützen, in denen benutzerdefinierter Code benötigt wird.
Ein konstruiertes Beispiel mit TCP/IP wird unten gezeigt. Ein realistischeres Beispiel würde eine SSL-Verbindung verwenden, die die Konfiguration von Zertifikaten usw. erfordern würde.
// Create the server and listen outside of QtRO QTcpServer tcpServer; tcpServer.listen(QHostAddress(QStringLiteral("127.0.0.1")), 65213); // Create the host node. We don't need a hostUrl unless we want to take // advantage of external schemas (see next example). QRemoteObjectHost srcNode; // Make sure any connections are handed to QtRO QObject::connect(&tcpServer, &QTcpServer::newConnection, &srcNode, [&srcNode, &tcpServer]() { srcNode.addHostSideConnection(tcpServer.nextPendingConnection()); });
Der Code auf der Replikatseite muss sich manuell mit dem Host verbinden
QRemoteObjectNode repNode; QTcpSocket *socket = new QTcpSocket(&repNode); QObject::connect(socket, &QTcpSocket::connected, &repNode, [socket, &repNode]() { repNode.addClientSideConnection(socket); }); socket->connectToHost(QHostAddress(QStringLiteral("127.0.0.1")), 65213);
Externe Schemata
Es ist möglich, jede Seite von QIODevice zu erstellen und QRemoteObjectNode::addClientSideConnection(QIODevice *ioDevice) und QRemoteObjectHostBase::addHostSideConnection(QIODevice *ioDevice) wie oben gezeigt aufzurufen. Dies wird vollständig unterstützt, setzt aber voraus, dass der Client weiß, wie die Verbindung hergestellt wird, oder dass er eine Möglichkeit hat, diese Informationen zu ermitteln. Dies ist genau das Problem, für das die Registry entwickelt wurde.
Qt Remote Objects erlaubt auch die Verwendung von "External Schemas" mit der Registry, was beim Verbindungsaufbau hilft. Auf der Seite QRemoteObjectHost muss der Benutzer die hostUrl mit dem gewünschten Schema einstellen.
// 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; tcpServer.listen(QHostAddress(extUrl.host()), extUrl.port()); // 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. QRemoteObjectHost srcNode(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.
Auf der Replica-Seite muss QRemoteObjectNode einen Callback registrieren, der verwendet wird, wenn das externe Schema erkannt wird. Der Callback muss ein RemoteObjectSchemaHandler sein.
// 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) { QTcpSocket *socket = new QTcpSocket(&repNode); connect(socket, &QTcpSocket::connected, [socket, &repNode]() { repNode.addClientSideConnection(socket); }); connect(socket, &QSslSocket::errorOccurred, [socket](QAbstractSocket::SocketError error) { delete socket; }); 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);
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.