• Fast DDS之Publisher


    在这里插入图片描述

    数据结构

    WriterProxyData,包含以下成员:

    GUID_t m_guid;
    RemoteLocatorList remote_locators_;  //!Holds locator information
    InstanceHandle_t m_key;  //!GUID_t of the Writer converted to InstanceHandle_t
    InstanceHandle_t m_RTPSParticipantKey;  //!GUID_t of the participant converted to InstanceHandle
    string_255 m_typeName;  //!Type name
    string_255 m_topicName;  //!Topic name
    uint16_t m_userDefinedId;  //!User defined ID
    uint32_t m_typeMaxSerialized;  //!Maximum size of the type associated with this Wrtiter, serialized.
    TopicKind_t m_topicKind; //!Topic kind
    GUID_t persistence_guid_;  //!Persistence GUID
    TypeIdV1* m_type_id;  //!Type Identifier
    TypeObjectV1* m_type;  //!Type Object
    xtypes::TypeInformation* m_type_information;   //!Type Information
    ParameterPropertyList_t m_properties;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Publisher

    Publisher是一直归属于DomainParticipant。

    1. 通过DomainParticipant的create_publisher创建Publisher,参数PublisherQos是必需的,可以使用默认值PUBLISHER_QOS_DEFAULT。可选的参数:PublisherListener和StatusMask
    Publisher* publisher_with_default_qos =
            participant->create_publisher(PUBLISHER_QOS_DEFAULT);
    if (nullptr == publisher_with_default_qos) {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 基于Profile创建Publisher,参数PublisherQos是必需的,可以使用默认值PUBLISHER_QOS_DEFAULT。可选的参数:PublisherListener和StatusMask
    // First load the XML with the profiles
    DomainParticipantFactory::get_instance()->load_XML_profiles_file("profiles.xml");
    // Create participant
    // Create a Publisher using a profile and no Listener
    Publisher* publisher_with_profile = participant->create_publisher_with_profile("publisher_profile");
    if (nullptr == publisher_with_profile) {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    1. 删除Publisher
      删除Publisher前需要先删除所有属于Publisher的实体(DataWriters),可以使用delete_contained_entities()。
    // Delete the entities the Publisher created.
    if (publisher->delete_contained_entities() != ReturnCode_t::RETCODE_OK) {
        // Publisher failed to delete the entities it created.
        return;
    }
    
    // Delete the Publisher
    if (participant->delete_publisher(publisher) != ReturnCode_t::RETCODE_OK) {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    DataWriter

    DataWriter归属于Publisher,对DataWriter来说,Publisher就是一个创建工厂。每个DataWriter在创建之处绑定到一个Topic,所以Topic必须先于DataWriter创建。
    默认的DataWriterQos可以通过Publisher实例函数get_default_datawriter_qos()获取
    DataWriterListener是一个抽象类,用于响应DataWriter的状态发生变化,有以下成员函数用于回调:

    • on_publication_matched()
    • on_offered_deadline_missed()
    • on_offered_incompatible_qos()
    • on_liveliness_lost()
    • on_unacknowledged_sample_removed()
      创建DataWriter:使用Publisher的函数create_datawriter()函数创建。必选的参数为绑定到要发送数据的Topic和DataWriterQos,可选的参数为继承自DataWriterListener和StatusMask
    DataWriter* data_writer_with_default_qos = publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT);
    if (nullptr == data_writer_with_default_qos) {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过Profile创建:

    // First load the XML with the profiles
    DomainParticipantFactory::get_instance()->load_XML_profiles_file("profiles.xml");
    
    // Create a DataWriter using a profile and no Listener
    DataWriter* data_writer_with_profile = publisher->create_datawriter_with_profile(topic, "data_writer_profile");
    if (nullptr == data_writer_with_profile) {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    通过自定义PayloadPool创建DataWriter

    // A DataWriterQos must be provided to the creation method
    DataWriterQos qos;
    
    // Create PayloadPool
    std::shared_ptr<eprosima::fastrtps::rtps::IPayloadPool> payload_pool =
            std::dynamic_pointer_cast<eprosima::fastrtps::rtps::IPayloadPool>(std::make_shared<CustomPayloadPool>());
    
    DataWriter* data_writer = publisher->create_datawriter(topic, qos, nullptr, StatusMask::all(), payload_pool);
    if (nullptr == data_writer) {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    删除DataWriter

    // Delete the DataWriter
    if (publisher->delete_datawriter(data_writer) != ReturnCode_t::RETCODE_OK) {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    发送数据
    write()函数有两个参数:指向要发送数值的data实例;instance的handler。如果handle是非空,那么它必须要被TypeSupport的getKey()获取到,否则write函数会失败。

    // Publish the new value, deduce the instance handle
    if (data_writer->write(data, eprosima::fastrtps::rtps::InstanceHandle_t()) != ReturnCode_t::RETCODE_OK) {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    减少发送数据的拷贝:通过loan sample,步骤如下:

    1. 通过 loan_sample()函数获取loaned sample的引用
    2. 使用上一步获取的引用构建data
    3. write发送
      如果loan_sample()被调用但sample未被发送,必须要使用discard_loan()释放。
    void* data = nullptr;
    if (ReturnCode_t::RETCODE_OK == data_writer->loan_sample(data)) {
        bool error = false;
    
        // Fill the data values
        // (...)
    
        if (error) {
            // Return the loan without publishing
            data_writer->discard_loan(data);
            return;
        }
    
        // Publish the new value
        if (data_writer->write(data, eprosima::fastrtps::rtps::InstanceHandle_t()) != ReturnCode_t::RETCODE_OK) {
            // Error
            return;
        }
    }
    
    // The data instance can be reused to publish new values,
    // but delete it at the end to avoid leaks
    custom_type_support->deleteData(data)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    Golang 实现word和Excel处理
    Linux系统编程系列之条件变量
    第三十二章 管理许可(五)
    码农必备?清华大学开源了一款写代码神器。。。
    Docker Compose
    让自定义的容器,也能基于范围循环
    ubuntu16.4 anaconda安装TensorRT
    SIM卡相关知识介绍
    .NET WebAPI 自定义 NullableConverter 解决可为空类型字段入参“”空字符触发转换异常问题
    【无标题】
  • 原文地址:https://blog.csdn.net/u010378559/article/details/133924601