Qt Remote Objects - 외부 QIODevices
외부 QIODevices
Qt Remote Objects QUrl 는 및 쌍과 같은 여러 통신 채널을 기본적으로 지원합니다. 및 쌍의 경우 원하는 이름을 지정하면, 수신 대기 및 연결에 필요한 코드는 상용구이며 Qt에서 내부적으로 처리합니다. 는 다른 유형의 도 지원하며, 클래스는 사용자 정의 코드가 필요한 경우를 지원하기 위한 추가 메서드를 제공합니다. QTcpServer QTcpSocket QLocalSocket QLocalServer 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.복제본(Replica) 측에서는 레지스트리( 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);© 2026 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.