ZBLOG

15.8.3.2. DataReader The following is an application example of a DataReader that supports Zero-Copy using the Fast DDS library. As shown in this code snippet, the configuration in the DataReader is similar to the DataWriter. Be sure not to disab...

15.8.3.2. DataReader

以下是一个支持零拷贝的 DataReader 的应用示例,使用了 Fast DDS 库。如代码片段所示,DataReader 中的配置与 DataWriter 类似。请确保不要禁用 DataSharingQosPolicy。AUTO 类型会在可能的情况下自动启用零拷贝。

  1. // 创建参与者
  2. DomainParticipantQos pqos;
  3. pqos.name("Participant_sub");
  4. DomainParticipant* participant = DomainParticipantFactory::get_instance()->create_participant(0, pqos);
  5. // 注册类型
  6. TypeSupport type(new LoanableHelloWorldPubSubType());
  7. type.register_type(participant);
  8. // 创建订阅者
  9. Subscriber* subscriber = participant->create_subscriber(SUBSCRIBER_QOS_DEFAULT, nullptr);
  10. // 创建主题
  11. Topic* topic = participant->create_topic(
  12. "LoanableHelloWorldTopic",
  13. type.get_type_name(),
  14. TOPIC_QOS_DEFAULT);
  15. // 创建读取器
  16. DataReaderQos rqos = subscriber->get_default_datareader_qos();
  17. rqos.history().depth = 10;
  18. rqos.reliability().kind = RELIABLE_RELIABILITY_QOS;
  19. rqos.durability().kind = TRANSIENT_LOCAL_DURABILITY_QOS;
  20. // 必须将 DataSharingQosPolicy 设置为 AUTO(默认)或 ON,以启用零拷贝
  21. rqos.data_sharing().automatic();
  22. DataReader* reader = subscriber->create_datareader(topic, rqos, &datareader_listener);

最后,下面的代码片段实现了 on_data_available()DataReaderListener 回调。该函数中的关键点如下:

  • 声明和处理 LoanableSequence
  • 检查 DataReader::is_sample_valid() 以验证样本未被替换。有关更多信息,请参考 DataReaderDataWriter 历史关联。
  • 使用 DataReader::return_loan() 函数向 DataReader 指示应用程序已完成对序列的访问。
  1. void on_data_available(
  2. eprosima::fastdds::dds::DataReader* reader) override
  3. {
  4. // 为数据类型声明 LoanableSequence
  5. FASTDDS_SEQUENCE(DataSeq, LoanableHelloWorld);
  6. DataSeq data;
  7. SampleInfoSeq infos;
  8. // 访问数据样本集合及其对应的 SampleInfo 结构集合
  9. while (RETCODE_OK == reader->take(data, infos))
  10. {
  11. // 遍历 SampleInfo 序列中的每个 LoanableCollection
  12. for (LoanableCollection::size_type i = 0; i < infos.length(); ++i)
  13. {
  14. // 检查数据样本是否包含数据,或仅用于传达实例中的变化
  15. if (infos[i].valid_data)
  16. {
  17. // 打印数据。
  18. const LoanableHelloWorld& sample = data[i];
  19. ++samples;
  20. std::cout << "Sample received (count=" << samples
  21. << ") at address " << &sample
  22. << (reader->is_sample_valid(&sample,
  23. &infos[i]) ? " is valid" : " was replaced" ) << std::endl
  24. << " index=" << sample.index() << std::endl
  25. << " message=" << sample.message().data() << std::endl;
  26. }
  27. }
  28. // 向 DataReader 指示应用程序已完成对之前通过 read 或 take 获取的数据值和 SampleInfo 集合的访问。
  29. reader->return_loan(data, infos);
  30. }
  31. }

此代码展示了如何设置和使用 Fast DDS 库中的零拷贝特性,以及如何正确地管理内存和验证接收到的数据有效性。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=21879

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?