假如DataWriter先起来,并且已经写了一些数据,之后有新的DataReader起来,那么新起来的DataReader能不能接收到它启动之前,DataWriter发布的数据呢。DurabilityQosPolicy用来做这种控制。
VOLATILE_DURABILITY_QOS:易失的,新上线的DataReader只能读取上线之后,DataWriter发布的数据。
TRANSIENT_LOCAL_DURABILITY_QOS:会在内存中保存数据,新上线的DataReader也能读取上线之前的数据,前提是DataWriter还在,如果这个时候DataWriter已经退出了,那么DataReader就无法获取到。
/*** Enum DurabilityQosPolicyKind_t, different kinds of durability for DurabilityQosPolicy.*/
typedef enum DurabilityQosPolicyKind : fastdds::rtps::octet
{/*** The Service does not need to keep any samples of data-instances on behalf of any DataReader that is not* known by the DataWriter at the time the instance is written. In other words the Service will only attempt* to provide the data to existing subscribers*/VOLATILE_DURABILITY_QOS,/*** For TRANSIENT_LOCAL, the service is only required to keep the data in the memory of the DataWriter that* wrote the data and the data is not required to survive the DataWriter.*/TRANSIENT_LOCAL_DURABILITY_QOS,/*** For TRANSIENT, the service is only required to keep the data in memory and not in permanent storage; but* the data is not tied to the lifecycle of the DataWriter and will, in general, survive it.*/TRANSIENT_DURABILITY_QOS,/*** Data is kept on permanent storage, so that they can outlive a system session.** @warning Not Supported*/PERSISTENT_DURABILITY_QOS
} DurabilityQosPolicyKind_t;
1hello world
仍然以fastdds自带的hello world做为例子进行测试。
默认情况下,qos类型为VOLATILE_DURABILITY_QOS,即易失的。如果直接像下边这样进行修改,不生效的,因为DurabilityQosPolicy的使用要同时结合HistoryQosPolicy和ResourceLimitsQosPolicy以及ReliabilityQosPolicy进行统一配置。
- ReliabilityQosPolicyKind需要配置可靠类型
- HistoryQosPolicy配置KEEP_LAST_HISTORY_QOS,则depth需要大于0;或者配置为KEEP_ALL_HISTORY_QOS
- ResourceLimitsQosPolicy中ResourceLimitsQosPolicy不可小于HistoryQosPolicy中的depth
DataWriterQos writer_qos = DATAWRITER_QOS_DEFAULT;
publisher_->get_default_datawriter_qos(writer_qos);auto max_samples = 500;
writer_qos.reliability().kind = ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
writer_qos.durability().kind = DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS;
writer_qos.history().kind = HistoryQosPolicyKind::KEEP_LAST_HISTORY_QOS;
writer_qos.history().depth = max_samples;
writer_qos.resource_limits().ResourceLimitsQosPolicy= max_samples;
writer_qos.resource_limits().max_samples = writer_qos.resource_limits().max_instances * max_samples;
writer_ = publisher_->create_datawriter(topic_, writer_qos, this, StatusMask::all());