• dds:publish


    Publish

    发布由DataWriter 与Publisher的关联定义。要开始发布数据实例的值,应用程序会在 Publisher 中创建一个新的 DataWriter。此 DataWriter 将绑定到 描述正在传输的数据类型的主题。与此 Topic 匹配的远程订阅将能够接收来自 DataWriter 的数据值更新
    在这里插入图片描述

    Publisher

    Publisher代表属于它的一个或多个DataWriter对象。它作为一个容器,允许在Publisher的 PublisherQos 给定的公共配置下对不同的 DataWriter 对象进行分组。

    属于同一个 Publisher 的 DataWriter 对象之间除了 Publisher 的 PublisherQos 之外没有任何其他关系,否则独立运行。具体来说,发布者可以为不同的主题 和数据类型托管 DataWriter 对象。

    PublisherQos

    PublisherQos控制Publisher. 它在内部包含以下QosPolicy对象:
    在这里插入图片描述

    QosPolicy有关它们的用法和默认值的更多信息,请参阅每个类的详细说明。

    之前创建的 Publisher 的 QoS 值可以使用 Publisher::set_qos()成员函数进行修改。

    // Create a DomainParticipant in the desired domain
    DomainParticipant* participant =
            DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
    if (nullptr == participant)
    {
        // Error
        return;
    }
    
    // Create a Publisher with default PublisherQos
    Publisher* publisher =
            participant->create_publisher(PUBLISHER_QOS_DEFAULT);
    if (nullptr == publisher)
    {
        // Error
        return;
    }
    
    // Get the current QoS or create a new one from scratch
    PublisherQos qos = publisher->get_qos();
    
    // Modify QoS attributes
    // (...)
    
    // Assign the new Qos to the object
    publisher->set_qos(qos);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    默认 PublisherQos

    默认的PublisherQosget_default_publisher_qos()是指DomainParticipant 实例上的成员函数返回的值 。该特殊值PUBLISHER_QOS_DEFAULT可用作 QoS 参数 create_publisher()或Publisher::set_qos()成员函数,以指示应使用当前默认的 PublisherQos。

    系统启动时,默认的 PublisherQos 等价于默认的构造值PublisherQos()。可以使用DomainParticipant实例set_default_publisher_qos()上的成员函数随时修改默认 PublisherQos 。修改默认的 PublisherQos 不会影响已经存在的 Publisher实例。

    // Create a DomainParticipant in the desired domain
    DomainParticipant* participant =
            DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
    if (nullptr == participant)
    {
        // Error
        return;
    }
    
    // Get the current QoS or create a new one from scratch
    PublisherQos qos_type1 = participant->get_default_publisher_qos();
    
    // Modify QoS attributes
    // (...)
    
    // Set as the new default PublisherQos
    if (participant->set_default_publisher_qos(qos_type1) != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    // Create a Publisher with the new default PublisherQos.
    Publisher* publisher_with_qos_type1 =
            participant->create_publisher(PUBLISHER_QOS_DEFAULT);
    if (nullptr == publisher_with_qos_type1)
    {
        // Error
        return;
    }
    
    // Get the current QoS or create a new one from scratch
    PublisherQos qos_type2;
    
    // Modify QoS attributes
    // (...)
    
    // Set as the new default PublisherQos
    if (participant->set_default_publisher_qos(qos_type2) != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    // Create a Publisher with the new default PublisherQos.
    Publisher* publisher_with_qos_type2 =
            participant->create_publisher(PUBLISHER_QOS_DEFAULT);
    if (nullptr == publisher_with_qos_type2)
    {
        // Error
        return;
    }
    
    // Resetting the default PublisherQos to the original default constructed values
    if (participant->set_default_publisher_qos(PUBLISHER_QOS_DEFAULT)
            != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    // The previous instruction is equivalent to the following
    if (participant->set_default_publisher_qos(PublisherQos())
            != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    set_default_publisher_qos()成员函数也接受特殊值 PUBLISHER_QOS_DEFAULT作为输入参数。这会将当前默认 PublisherQos 重置为默认构造值PublisherQos()。

    // Create a DomainParticipant in the desired domain
    DomainParticipant* participant =
            DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
    if (nullptr == participant)
    {
        // Error
        return;
    }
    
    // Create a custom PublisherQos
    PublisherQos custom_qos;
    
    // Modify QoS attributes
    // (...)
    
    // Create a publisher with a custom PublisherQos
    Publisher* publisher = participant->create_publisher(custom_qos);
    if (nullptr == publisher)
    {
        // Error
        return;
    }
    
    // Set the QoS on the publisher to the default
    if (publisher->set_qos(PUBLISHER_QOS_DEFAULT) != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    // The previous instruction is equivalent to the following:
    if (publisher->set_qos(participant->get_default_publisher_qos())
            != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    笔记

    该值PUBLISHER_QOS_DEFAULT具有不同的含义,具体取决于它的使用位置:

    create_publisher()、Publisher::set_qos()它指的是默认的 PublisherQos。作为返回get_default_publisher_qos()。

    set_default_publisher_qos()它指的是默认构造 PublisherQos()的.

    PublisherListener

    PublisherListener是一个抽象类,定义了将响应Publisher上的状态更改而触发的回调。默认情况下,所有这些回调都是空的并且什么都不做。用户应实现此类的特化,覆盖应用程序所需的回调。未被覆盖的回调将保持其空实现。

    PublisherListener继承自DataWriterListener。因此,它能够对报告给 DataWriter的所有事件做出反应。由于事件总是被通知给可以处理事件的最具体的实体侦听器,因此PublisherListener从 DataWriterListener 继承的回调只有在触发的 DataWriter 没有附加侦听器时才会被调用,或者如果回调被StatusMaskDataWriter 上的禁用。

    PublisherListener不添加任何新的回调。请参阅DataWriterListener以获取继承的回调和覆盖示例的列表。

    创建发布者

    Publisher始终属于DomainParticipant 。Publisher 的创建是通过create_publisher()DomainParticipant 实例上的成员函数完成的,该实例充当 Publisher 的工厂。

    强制性论点是:

    PublisherQos描述 Publisher的行为。如果提供PUBLISHER_QOS_DEFAULT的值为 ,则使用Default PublisherQos的值。

    可选参数是:

    一个从PublisherListener派生的监听器,实现将触发的回调,以响应 Publisher 上的事件和状态更改。默认情况下使用空回调。

    StatusMask激活或停用在 PublisherListener 上触发单个回调的A。默认情况下,所有事件都已启用。

    create_publisher()如果在操作过程中出现错误,将返回一个空指针,例如,如果提供的 QoS 不兼容或不受支持。建议检查返回值是否为有效指针。

    // Create a DomainParticipant in the desired domain
    DomainParticipant* participant =
            DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
    if (nullptr == participant)
    {
        // Error
        return;
    }
    
    // Create a Publisher with default PublisherQos and no Listener
    // The value PUBLISHER_QOS_DEFAULT is used to denote the default QoS.
    Publisher* publisher_with_default_qos =
            participant->create_publisher(PUBLISHER_QOS_DEFAULT);
    if (nullptr == publisher_with_default_qos)
    {
        // Error
        return;
    }
    
    // A custom PublisherQos can be provided to the creation method
    PublisherQos custom_qos;
    
    // Modify QoS attributes
    // (...)
    
    Publisher* publisher_with_custom_qos =
            participant->create_publisher(custom_qos);
    if (nullptr == publisher_with_custom_qos)
    {
        // Error
        return;
    }
    
    // Create a Publisher with default QoS and a custom Listener.
    // CustomPublisherListener inherits from PublisherListener.
    // The value PUBLISHER_QOS_DEFAULT is used to denote the default QoS.
    CustomPublisherListener custom_listener;
    Publisher* publisher_with_default_qos_and_custom_listener =
            participant->create_publisher(PUBLISHER_QOS_DEFAULT, &custom_listener);
    if (nullptr == publisher_with_default_qos_and_custom_listener)
    {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    基于配置文件的发布者创建

    除了使用PublisherQos,配置文件的名称可用于创建具有create_publisher_with_profile() DomainParticipant 实例上的成员函数的 Publisher。

    强制性论点是:

    具有标识发布者的名称的字符串。

    可选参数是:

    一个从PublisherListener派生的监听器,实现将触发的回调,以响应 Publisher 上的事件和状态更改。默认情况下使用空回调。

    StatusMask激活或停用在 PublisherListener 上触发单个回调的A。默认情况下,所有事件都已启用。

    create_publisher_with_profile()如果在操作过程中出现错误,将返回一个空指针,例如,如果提供的 QoS 不兼容或不受支持。建议检查返回值是否为有效指针。

    笔记

    XML 配置文件必须先前已加载。请参阅从 XML 文件加载配置文件。

    // First load the XML with the profiles
    DomainParticipantFactory::get_instance()->load_XML_profiles_file("profiles.xml");
    
    // Create a DomainParticipant in the desired domain
    DomainParticipant* participant =
            DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
    if (nullptr == participant)
    {
        // Error
        return;
    }
    
    // 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;
    }
    
    // Create a Publisher using a profile and a custom Listener.
    // CustomPublisherListener inherits from PublisherListener.
    CustomPublisherListener custom_listener;
    Publisher* publisher_with_profile_and_custom_listener =
            participant->create_publisher_with_profile("publisher_profile", &custom_listener);
    if (nullptr == publisher_with_profile_and_custom_listener)
    {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    删除发布者

    可以使用delete_publisher()创建 Publisher 的 DomainParticipant 实例上的成员函数删除 Publisher。

    笔记

    仅当属于发布者 (DataWriters) 的所有实体都已被删除时,才能删除发布者。否则函数会报错,Publisher 不会被删除。这可以通过使用Publisherdelete_contained_entities()的成员函数 来执行。

    // Create a DomainParticipant in the desired domain
    DomainParticipant* participant =
            DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
    if (nullptr == participant)
    {
        // Error
        return;
    }
    
    // Create a Publisher
    Publisher* publisher =
            participant->create_publisher(PUBLISHER_QOS_DEFAULT);
    if (nullptr == publisher)
    {
        // Error
        return;
    }
    
    // Use the Publisher to communicate
    // (...)
    
    // 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
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    数据写入器

    DataWriter恰好附加到一个充当它的工厂的Publisher上。此外,每个 DataWriter自创建以来都绑定到一个Topic 。该 Topic 必须在创建 DataWriter 之前存在,并且必须绑定到 DataWriter 要发布的数据类型。

    在 Publisher 中为特定 Topic 创建新 DataWriter 的效果是使用 Topic 描述的名称和数据类型启动新发布。

    一旦创建了 DataWriter,应用程序就可以使用 write()DataWriter 上的成员函数来通知数据值的变化。这些更改将传输到与此出版物匹配的所有订阅

    数据写入器Qos

    DataWriterQos控制 DataWriter 的行为。它在内部包含以下QosPolicy对象
    在这里插入图片描述
    之前创建的 DataWriter 的 QoS 值可以使用 DataWriter::set_qos()成员函数进行修改。

    // Create a DataWriter with default DataWriterQos
    DataWriter* data_writer =
            publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT);
    if (nullptr == data_writer)
    {
        // Error
        return;
    }
    
    // Get the current QoS or create a new one from scratch
    DataWriterQos qos = data_writer->get_qos();
    
    // Modify QoS attributes
    // (...)
    
    // Assign the new Qos to the object
    data_writer->set_qos(qos);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    默认 DataWriterQos

    默认的DataWriterQosget_default_datawriter_qos()是指Publisher 实例上的成员函数返回的值 。该特殊值DATAWRITER_QOS_DEFAULT可用作 QoS 参数create_datawriter() 或DataWriter::set_qos()成员函数,以指示应使用当前默认的 DataWriterQos。

    系统启动时,默认的 DataWriterQos 等价于默认的构造值DataWriterQos()。set_default_datawriter_qos()可以使用Publisher 实例上的成员函数随时修改默认 DataWriterQos 。修改默认 DataWriterQos 不会影响已经存在的 DataWriter 实例。

    // Get the current QoS or create a new one from scratch
    DataWriterQos qos_type1 = publisher->get_default_datawriter_qos();
    
    // Modify QoS attributes
    // (...)
    
    // Set as the new default DataWriterQos
    if (publisher->set_default_datawriter_qos(qos_type1) != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    // Create a DataWriter with the new default DataWriterQos.
    DataWriter* data_writer_with_qos_type1 =
            publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT);
    if (nullptr == data_writer_with_qos_type1)
    {
        // Error
        return;
    }
    
    // Get the current QoS or create a new one from scratch
    DataWriterQos qos_type2;
    
    // Modify QoS attributes
    // (...)
    
    // Set as the new default DataWriterQos
    if (publisher->set_default_datawriter_qos(qos_type2) != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    // Create a DataWriter with the new default DataWriterQos.
    DataWriter* data_writer_with_qos_type2 =
            publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT);
    if (nullptr == data_writer_with_qos_type2)
    {
        // Error
        return;
    }
    
    // Resetting the default DataWriterQos to the original default constructed values
    if (publisher->set_default_datawriter_qos(DATAWRITER_QOS_DEFAULT)
            != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    // The previous instruction is equivalent to the following
    if (publisher->set_default_datawriter_qos(DataWriterQos())
            != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    set_default_datawriter_qos()成员函数也接受特殊值DATAWRITER_QOS_DEFAULT 作为输入参数。这会将当前默认 DataWriterQos 重置为默认构造值DataWriterQos()。

    // Create a custom DataWriterQos
    DataWriterQos custom_qos;
    
    // Modify QoS attributes
    // (...)
    
    // Create a DataWriter with a custom DataWriterQos
    DataWriter* data_writer = publisher->create_datawriter(topic, custom_qos);
    if (nullptr == data_writer)
    {
        // Error
        return;
    }
    
    // Set the QoS on the DataWriter to the default
    if (data_writer->set_qos(DATAWRITER_QOS_DEFAULT) != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    // The previous instruction is equivalent to the following:
    if (data_writer->set_qos(publisher->get_default_datawriter_qos())
            != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    该值DATAWRITER_QOS_DEFAULT具有不同的含义,具体取决于它的使用位置:

    create_datawriter() 和DataWriter::set_qos()它指的是由 . 返回的默认 DataWriterQos get_default_datawriter_qos()。

    set_default_datawriter_qos()它指的是默认构造 DataWriterQos()的.

    数据写入器监听器

    DataWriterListener是一个抽象类,定义了将响应DataWriter上的状态更改而触发的回调。默认情况下,所有这些回调都是空的并且什么都不做。用户应实现此类的特化,覆盖应用程序所需的回调。未被覆盖的回调将保持其空实现。

    DataWriterListener定义以下回调:

    on_publication_matched(): DataWriter 找到了 与Topic匹配且具有公共分区和兼容 QoS 的DataReader,或者已停止与之前认为匹配的 DataReader 匹配。

    on_offered_deadline_missed(): DataWriter 未能在其DataWriterQos上配置的期限内提供数据。它将在 DataWriter 未能为其提供数据的每个截止期限和数据实例中调用。
    on_offered_incompatible_qos(): DataWriter 找到了一个 DataReader,它与 Topic 匹配并且具有公共分区,但请求的 QoS 与 DataWriter 上定义的 QoS 不兼容。

    on_liveliness_lost():DataWriter 不尊重其 DataWriterQos 上的 liveliness 配置,因此,DataReader 实体将认为 DataWriter 不再活动

    class CustomDataWriterListener : public DataWriterListener
    {
    
    public:
    
        CustomDataWriterListener()
            : DataWriterListener()
        {
        }
    
        virtual ~CustomDataWriterListener()
        {
        }
    
        virtual void on_publication_matched(
                DataWriter* writer,
                const PublicationMatchedStatus& info)
        {
            (void)writer
            ;
            if (info.current_count_change == 1)
            {
                std::cout << "Matched a remote Subscriber for one of our Topics" << std::endl;
            }
            else if (info.current_count_change == -1)
            {
                std::cout << "Unmatched a remote Subscriber" << std::endl;
            }
        }
    
        virtual void on_offered_deadline_missed(
                DataWriter* writer,
                const OfferedDeadlineMissedStatus& status)
        {
            (void)writer, (void)status;
            std::cout << "Some data could not be delivered on time" << std::endl;
        }
    
        virtual void on_offered_incompatible_qos(
                DataWriter* /*writer*/,
                const OfferedIncompatibleQosStatus& status)
        {
            std::cout << "Found a remote Topic with incompatible QoS (QoS ID: " << status.last_policy_id <<
                ")" << std::endl;
        }
    
        virtual void on_liveliness_lost(
                DataWriter* writer,
                const LivelinessLostStatus& status)
        {
            (void)writer, (void)status;
            std::cout << "Liveliness lost. Matched Subscribers will consider us offline" << std::endl;
        }
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    创建数据写入器

    DataWriter始终属于Publisher。DataWriter 的创建是通过create_datawriter()Publisher 实例上的成员函数完成的,它充当 DataWriter 的工厂。

    强制性论点是:

    绑定到将要传输的数据类型的主题。

    描述DataWriter行为的 DataWriterQos 。如果提供DATAWRITER_QOS_DEFAULT的值为 ,则使用Default DataWriterQos的值。

    可选参数是:

    派生自DataWriterListener的侦听器,实现将触发的回调,以响应 DataWriter 上的事件和状态更改。默认情况下使用空回调。

    StatusMask激活或取消激活 DataWriterListener 上各个回调的触发。默认情况下,所有事件都已启用。

    create_datawriter()如果在操作过程中出现错误,将返回一个空指针,例如,如果提供的 QoS 不兼容或不受支持。建议检查返回值是否为有效指针。

    // Create a DataWriter with default DataWriterQos and no Listener
    // The value DATAWRITER_QOS_DEFAULT is used to denote the default QoS.
    DataWriter* data_writer_with_default_qos =
            publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT);
    if (nullptr == data_writer_with_default_qos)
    {
        // Error
        return;
    }
    
    // A custom DataWriterQos can be provided to the creation method
    DataWriterQos custom_qos;
    
    // Modify QoS attributes
    // (...)
    
    DataWriter* data_writer_with_custom_qos =
            publisher->create_datawriter(topic, custom_qos);
    if (nullptr == data_writer_with_custom_qos)
    {
        // Error
        return;
    }
    
    // Create a DataWriter with default QoS and a custom Listener.
    // CustomDataWriterListener inherits from DataWriterListener.
    // The value DATAWRITER_QOS_DEFAULT is used to denote the default QoS.
    CustomDataWriterListener custom_listener;
    DataWriter* data_writer_with_default_qos_and_custom_listener =
            publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT, &custom_listener);
    if (nullptr == data_writer_with_default_qos_and_custom_listener)
    {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    基于配置文件的 DataWriter 创建

    除了使用 DataWriterQos,配置文件的名称可用于创建具有create_datawriter_with_profile() Publisher 实例上的成员函数的 DataWriter。

    强制性论点是:

    绑定到将要传输的数据类型的主题。

    具有标识 DataWriter 的名称的字符串。

    可选参数是:

    派生自 DataWriterListener 的侦听器,实现将触发的回调,以响应 DataWriter 上的事件和状态更改。默认情况下使用空回调。

    StatusMask激活或取消激活 DataWriterListener 上各个回调的触发。默认情况下,所有事件都已启用。

    create_datawriter_with_profile()如果在操作过程中出现错误,将返回一个空指针,例如,如果提供的 QoS 不兼容或不受支持。建议检查返回值是否为有效指针。

    // 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;
    }
    
    // Create a DataWriter using a profile and a custom Listener.
    // CustomDataWriterListener inherits from DataWriterListener.
    CustomDataWriterListener custom_listener;
    DataWriter* data_writer_with_profile_and_custom_listener =
            publisher->create_datawriter_with_profile(topic, "data_writer_profile", &custom_listener);
    if (nullptr == data_writer_with_profile_and_custom_listener)
    {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    删除数据写入器

    可以使用创建 DataWriter 的Publisher实例delete_datawriter()上的成员函数 删除 DataWriter。

    
    // Create a DataWriter
    DataWriter* data_writer =
            publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT);
    if (nullptr == data_writer)
    {
        // Error
        return;
    }
    
    // Use the DataWriter to communicate
    // (...)
    
    // Delete the DataWriter
    if (publisher->delete_datawriter(data_writer) != ReturnCode_t::RETCODE_OK)
    {
        // Error
        return;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    发布数据

    用户通过DataWriterwrite()上的成员函数 通知数据实例值的变化。然后,此更改将传达 给与 DataWriter 匹配的每个DataReader 。作为副作用,此操作断言 DataWriter 本身、Publisher和DomainParticipant的活跃度。

    该函数有两个参数:

    指向具有新值的数据实例的指针。

    实例的处理程序。

    空的(即默认构造的InstanceHandle_t)实例处理程序可用于参数句柄。这表明应该从实例数据的键中自动推导出实例的身份。或者,write()重载成员函数以仅获取指向数据实例的指针,这将始终从实例数据的键中推断出身份。

    如果句柄不为空,那么它必须对应于实例的 获得getKey()的 值。TypeSupport否则 write 函数将失败并显示RETCODE_PRECONDITION_NOT_MET.

    // Register the data type in the DomainParticipant.
    TypeSupport custom_type_support(new CustomDataType());
    custom_type_support.register_type(participant, custom_type_support.get_type_name());
    
    // Create a Topic with the registered type.
    Topic* custom_topic =
            participant->create_topic("topic_name", custom_type_support.get_type_name(), TOPIC_QOS_DEFAULT);
    if (nullptr == custom_topic)
    {
        // Error
        return;
    }
    
    // Create a DataWriter
    DataWriter* data_writer =
            publisher->create_datawriter(custom_topic, DATAWRITER_QOS_DEFAULT);
    if (nullptr == data_writer)
    {
        // Error
        return;
    }
    
    // Get a data instance
    void* data = custom_type_support->createData();
    
    // Fill the data values
    // (...)
    
    // Publish the new value, deduce the instance handle
    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
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    写操作的阻塞

    RELIABLE如果在DataWriterQos上将可靠性类型设置为,则write()操作可能会阻塞。具体来说,如果已达到配置的资源限制中指定的限制,则 write()操作将阻止等待空间变得可用。在这些情况下,可靠性max_blocking_time配置了写操作可能阻塞等待的最长时间。如果max_blocking_time在 DataWriter 能够在不超过限制的情况下存储修改之前经过,则写入操作将失败并返回TIMEOUT.

    借用数据缓冲区

    当用户write()使用新的样本值调用时,数据会从给定样本复制到 DataWriter 的内存中。对于大型数据类型,此副本可能会消耗大量时间和内存资源。相反,DataWriter 可以将其内存中的样本借给用户,并且用户可以使用所需的值填充此样本。当write()使用这种借出的样本调用时,DataWriter 不会复制其内容,因为它已经拥有缓冲区。

    要在出版物中使用借出的数据样本,请执行以下步骤:

    使用 获取对借出样本的引用loan_sample()。

    使用参考来构建数据样本。

    使用 编写示例write()。

    一旦write()被借出的样品调用,该贷款被视为已退回,并且对样品的内容进行任何更改都是不安全的。

    如果调用函数loan_sample()但从未写入样本,则必须使用discard_loan(). 否则 DataWriter 可能会用完样本。

     // Borrow a data instance
        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
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    高效工具-常用
    MySQL 慢查询
    openGauss每日一练第4天 | openGauss中一个数据库可以被多个用户访问
    C#面:.NET默认的委托类型有哪几种?
    MongoDB CRUD操作:可重试写入
    OEEL高阶图表——对比2000和2017年全球不同类型发电占比柱状图
    【2023】windows下安装libevent
    《好笑的爱》阅读笔记
    【杂记】全栈开发中碰到的一些问题及解决方法
    资深测试面试-参考一下
  • 原文地址:https://blog.csdn.net/neuzhangno/article/details/127775756