DDS_Bridge
一个基于Fast DDS的DDS桥接库
载入中...
搜索中...
未找到
publisher.hpp
浏览该文件的文档.
1
10#pragma once
11#include <fastdds/dds/publisher/Publisher.hpp>
12#include <fastdds/dds/publisher/DataWriter.hpp>
13#include <fastdds/dds/topic/Topic.hpp>
14#include <fastdds/dds/topic/TypeSupport.hpp>
15
16
17namespace zclcpp
18{
19 class Node;
20
26 template <typename MsgPubSubType>
28 {
29 public:
31 using MsgT = typename MsgPubSubType::type;
32
34 using SharedPtr = std::shared_ptr<Publisher<MsgPubSubType>>;
35
43 Publisher(std::shared_ptr<Node> node,
44 const std::string& topic_name,
45 int qos_depth)
46 : node_(node)
47 {
48 // 1. 注册类型
49 type_.reset(new MsgPubSubType());
50 type_.register_type(node_->participant());
51
52 // 2. Topic
53 //check if topic already exists
54 topic_ = node_->participant()->find_topic(topic_name, eprosima::fastdds::dds::Duration_t(1));
55 if (topic_ == nullptr)
56 {
57 topic_ = node_->participant()->create_topic(
58 topic_name, type_.get_type_name(),
59 eprosima::fastdds::dds::TOPIC_QOS_DEFAULT);
60 }
61
62 // 3. Publisher / DataWriter
63 pub_ = node_->participant()->create_publisher(
64 eprosima::fastdds::dds::PUBLISHER_QOS_DEFAULT, nullptr);
65
66 eprosima::fastdds::dds::DataWriterQos wqos;
67 wqos.history().kind = eprosima::fastdds::dds::KEEP_LAST_HISTORY_QOS;
68 wqos.history().depth = qos_depth;
69 writer_ = pub_->create_datawriter(topic_, wqos);
70 }
71
77 void publish(const typename MsgPubSubType::type& msg)
78 {
79 writer_->write(const_cast<MsgT*>(&msg));
80 }
81
82 private:
83 std::shared_ptr<Node> node_;
84 eprosima::fastdds::dds::TypeSupport type_;
85 eprosima::fastdds::dds::Topic* topic_{ nullptr };
86 eprosima::fastdds::dds::Publisher* pub_{ nullptr };
87 eprosima::fastdds::dds::DataWriter* writer_{ nullptr };
88 };
89};
Represents a DDS node.
定义 node.hpp:31
std::shared_ptr< Publisher< MsgPubSubType > > SharedPtr
Shared pointer type for Publisher.
定义 publisher.hpp:34
typename MsgPubSubType::type MsgT
Message type for Publisher.
定义 publisher.hpp:31
Publisher(std::shared_ptr< Node > node, const std::string &topic_name, int qos_depth)
Construct a new Publisher object, it is recommended to use the create_publisher() method of the Node ...
定义 publisher.hpp:43
void publish(const typename MsgPubSubType::type &msg)
Publish a message.
定义 publisher.hpp:77
Namespace for zzs communication library.
定义 node.hpp:18