Fast-DDS 源码详细分析

Fast-DDS 源码详细分析

工作区路径:/home/cp/work2/ros2Learn/ros2_humble/src/eProsima/Fast-DDS
版本:2.6.11CMakeLists.txt / package.xml),CMake 工程名 fastrtps(历史命名),语言 C++11+,许可证 Apache 2.0

eProsima Fast-DDS(原 Fast RTPS)是 OMG DDS 规范的 C++ 实现,底层协议为 RTPS(Real-Time Publish-Subscribe)。在 ROS 2 Humble 中,它是默认 RMW 实现 rmw_fastrtps_cpp 所依赖的中间件;所有 ros2 topic pub/echo、节点间通信最终都经由 Fast-DDS 的 RTPS 栈在网络上收发数据。


1. 总体认识

1.1 核心职责

能力 说明
DDS API Participant / Publisher / Subscriber / DataWriter / DataReader / Topic / QoS
RTPS 协议栈 SPDP/SEDP 发现、Reader/Writer 状态机、Heartbeat/AckNack/Gap/Data 子消息
传输层 UDP、TCP(可选 TLS)、共享内存(SHM)、链式传输
序列化 依赖 Fast-CDR 做 CDR payload 编码
XTypes 动态类型、TypeLookup、TypeObject
安全(可选) DDS Security:PKI-DH 认证、Permissions、AES-GCM-GMAC 加密
零拷贝 DataSharing(进程内共享内存 payload pool)

1.2 在 ROS 2 栈中的位置

ROS 2 应用RMW 层类型支持Fast-DDSrclcpp Noderclrmw_fastrtps_cpprmw_fastrtps_shared_cpprosidl_typesupport_fastrtps_cppFast-CDRfastdds::dds APIfastrtps::rtps 协议栈Transport UDP/SHM/TCP
组件 关系
Fast-CDR 序列化引擎(见 Fast-CDR 源码详细分析
foonathan_memory 自定义内存分配器,减少 RTPS 热路径堆分配
rmw_fastrtps_shared_cpp 封装 DomainParticipantDataWriterDataReader
rosidl_typesupport_fastrtps 为每个 msg 生成 TopicDataType + CDR serialize/deserialize

ROS 2 中 Publisher/Subscription 对应 DDS 的 DataWriter/DataReader,而非 DDS Publisher/Subscriber。RMW 为每个 Participant 创建一个 DDS Publisher 和一个 DDS Subscriber 作为容器。


2. 三层 API 架构

Fast-DDS 在同一个 libfastrtps中并存三套 API:

层级 命名空间 头文件路径 实现目录 状态
现代 DDS API eprosima::fastdds::dds include/fastdds/dds/ src/cpp/fastdds/ 主路径 / ROS 2 使用
RTPS 直连 API eprosima::fastrtps::rtps include/fastdds/rtps/ src/cpp/rtps/ 底层协议访问
Legacy Fast RTPS eprosima::fastrtps include/fastrtps/ src/cpp/fastrtps_deprecated/ 已弃用
OMG PSM 包装 dds:: include/dds/ src/cpp/dds/ ISO C++ DDS 薄封装

CMake 工程名仍为 fastrtps,ROS package 名也是 fastrtps,与产品名 Fast-DDS 并存——这是历史兼容设计。

2.1 分层调用关系

DomainParticipantDomainParticipantImplRTPSParticipant / RTPSParticipantImplBuiltinProtocolsNetworkFactoryMessageReceiverPDP 参与者发现EDP 端点发现WLP 存活协议

3. 目录结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Fast-DDS/
├── include/
│ ├── fastdds/dds/ # 现代 DDS 公开 API(221 个头文件)
│ ├── fastdds/rtps/ # RTPS 公开 API
│ ├── fastrtps/ # Legacy API(201 个头文件)
│ └── dds/ # OMG PSM 包装(119 个头文件)
├── src/cpp/ # 全部实现(478 源文件,~20 万行)
│ ├── fastdds/ # DDS 层(~29K 行)
│ ├── rtps/ # RTPS 核心(~101K 行)★ 最大模块
│ ├── dynamic-types/ # XTypes 动态类型(~35K 行)
│ ├── security/ # 安全插件(~12K 行)
│ ├── statistics/ # 统计模块(可选,~14K 行)
│ ├── fastrtps_deprecated/ # Legacy 实现(~5K 行)
│ ├── dds/ # PSM 包装(~1.3K 行)
│ └── utils/ # 公共工具
├── test/ # unittest / blackbox / performance
├── tools/ # fastdds CLI、discovery server
├── examples/ # C++ 示例
├── cmake/ # 构建与打包
├── thirdparty/ # taocpp-pegtl 等
├── CMakeLists.txt
└── package.xml # ROS 包名 fastrtps

3.1 src/cpp/rtps/ 子模块

子目录 文件数 职责
builtin/ 60 PDP/EDP 发现、WLP、Discovery Server 数据库
transport/ 55 UDP/TCP/SHM 传输实现
history/ 25 CacheChange、Reader/Writer History、Payload Pool
messages/ 12 RTPS 报文组装/解析、子消息
DataSharing/ 11 进程内零拷贝共享内存
reader/ / writer/ 各 10 Stateful/Stateless Reader/Writer
participant/ RTPSParticipantImpl
security/ SecurityManager 框架
network/ NetworkFactory、收发资源
persistence/ SQLite3 持久化 Writer/Reader

4. DDS 层 — 现代 API

路径:include/fastdds/dds/src/cpp/fastdds/

4.1 核心实体

实体 公开头文件 实现
DomainParticipantFactory dds/domain/DomainParticipantFactory.hpp domain/DomainParticipantFactory.cpp
DomainParticipant dds/domain/DomainParticipant.hpp domain/DomainParticipant.cpp + DomainParticipantImpl.cpp
Publisher dds/publisher/Publisher.hpp publisher/PublisherImpl.cpp
DataWriter dds/publisher/DataWriter.hpp publisher/DataWriterImpl.cpp + DataWriterHistory.cpp
Subscriber dds/subscriber/Subscriber.hpp subscriber/SubscriberImpl.cpp
DataReader dds/subscriber/DataReader.hpp subscriber/DataReaderImpl.cpp + history/DataReaderHistory.cpp
Topic dds/topic/Topic.hpp topic/TopicImpl.cpp
TypeSupport dds/topic/TypeSupport.hpp topic/TypeSupport.cpp

设计模式:公开类(Entity 子类)+ Impl 类。公开类仅转发调用,Impl 持有 RTPS 对象和业务逻辑。

4.2 Participant 启用流程

DomainParticipantImpl::enable() 是 DDS 与 RTPS 的衔接点:

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
ReturnCode_t DomainParticipantImpl::enable()
{
assert(rtps_participant_ == nullptr);

fastrtps::rtps::RTPSParticipantAttributes rtps_attr;
set_attributes_from_qos(rtps_attr, qos_);
rtps_attr.participantID = participant_id_;

RTPSParticipant* part = RTPSDomain::clientServerEnvironmentCreationOverride(...);

if (part == nullptr)
{
part = RTPSDomain::createParticipant(domain_id_, false, rtps_attr, &rtps_listener_);
// ...
}

guid_ = part->getGuid();
rtps_participant_ = part;

// autoenable: 依次 enable Topic → Publisher → Subscriber
if (qos_.entity_factory().autoenable_created_entities) { /* ... */ }

rtps_participant_->enable();

return ReturnCode_t::RETCODE_OK;
}

流程概要:

  1. DomainParticipantQos 映射出 RTPSParticipantAttributes
  2. 调用 RTPSDomain::createParticipant() 创建底层 RTPS 参与者
  3. autoenable_created_entities,依次 enable 已创建的 Topic/Publisher/Subscriber
  4. 调用 rtps_participant_->enable() 启动发现与传输

4.3 DataWriter 写数据路径

1
2
3
4
5
6
7
8
9
10
11
bool DataWriterImpl::write(
void* data)
{
if (writer_ == nullptr)
{
return false;
}

logInfo(DATA_WRITER, "Writing new data");
return ReturnCode_t::RETCODE_OK == create_new_change(ALIVE, data);
}

完整链路:

1
2
3
4
5
6
7
8
DataWriter::write(data)
→ DataWriterImpl::create_new_change()
→ TypeSupport::serialize() // Fast-CDR
→ WriterHistory 添加 CacheChange
→ RTPSWriter (StatefulWriter/StatelessWriter)
→ FlowController(流控,可选)
→ RTPSMessageGroup 组装 Data 子消息
→ NetworkFactory → Transport 发送

4.4 DataReader 读数据路径

1
2
3
4
5
6
7
Transport 接收 UDP/SHM 报文
→ MessageReceiver 解析 RTPS 子消息
→ StatefulReader / StatelessReader
→ ReaderHistory 存入 CacheChange
→ DataReaderImpl::read/take()
→ TypeSupport::deserialize() // Fast-CDR
→ 返回给应用 / RMW

4.5 其他 DDS 能力

模块 路径 说明
ContentFilteredTopic topic/ContentFilteredTopic*.cpp 内容过滤主题
DDSSQLFilter topic/DDSSQLFilter/ SQL-like 过滤表达式解析(pegtl)
TypeLookup fastdds/builtin/typelookup/ XTypes 远程类型查询
WaitSet / Condition fastdds/core/condition/ 同步等待机制
Log fastdds/log/ 可配置日志消费者

5. RTPS 层 — 协议核心

路径:include/fastdds/rtps/src/cpp/rtps/

5.1 RTPSParticipant

组件 路径
公开 API include/fastdds/rtps/participant/RTPSParticipant.h
实现 src/cpp/rtps/participant/RTPSParticipantImpl.cpp/.h
工厂 include/fastdds/rtps/RTPSDomain.hsrc/cpp/rtps/RTPSDomain.cpp

RTPSParticipantImpl 是 RTPS 层的中心对象,持有:

  • BuiltinProtocols — 发现与存活
  • NetworkFactory — 传输注册与 locator 选择
  • MessageReceiver — 入站报文分发
  • SecurityManager(可选)— 安全插件编排
  • Reader/Writer 集合

5.2 发现协议(BuiltinProtocols)

路径:src/cpp/rtps/builtin/BuiltinProtocols.cpp

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
bool BuiltinProtocols::initBuiltinProtocols(
RTPSParticipantImpl* p_part,
BuiltinAttributes& attributes)
{
// PDP — 按 discoveryProtocol 选择实现
switch (m_att.discovery_config.discoveryProtocol)
{
case DiscoveryProtocol_t::SIMPLE:
mp_PDP = new PDPSimple(this, allocation);
break;
case DiscoveryProtocol_t::CLIENT:
mp_PDP = new fastdds::rtps::PDPClient(this, allocation);
break;
case DiscoveryProtocol_t::SERVER:
mp_PDP = new fastdds::rtps::PDPServer(this, allocation, ...);
break;
// ...
}

mp_PDP->init(mp_participantImpl);

// WLP — Writer Liveliness Protocol
if (m_att.use_WriterLivelinessProtocol)
{
mp_WLP = new WLP(this);
mp_WLP->initWL(mp_participantImpl);
}

// TypeLookupManager
if (m_att.typelookup_config.use_client || m_att.typelookup_config.use_server)
{
tlm_ = new fastdds::dds::builtin::TypeLookupManager(this);
tlm_->init_typelookup_service(mp_participantImpl);
}

return true;
}

PDP — Participant Discovery Protocol

模式 用途
SIMPLE PDPSimple 默认:Multicast SPDP + 单播互发现
CLIENT PDPClient Discovery Server 客户端
SERVER PDPServer Discovery Server 服务端
SUPER_CLIENT PDPClient(..., true) 超级客户端
BACKUP PDPServer(..., TRANSIENT) 持久化 Discovery Server(需 SQLite3)

SPDP 通过内置 Writer/Reader 交换 ParticipantProxyData(GUID、locator、UserData 等)。

EDP — Endpoint Discovery Protocol

实现 路径 说明
EDPSimple rtps/builtin/discovery/endpoint/EDPSimple.cpp 动态端点发现(默认)
EDPStatic EDPStatic.cpp 静态端点配置(XML)
EDPClient/Server 配合 Discovery Server 中心化端点信息

EDP 在发现远端 Writer/Reader 的 WriterProxyData/ReaderProxyData 后,按 Topic 名 + QoS 兼容性做匹配,调用 matched_writer_add / matched_reader_add 建立通信关系:

1
2
3
4
5
6
7
publications_reader_.first->matched_writer_add(*temp_writer_proxy_data);
// ...
publications_writer_.first->matched_reader_add(*temp_reader_proxy_data);
// ...
subscriptions_reader_.first->matched_writer_add(*temp_writer_proxy_data);
// ...
subscriptions_writer_.first->matched_reader_add(*temp_reader_proxy_data);

WLP — Writer Liveliness Protocol

管理 DataWriter 存活断言(Automatic / Manual-by-Participant / Manual-by-Topic),超时后通知 DataReader。

5.3 Reader / Writer 类型

类型 可靠性 典型场景
StatelessWriter Best-Effort 不需 ACK 的发送
StatefulWriter Reliable 需 Heartbeat/AckNack 确认
StatelessReader Best-Effort 接收 BE 数据
StatefulReader Reliable 接收 REL 数据,回复 AckNack

StatefulWriter(~2100 行)维护 ReaderProxy 列表,负责:

  • 向每个 matched reader 发送 Heartbeat
  • 处理 AckNack,重传丢失的 CacheChange
  • 发送 Gap 通知不可用序列号
  • 与 FlowController 协作限流

5.4 History 与 CacheChange

路径:include/fastdds/rtps/common/CacheChange.hsrc/cpp/rtps/history/

1
2
3
4
5
6
7
8
9
10
CacheChange
├── writerGUID / sequenceNumber
├── serializedPayload(Fast-CDR 编码后的字节)
├── instanceHandle
└── kind(ALIVE / NOT_ALIVE_DISPOSED 等)

WriterHistory — _writer 侧变更缓存(发送队列)
ReaderHistory — _reader 侧变更缓存(接收队列)
CacheChangePool — 对象池,减少分配
TopicPayloadPool — payload 内存池(可共享)

5.5 报文处理

组件 路径 职责
MessageReceiver rtps/messages/MessageReceiver.cpp (~1500 行) 解析入站 RTPS 报文,分发 Data/Heartbeat/AckNack/Gap 子消息
RTPSMessageGroup RTPSMessageGroup.cpp 组装出站 RTPS 报文
RTPSMessageCreator RTPSMessageCreator.cpp 创建 RTPS 报文头
子消息模板 messages/submessages/ DataMsg、HeartbeatMsg、AckNackMsg、GapMsg

MessageReceiver 构造时根据是否启用安全选择不同的处理函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MessageReceiver::MessageReceiver(
RTPSParticipantImpl* participant,
uint32_t rec_buffer_size)
: participant_(participant)
// ...
#if HAVE_SECURITY
, crypto_msg_(participant->is_secure() ? rec_buffer_size : 0)
// ...
#endif
{
#if HAVE_SECURITY
if (participant->is_secure())
{
process_data_message_function_ = std::bind(
&MessageReceiver::process_data_message_with_security, ...);
}
#endif
}

6. 传输层

路径:src/cpp/rtps/transport/include/fastdds/rtps/transport/

NetworkFactoryrtps/network/NetworkFactory.cpp)根据 RTPSParticipantAttributes 注册传输、选择 locator、创建收发资源。

传输 Descriptor 头文件 实现
UDPv4/v6 UDPv4TransportDescriptor.h UDPv4Transport.cppUDPTransportInterface.cpp
TCPv4/v6 TCPv4TransportDescriptor.h TCPv4Transport.cppTCPTransportInterface.cpp
TLS TCPChannelResourceSecure.cppSECURITY + OpenSSL)
Shared Memory shared_mem/SharedMemTransportDescriptor.h shared_mem/SharedMemTransport.cpp
Chaining ChainingTransportDescriptor.h 包装其他传输,做过滤/统计

6.1 默认传输配置

ROS 2 Humble 中 RMW 默认配置 UDP + SHM

  • UDP:跨进程/跨主机通信
  • SHM:同主机进程间高效通信(SHM_TRANSPORT_DEFAULT=ON

SHM 实现基于 MultiProducerConsumerRingBuffer 无锁环形缓冲区。

6.2 DataSharing(零拷贝)

路径:src/cpp/rtps/DataSharing/

与 SHM 传输不同,DataSharing 是 payload 级别的共享内存池

  • Writer 将 serialized payload 写入共享 segment
  • Reader 直接读取,避免 memcpy
  • 适用于同进程或配置了 DataSharing 的 Writer/Reader 对

7. 序列化与类型系统

7.1 Fast-CDR 集成

  • DDS 层通过 TopicDataType 接口定义 serialize() / deserialize() / getSerializedSizeProvider()
  • ROS 2 的 rosidl_typesupport_fastrtps_cpp 生成这些方法的实现,内部使用 eprosima::fastcdr::Cdr
  • RTPS 层只处理 SerializedPayload_t(原始字节 + length)

7.2 动态类型(XTypes)

路径:src/cpp/dynamic-types/include/fastrtps/types/

说明
DynamicTypeBuilderFactory 运行时构建类型
DynamicData 运行时数据实例
DynamicPubSubType 动态类型的 TopicDataType
TypeObjectFactory TypeObject 注册与查询
TypeLookupManager 远程类型发现服务

8. 安全模块(可选)

构建选项:option(SECURITY "Activate security" OFF),需 OpenSSL。

8.1 两层结构

  1. RTPS 框架src/cpp/rtps/security/

    • SecurityManager.cpp(~4300 行)— 编排认证、授权、加密
    • 插件接口:Authentication.hAccessControl.hCryptography.h
  2. 内置插件src/cpp/security/

子模块 实现 功能
authentication/ PKIDH.cpp PKI-DH 参与者认证
accesscontrol/ Permissions.cpp Governance/Permissions XML 解析与校验
cryptography/ AESGCMGMAC*.cpp AES-GCM 加密 + GMAC 完整性
artifact_providers/ FileProvider.cpp 证书/密钥文件加载

ROS 2 安全启用时,RMW 通过 rmw_dds_common 配置 Security 属性,Fast-DDS 加载对应插件。


9. 其他模块

9.1 Statistics(可选)

option(FASTDDS_STATISTICS "Enable Fast DDS Statistics Module" OFF)

路径:src/cpp/statistics/

提供 DDS 级统计 topic(如网络流量、延迟、丢包),通过 hook RTPS 层 StatisticsBase 收集数据。

9.2 持久化

option(SQLITE3_SUPPORT "Activate SQLITE3 support" ON)

路径:src/cpp/rtps/persistence/

  • SQLite3PersistenceService — 将 Writer/Reader 历史持久化到 SQLite
  • 用于 TRANSIENT/PERSISTENT durability 和 Discovery Server BACKUP 模式

9.3 XML 配置

路径:src/cpp/rtps/xmlparser/

  • XMLProfileManager — 加载 DEFAULT_FASTRTPS_PROFILES.xml 等配置文件
  • 可覆盖 Participant/Writer/Reader 的 QoS 和传输设置
  • ROS 2 可通过 FASTRTPS_DEFAULT_PROFILES_FILE 环境变量指定

9.4 工具

工具 路径 说明
fastdds tools/fastdds/ Python CLI:discovery、shm clean 等
fast-discovery-server tools/fds/ Discovery Server 独立进程

10. 构建与依赖

10.1 产物

CMake target fastrtps
库文件 libfastrtps.so
版本 2.6.11

10.2 依赖

依赖 用途
fastcdr CDR 序列化
foonathan_memory 内存分配器
Asio(bundled) 异步 I/O(TCP/UDP)
TinyXML2(bundled) XML 配置解析
OpenSSL(可选) SECURITY / TLS
SQLite3(可选,默认 ON) 持久化

10.3 重要 CMake 选项

选项 默认 说明
SECURITY OFF DDS Security 插件
SHM_TRANSPORT_DEFAULT ON 默认传输含 SHM
SQLITE3_SUPPORT ON SQLite 持久化
FASTDDS_STATISTICS OFF 统计模块
COMPILE_TOOLS ON 构建 fastdds CLI
BUILD_SHARED_LIBS ON 动态库
STRICT_REALTIME OFF 实时 API 行为

10.4 ROS / colcon 集成

1
2
3
4
5
6
7
<!-- package.xml -->
<name>fastrtps</name>
<version>2.6.11</version>
<depend>fastcdr</depend>
<depend>foonathan_memory_vendor</depend>
<depend>tinyxml2</depend>
<depend>libssl-dev</depend>

colcon.pkg 声明依赖:fastcdrFOONATHAN_MEMORYfoonathan_memory_vendor


11. ROS 2 RMW 集成

路径:ros2_humble/src/ros2/rmw_fastrtps/rmw_fastrtps_shared_cpp/

11.1 核心包装结构

1
2
3
4
5
6
7
8
9
10
11
12
typedef struct CustomParticipantInfo
{
eprosima::fastdds::dds::DomainParticipant * participant_{nullptr};
ParticipantListener * listener_{nullptr};

eprosima::fastdds::dds::Publisher * publisher_{nullptr};
eprosima::fastdds::dds::Subscriber * subscriber_{nullptr};

mutable std::mutex entity_creation_mutex_;
bool leave_middleware_default_qos;
publishing_mode_t publishing_mode;
} CustomParticipantInfo;
RMW 结构 封装的 Fast-DDS 类型
CustomParticipantInfo DomainParticipant + 容器 Publisher/Subscriber
CustomPublisherInfo DataWriter + TypeSupport + Listener
CustomSubscriberInfo DataReader + Listener + ContentFilteredTopic

11.2 Participant 创建

rmw_fastrtps_shared_cpp/src/participant.cpp

  1. 构造 DomainParticipantQos(含 enclave、security 属性)
  2. 注册 UDPv4 + SharedMem 传输 descriptor
  3. DomainParticipantFactory::create_participant(domain_id, qos, listener)
  4. 创建容器 PublisherSubscriber
  5. ParticipantListener 监听 on_participant_discovery 更新 ROS graph

11.3 发布 / 订阅

RMW 操作 Fast-DDS 调用
rmw_publish CustomPublisherInfo::data_writer_->write(data)
rmw_take CustomSubscriberInfo::data_reader_->take(...)
QoS 映射 rmw_fastrtps_shared_cpp/qos.cppDataWriterQos/DataReaderQos
类型支持 TypeSupport.hpp 桥接 rosidl → TopicDataType

11.4 完整 ROS 2 通信链路

1
2
3
4
5
6
7
8
9
10
11
12
rclcpp::Publisher::publish(msg)
→ rcl_publish()
→ rmw_publish() [rmw_fastrtps_shared_cpp]
→ DataWriter::write(msg)
→ TypeSupport::serialize(msg) [rosidl_typesupport_fastrtps + Fast-CDR]
→ StatefulWriter::new_change()
→ RTPSMessageGroup → UDP/SHM
═══════ 网络 / 共享内存 ═══════
→ MessageReceiver → StatefulReader
→ TypeSupport::deserialize(msg)
→ rmw_take() → rcl_take()
→ rclcpp::Subscription callback

12. 测试

1
2
3
4
5
6
test/
├── unittest/ # 单元测试(rtps/history, transport, security 等)
├── blackbox/ # 端到端通信测试
├── performance/ # 吞吐量/延迟基准
├── communication/ # 多参与者互通
└── system/tools/ # fastdds CLI 测试

核心 API 覆盖:test/unittest/rtps/test/blackbox/


13. 设计特点小结

特点 说明
双 API 共存 现代 fastdds::dds + Legacy fastrtps + 底层 rtps
Impl 模式 所有 DDS Entity 均有对应 *Impl
RTPS 为核心 DDS 层是 RTPS 的薄封装;~50% 代码在 rtps/
插件化安全 Authentication/AccessControl/Cryptography 可替换
多传输并存 同一 Participant 可同时使用 UDP + SHM + TCP
Discovery 可扩展 Simple / Client-Server / Static EDP
内存优化 foonathan_memory + CacheChangePool + PayloadPool + DataSharing
ROS 2 QL1 声明 Quality Level 1(见 QUALITY.md

14. 与 Fast-CDR 的分工

层次 职责
应用语义 Fast-DDS DDS API Topic、QoS、匹配、History
Wire 协议 Fast-DDS RTPS 发现、可靠传输、子消息
Payload 编码 Fast-CDR CDR serialize/deserialize

Fast-DDS 不做序列化,只搬运 SerializedPayload_t;类型编码全部由 Fast-CDR + 生成的 TopicDataType 完成。


15. 推荐阅读顺序

  1. DDS 入口DomainParticipantImpl::enable() — 理解 DDS→RTPS 衔接
  2. 发现机制BuiltinProtocols::initBuiltinProtocols()PDPSimpleEDPSimple
  3. 发送路径DataWriterImpl::write()StatefulWriterRTPSMessageGroup
  4. 接收路径MessageReceiverStatefulReaderDataReaderImpl
  5. 传输选择NetworkFactory + UDPv4Transport / SharedMemTransport
  6. ROS 2 集成rmw_fastrtps_shared_cpp/src/participant.cpp + custom_participant_info.hpp
  7. 序列化rosidl_typesupport_fastrtps_cpp 生成代码 + Fast-CDR 分析
  8. 配置DEFAULT_FASTRTPS_PROFILES.xml + XMLProfileManager

16. API 速查

16.1 最小 DDS 发布示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/publisher/Publisher.hpp>
#include <fastdds/dds/publisher/DataWriter.hpp>
#include <fastdds/dds/topic/Topic.hpp>
#include <fastdds/dds/topic/TypeSupport.hpp>

using namespace eprosima::fastdds::dds;

auto factory = DomainParticipantFactory::get_instance();
DomainParticipant* participant = factory->create_participant(0, PARTICIPANT_QOS_DEFAULT);

TypeSupport type(new MyTypePubSubType());
type.register_type(participant);

Topic* topic = participant->create_topic("MyTopic", "MyType", TOPIC_QOS_DEFAULT);
Publisher* publisher = participant->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);
DataWriter* writer = publisher->create_datawriter(topic, DATAWRITER_QOS_DEFAULT, nullptr);

MyMsg msg;
writer->write(&msg);

16.2 关键类一览

命名空间 层级
DomainParticipant fastdds::dds DDS
DataWriter / DataReader fastdds::dds DDS
RTPSParticipant fastrtps::rtps RTPS
StatefulWriter / StatefulReader fastrtps::rtps RTPS
PDPSimple / EDPSimple fastrtps::rtps 发现
NetworkFactory fastrtps::rtps 传输
MessageReceiver fastrtps::rtps 报文
CacheChange fastrtps::rtps 历史
SecurityManager fastrtps::rtps 安全

文档基于 ROS 2 Humble 工作区中的 Fast-DDS 2.6.11 源码分析生成。

文章互动

阅读 --

留言

0 条留言

正在加载留言…