Qt远程对象-外部QIODevices

Qt Remote Objects - External QIODevices

Qt远程对象-外部QIODevices

External QIODevices

外部QIO设备

Qt Remote Objects supports several communications channels out-of-the-box, such as the QTcpServer and QTcpSocket pair. Given the desired QUrl for tcp, or the desired name (for the QLocalServer and QLocalSocket pair), the code needed to listen and connect are boilerplate and handled internally by Qt. Qt Remote Objects supports other types of QIODevice as well, and the QRemoteObjectNode classes provide additional methods to support cases where custom code is needed.

​Qt远程对象支持多种开箱即用的通信通道,如QTcpServer和QTcpSocket对。给定tcp所需的QUrl或所需的名称(QLocalServer和QLocalSocket对),监听和连接所需的代码是样板,由Qt内部处理。Qt远程对象也支持其他类型的QIODevice,QRemoteObjectNode类提供了额外的方法来支持需要自定义代码的情况。

A contrived example with TCP/IP is shown below. A more realistic example would use an SSL connection, which would require configuration of certificates, etc.

下面显示了一个使用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());
});

The Replica side code needs to manually connect to the Host

副本侧代码需要手动连接到主机

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

External Schemas

外部模式

It is possible to create each side of the QIODevice and call QRemoteObjectNode::addClientSideConnection(QIODevice *ioDevice) and QRemoteObjectHostBase::addHostSideConnection(QIODevice *ioDevice) as shown above. This is fully supported, but requires the client know how to establish the connection or have a way to discover that information. This is exactly the problem the registry was designed to solve.

​可以创建QIODevice的每一侧,并调用QRemoteObjectNode::addClientSideConnection(QIODevice*ioDevice)和QRemoteObject HostBase::addHostSideConnection(QIODevice*io Device),如上所示。这是完全支持的,但要求客户端知道如何建立连接或有方法发现该信息。这正是注册表旨在解决的问题。

Qt Remote Objects also allows "External Schemas" to be used with the registry, which helps with connection setup. On the QRemoteObjectHost side, the user must set the hostUrl with the desired schema.

​Qt远程对象还允许“外部模式”与注册表一起使用,这有助于连接设置。在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.

On the Replica side, the QRemoteObjectNode needs to register a callback to be used when the external schema is detected. The callback must be a RemoteObjectSchemaHandler.

​在副本端,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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值