Fast DDS之Publisher

目录

  • Publisher
  • DataWriter

Fast DDS之Publisher_第1张图片

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. 基于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. 删除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;
}

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;
}

通过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;
}

通过自定义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;
}

删除DataWriter

// Delete the DataWriter
if (publisher->delete_datawriter(data_writer) != ReturnCode_t::RETCODE_OK) {
    // Error
    return;
}

发送数据
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;
}

减少发送数据的拷贝:通过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)

你可能感兴趣的:(Fast,DDS,数据库,网络,中间件,c++,linux,去中心化)