一、PublisherOptions & SubscriberOptions
1、PublisherOptions
/// @brief This struct is used to configure the publisher
struct PublisherOptions
{
/// @brief The size of the history chunk queue
uint64_t historyCapacity{0U};
/// @brief The name of the node where the publisher should belong to
iox::NodeName_t nodeName{""};
/// @brief The option whether the publisher should already be offered when creating it
bool offerOnCreate{true};
/// @brief The option whether the publisher should block when the subscriber queue is full
ConsumerTooSlowPolicy subscriberTooSlowPolicy{ConsumerTooSlowPolicy::DISCARD_OLDEST_DATA};
/// @brief serialization of the PublisherOptions
cxx::Serialization serialize() const noexcept;
/// @brief deserialization of the PublisherOptions
static cxx::expected<PublisherOptions, cxx::Serialization::Error>
deserialize(const cxx::Serialization& serialized) noexcept;
};
详细解释
-
historyCapacity:历史数据块队列的大小。这决定了发布者在发布数据时保留的历史数据块数量。
-
nodeName:发布者所属节点的名称。
-
offerOnCreate:一个布尔选项,用于指示发布者在创建时是否已经提供数据。
-
subscriberTooSlowPolicy:当订阅者队列已满时,发布者是否应该阻塞的选项。默认行为是丢弃最旧的数据。
-
serialize():发布者选项的序列化方法,用于将
PublisherOptions转换为可存储或传输的格式。 -
deserialize():发布者选项的反序列化方法,用于将序列化的数据转换回
PublisherOptions对象。
这些注释和变量用于配置一个发布者的行为和属性,确保在创建和使用发布者时能够正确处理数据发布和队列管理。
PublisherOptions 示例
#include "iceoryx_posh/popo/publisher.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iceoryx_utils/cxx/optional.hpp"
#include <iostream>
struct MyData
{
int value;
};
int main()
{
iox::runtime::PoshRuntime::initRuntime("publisher");
// 配置 PublisherOptions
iox::popo::PublisherOptions publisherOptions;
publisherOptions.historyCapacity = 10; // 设置历史缓冲区容量
publisherOptions.nodeName = "MyNode"; // 设置节点名称 // 创建 Publisher
iox::popo::Publisher<MyData> publisher({"Example", "Publisher", "MyData"}, publisherOptions);
// 发布数据
for (int i = 0; i < 10; ++i)
{
publisher.loan()
.and_then([&](auto& sample) {
sample->value = i;
sample.publish();
})
.or_else([](auto& error) {
std::cerr << "Could not loan sample! Error: " << error << std::endl;
});
} return 0;
}
2、SubscriberOptions
/// @brief This struct is used to configu

最低0.47元/天 解锁文章
2096

被折叠的 条评论
为什么被折叠?



