Qt Remote Objects - 외부 QIODevices

외부 QIODevices

Qt Remote ObjectsQTcpServerQTcpSocket 쌍과 같은 여러 통신 채널을 기본적으로 지원합니다. 원하는 QUrl 또는 원하는 이름( QLocalServerQLocalSocket 쌍의 경우)이 주어지면 수신 및 연결에 필요한 코드는 상용구이며 Qt에서 내부적으로 처리합니다. Qt Remote Objects 는 다른 유형의 QIODevice 도 지원하며, QRemoteObjectNode 클래스는 사용자 정의 코드가 필요한 경우를 지원하기 위한 추가 메서드를 제공합니다.

아래는 TCP/IP를 사용한 인위적인 예제입니다. 보다 현실적인 예는 인증서 등의 구성이 필요한 SSL 연결을 사용하는 것입니다.

// 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());
});

레플리카 측 코드는 호스트에 수동으로 연결해야 합니다.

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);

외부 스키마

위와 같이 QIODevice 의 각 쪽을 만들고 QRemoteObjectNode::addClientSideConnection(QIODevice *ioDevice) 및 QRemoteObjectHostBase::addHostSideConnection(QIODevice *ioDevice)를 호출할 수 있습니다. 이 방법은 완전히 지원되지만 클라이언트가 연결을 설정하는 방법을 알고 있거나 해당 정보를 검색할 수 있는 방법이 있어야 합니다. 이것이 바로 레지스트리가 해결하도록 설계된 문제입니다.

Qt Remote Objects 또한 "외부 스키마"를 레지스트리와 함께 사용할 수 있도록 허용하여 연결 설정에 도움이 됩니다. QRemoteObjectHost 쪽에서는 사용자가 원하는 스키마로 hostUrl을 설정해야 합니다.

// 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.

복제본 쪽에서는 외부 스키마가 감지될 때 사용할 콜백을 QRemoteObjectNode 에 등록해야 합니다. 콜백은 RemoteObjectSchemaHandler 이어야 합니다.

// 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.