Qt远程对象副本

Qt Remote Objects Replica

Qt远程对象副本

A compiled replica is a QRemoteObjectReplica based type, where the derived class definition is automatically generated by the repc compiler. When you use CMake functions or qmake variables for running the repc compiler, this makes the generation part of the build process. Although only a header is generated, it's a complete type. There is no public constructor, so you need to use the QRemoteObjectNode::acquire template function to create the Replica instance.

​编译后的副本是基于QRemoteObjectReplica的类型,其中派生类定义由repc编译器自动生成。当使用CMake函数或qmake变量来运行repc编译器时,这使得生成过程成为构建过程的一部分。虽然只生成了一个头文件,但它是一个完整的类型。没有公共构造函数,因此需要使用QRemoteObjectNode::acquire模板函数来创建Replica实例。

QRemoteObjectDynamicReplica can be generated at runtime. To do so, you call QRemoteObjectNode::acquireDynamic(), passing in the source name (a QString) as an argument. Dynamic replicas are a bit more verbose to use from C++, but they do not require compilation. Dynamic replicas do not support initial property values, or introspection until they have been initialized.

​可以在运行时生成QRemoteObjectDynamicReplica。为此,调用QRemoteObjectNode::acquireDynamic(),将源名称(QString)作为参数传递。从C++中使用动态副本有点冗长,但它们不需要编译。动态副本在初始化之前不支持初始属性值或自检。

An important difference between these two ways of creating replicas is the behavior before the replica is initialized. Since a dynamic replica only gets a metaObject after initialization, it has basically no API before initialization – no properties, and no signals to connect slots to.

这两种创建副本的方法之间的一个重要区别是副本初始化前的行为。由于动态复制品在初始化后只获得一个metaObject,因此在初始化前基本上没有API——没有属性,也没有连接插槽的信号。

Because metaObjects for compiled replicas are created at compile-time, their API is available when the replica is instantiated. You can even provide default values for properties in the template file, which are used until the replica is initialized with current values from the source.

由于编译副本的元对象是在编译时创建的,因此当副本被实例化时,它们的API是可用的。甚至可以为模板文件中的属性提供默认值,这些值将一直使用,直到使用源中的当前值初始化副本。

Replica Initialization

副本初始化

A host node will share the list of sources that it hosts with every other node that connects to it. This host sends updates when sources are added to or removed from the list. In this way, a connected node will always know what sources it can attach itself to. Changes to a specific source are only propagated to nodes that have a replica of that source. Consequently, this avoids any unnecessary network traffic.

主机节点将与连接到它的每个其他节点共享它所承载的源列表。当源被添加到列表或从列表中删除时,此主机会发送更新。这样,连接的节点将始终知道它可以连接到哪些源。对特定源的更改只会传播到具有该源副本的节点。因此,这避免了任何不必要的网络流量。

When a node acquires a replica for a known source, it sends a request for that source to the host node. Upon receiving this request, the host creates a reply packet with the current values for all properties of that source. If the requested replica is dynamic, the reply packet includes the API definition for the source. From then on, the replica's node will be included in the list of connections that receive changes to that source.

​当节点获取已知源的副本时,它会向主机节点发送对该源的请求。在收到此请求后,主机会创建一个回复包,其中包含该源所有属性的当前值。如果请求的副本是动态的,则回复数据包包括源的API定义。从那时起,副本的节点将被包括在接收该源更改的连接列表中。

If a replica is instantiated but its node is not connected to the node that hosts the requested source – or that object lives in a host node process, but sharing/remoting has not been enabled for the QObject – the Replica will still be created, but remain uninitialized.

​如果复制副本已实例化,但其节点未连接到承载所请求源的节点,或者该对象位于主机节点进程中,但尚未为QObject启用共享/远程处理,则复制副本仍将被创建,但仍未初始化。

If, at a later time, the replica's node gets notified that the requested source is available from a connected node, at that point it will request the source and start the initialization process.

如果稍后副本的节点收到通知,表示所请求的源可以从连接的节点获得,则此时它将请求该源并启动初始化过程。

If the connection to a host node is lost, the replica will transition to the invalid state. It will attempt to reconnect and will re-initialize if the connection is restored; this makes sure all properties are current.

如果与主机节点的连接丢失,副本将转换为无效状态。它将尝试重新连接,如果连接恢复,它将重新初始化;这确保了所有属性都是最新的。

Replica Ownership

副本所有权

The acquire methods return a pointer to the replica QObject instantiated by the node. The node has no way of knowing the replica's intended lifetime. Consequently, when the replica is not longer needed, it is the calling program's responsibility to delete it.

​获取方法返回一个指向节点实例化的副本QObject的指针。节点无法知道副本的预期寿命。因此,当不再需要副本时,调用程序有责任删除它。

You can instantiate multiple copies of the same replica. All replicas of the same source from a single node will share a private data member which handles the network communication. This means multiple instances of a replica do not introduce additional network traffic, although there will be some additional processing overhead. Failing to delete replicas will prevent the reference count on this private object from reaching 0, and cause unnecessary network communication until the calling process exits. For this reason, it is recommended to use QScopedPointer or QSharedPointer to help track the lifetime of replicas.

​可以实例化同一复制副本的多个副本。来自单个节点的同一源的所有副本将共享一个处理网络通信的私有数据成员。这意味着副本的多个实例不会引入额外的网络流量,尽管会有一些额外的处理开销。未能删除副本将阻止此私有对象的引用计数达到0,并导致不必要的网络通信,直到调用进程退出。因此,建议使用QScopedPointer或QSharedPointer来帮助跟踪副本的生命周期。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值