CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
ros2
middleware
fastdds
FastDDSPublisherMiddleware.h
Go to the documentation of this file.
1
// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma de Barcelona (UAB).
2
// This work is licensed under the terms of the MIT license.
3
// For a copy, see <https://opensource.org/licenses/MIT>.
4
5
#pragma once
6
7
#include "
carla/ros2/middleware/IPublisherMiddleware.h
"
8
#include "
carla/ros2/middleware/fastdds/FastDDSSharedParticipant.h
"
9
#include "
carla/ros2/middleware/fastdds/GenericCdrPubSubType.h
"
10
#include "
carla/ros2/types/UserDataFormat.h
"
11
#include "
carla/Logging.h
"
12
13
#include <fastdds/dds/domain/DomainParticipant.hpp>
14
#include <fastdds/dds/topic/Topic.hpp>
15
#include <fastdds/dds/topic/TypeSupport.hpp>
16
#include <fastdds/dds/publisher/Publisher.hpp>
17
#include <fastdds/dds/publisher/DataWriter.hpp>
18
#include <fastdds/dds/publisher/DataWriterListener.hpp>
19
#include <fastdds/dds/publisher/qos/DataWriterQos.hpp>
20
#include <fastdds/dds/core/status/PublicationMatchedStatus.hpp>
21
#include <fastdds/dds/topic/qos/TopicQos.hpp>
22
#include <fastrtps/attributes/ParticipantAttributes.h>
23
#include <fastrtps/qos/QosPolicies.h>
24
25
#include <atomic>
26
27
namespace
carla
{
28
namespace
ros2
{
29
30
namespace
efd = eprosima::fastdds::dds;
31
using
erc
= eprosima::fastrtps::types::ReturnCode_t;
32
33
/// FastDDS implementation of IPublisherMiddleware.
34
/// Parameterized on a traits type T that provides:
35
/// T::msg_type — the message type (a carla::ros2::msg::* POD struct)
36
/// Serialization is handled by GenericCdrPubSubType<msg_type> via CdrSerialization.h.
37
///
38
/// Uses FastDDSSharedParticipant for a single refcounted DomainParticipant
39
/// across all FastDDS endpoints, mirroring CycloneDDS's shared-participant
40
/// model. This avoids the discovery storm that occurred when N independent
41
/// participants were destroyed back-to-back on ROS2::Shutdown().
42
///
43
/// Sets USER_DATA QoS on the DataWriter to the REP-2016 KV payload
44
/// "typehash=RIHS01_<hex>;" (PID_USER_DATA = 0x002c in DDSI-RTPS v2.5
45
/// §9.6.2.2.2) so that Jazzy rmw_cyclonedds_cpp and rmw_fastrtps_cpp can
46
/// perform REP-2011 type-hash-based endpoint matching without warning.
47
template
<
typename
T>
48
class
FastDDSPublisherMiddleware
49
:
public
IPublisherMiddleware
,
50
public
eprosima::fastdds::dds::DataWriterListener {
51
public
:
52
using
msg_type
=
typename
T::msg_type;
53
54
FastDDSPublisherMiddleware
() =
default
;
55
FastDDSPublisherMiddleware
(
const
FastDDSPublisherMiddleware
&) =
delete
;
56
FastDDSPublisherMiddleware
&
operator=
(
const
FastDDSPublisherMiddleware
&) =
delete
;
57
58
void
on_publication_matched
(
59
efd::DataWriter*
writer
,
60
const
efd::PublicationMatchedStatus& info)
override
{
61
_alive
.store(info.current_count > 0, std::memory_order_relaxed);
62
}
63
64
~FastDDSPublisherMiddleware
()
override
{
65
if
(
_datawriter
) {
66
// Detach listener before deletion so that on_publication_matched cannot
67
// fire on a partially-destroyed object.
68
_datawriter
->set_listener(
nullptr
);
69
_publisher
->delete_datawriter(
_datawriter
);
70
}
71
if
(
_publisher
) {
72
_participant
->delete_publisher(
_publisher
);
73
}
74
if
(
_topic
) {
75
_participant
->delete_topic(
_topic
);
76
}
77
if
(
_participant
) {
78
// Release the per-type refcount so unregister_type() fires when the last
79
// user of this TypeSupport goes away, before the shared participant drops.
80
FastDDSSharedParticipant::release_type
(
_type
->getName());
81
FastDDSSharedParticipant::release
();
82
_participant
=
nullptr
;
83
}
84
}
85
86
bool
Init
(
const
std::string& topic_name)
override
{
87
return
InitImpl
(topic_name,
nullptr
);
88
}
89
90
bool
Init
(
const
std::string& topic_name,
const
QosProfile
& qos)
override
{
91
return
InitImpl
(topic_name, &qos);
92
}
93
94
private
:
95
/// Shared Init body. When @a qos is non-null the profile overrides the
96
/// FastDDS DataWriter defaults; when null the defaults are kept, preserving
97
/// the pre-QoS behavior for every existing publisher.
98
bool
InitImpl
(
const
std::string& topic_name,
const
QosProfile
* qos) {
99
if
(
_type
==
nullptr
) {
100
log_error(
"FastDDSPublisherMiddleware: Invalid TypeSupport"
);
101
return
false
;
102
}
103
104
_participant
=
FastDDSSharedParticipant::acquire
();
105
if
(
_participant
==
nullptr
) {
106
log_error(
"FastDDSPublisherMiddleware: Shared participant unavailable"
);
107
return
false
;
108
}
109
110
_type
.register_type(
_participant
);
111
FastDDSSharedParticipant::retain_type
(
_type
->getName());
112
113
efd::PublisherQos pubqos = efd::PUBLISHER_QOS_DEFAULT;
114
_publisher
=
_participant
->create_publisher(pubqos,
nullptr
);
115
if
(
_publisher
==
nullptr
) {
116
log_error(
"FastDDSPublisherMiddleware: Failed to create Publisher"
);
117
return
false
;
118
}
119
120
// Multiple endpoints may share the same topic on the shared participant
121
// (e.g. every actor publishes on rt/tf). FastDDS's create_topic rejects a
122
// duplicate name, so probe first and reuse via find_topic if it already
123
// exists; find_topic increments an internal refcount that the matching
124
// delete_topic in the destructor balances.
125
if
(
_participant
->lookup_topicdescription(topic_name) !=
nullptr
) {
126
_topic
=
_participant
->find_topic(topic_name, eprosima::fastrtps::Duration_t{0, 0});
127
}
else
{
128
efd::TopicQos tqos = efd::TOPIC_QOS_DEFAULT;
129
_topic
=
_participant
->create_topic(topic_name,
_type
->getName(), tqos);
130
}
131
if
(
_topic
==
nullptr
) {
132
log_error(
"FastDDSPublisherMiddleware: Failed to create Topic"
);
133
return
false
;
134
}
135
136
efd::DataWriterQos wqos = efd::DATAWRITER_QOS_DEFAULT;
137
wqos.endpoint().history_memory_policy =
138
eprosima::fastrtps::rtps::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
139
140
// ASYNCHRONOUS_PUBLISH_MODE: write() must never do network I/O on the
141
// calling thread. The FastDDS default (synchronous) runs the whole
142
// RTPS/UDP send inline in write(); with the simulator publishing multi-MB
143
// camera frames from engine threads, UE's task scheduler sees those
144
// threads blocked in sendto(), spins up standby workers and reaps them
145
// again, and the destabilized worker pool stalls every render-thread
146
// ParallelFor (SceneCapture InitViews) -- the tick rate collapses to a
147
// few Hz with CPU and GPU idle. In asynchronous mode write() only
148
// enqueues; the FastDDS flow-controller thread does the sends. The
149
// default (unthrottled) flow controller is intentional: we want the
150
// I/O off-thread, not rate-limited.
151
wqos.publish_mode().kind = efd::ASYNCHRONOUS_PUBLISH_MODE;
152
153
if
(qos !=
nullptr
) {
154
wqos.reliability().kind =
155
qos->
reliability
==
QosProfile::Reliability::Reliable
156
? efd::RELIABLE_RELIABILITY_QOS
157
: efd::BEST_EFFORT_RELIABILITY_QOS;
158
wqos.durability().kind =
159
qos->
durability
==
QosProfile::Durability::TransientLocal
160
? efd::TRANSIENT_LOCAL_DURABILITY_QOS
161
: efd::VOLATILE_DURABILITY_QOS;
162
wqos.history().kind =
163
qos->
history
==
QosProfile::History::KeepLast
164
? efd::KEEP_LAST_HISTORY_QOS
165
: efd::KEEP_ALL_HISTORY_QOS;
166
wqos.history().depth = qos->
history_depth
;
167
}
168
169
// Set USER_DATA (PID_USER_DATA = 0x002c per OMG DDSI-RTPS v2.5 §9.6.2.2.2)
170
// to the REP-2011/REP-2016 type-hash KV payload "typehash=RIHS01_<hex>;".
171
// Jazzy rmw_cyclonedds_cpp / rmw_fastrtps_cpp parse this during SEDP
172
// endpoint discovery to verify type compatibility.
173
auto
ud =
build_user_data_for<msg_type>
();
174
if
(!ud.empty()) {
175
wqos.user_data().data_vec(ud);
176
}
177
178
efd::DataWriterListener* listener =
179
static_cast<
efd::DataWriterListener*
>
(
this
);
180
_datawriter
=
_publisher
->create_datawriter(
_topic
, wqos, listener);
181
if
(
_datawriter
==
nullptr
) {
182
log_error(
"FastDDSPublisherMiddleware: Failed to create DataWriter"
);
183
return
false
;
184
}
185
186
_topic_name
= topic_name;
187
return
true
;
188
}
189
190
public
:
191
bool
Publish
(
void
* message_data)
override
{
192
// Init() failed (e.g. no participant): there is no writer to use.
193
if
(
_datawriter
==
nullptr
) {
194
if
(!
_reported_not_ready
.exchange(
true
)) {
195
log_error(
"FastDDSPublisherMiddleware::Publish called on a publisher "
196
"whose Init() failed (see the errors above); all its messages "
197
"are dropped"
);
198
}
199
return
false
;
200
}
201
eprosima::fastrtps::rtps::InstanceHandle_t instance_handle;
202
erc
rcode =
_datawriter
->write(message_data, instance_handle);
203
if
(rcode == erc::ReturnCodeValue::RETCODE_OK) {
204
return
true
;
205
}
206
log_error(
"FastDDSPublisherMiddleware::Publish ("
,
207
_topic_name
,
") failed with code:"
, rcode());
208
return
false
;
209
}
210
211
bool
IsAlive
()
const override
{
212
return
_alive
.load(std::memory_order_relaxed);
213
}
214
215
std::string
GetTopicName
()
const override
{
216
return
_topic_name
;
217
}
218
219
private
:
220
efd::DomainParticipant*
_participant
{
nullptr
};
221
efd::Publisher*
_publisher
{
nullptr
};
222
efd::Topic*
_topic
{
nullptr
};
223
efd::DataWriter*
_datawriter
{
nullptr
};
224
efd::TypeSupport
_type
{
new
GenericCdrPubSubType<msg_type>
() };
225
226
std::string
_topic_name
;
227
std::atomic<bool>
_alive
{
false
};
228
std::atomic<bool>
_reported_not_ready
{
false
};
229
};
230
231
}
// namespace ros2
232
}
// namespace carla
FastDDSSharedParticipant.h
GenericCdrPubSubType.h
IPublisherMiddleware.h
Logging.h
UserDataFormat.h
carla::ros2::FastDDSPublisherMiddleware::Publish
bool Publish(void *message_data) override
Serialize and write a message to the network.
Definition
FastDDSPublisherMiddleware.h:191
carla::ros2::FastDDSPublisherMiddleware::_alive
std::atomic< bool > _alive
Definition
FastDDSPublisherMiddleware.h:227
carla::ros2::FastDDSPublisherMiddleware::IsAlive
bool IsAlive() const override
Definition
FastDDSPublisherMiddleware.h:211
carla::ros2::FastDDSPublisherMiddleware::FastDDSPublisherMiddleware
FastDDSPublisherMiddleware(const FastDDSPublisherMiddleware &)=delete
carla::ros2::FastDDSPublisherMiddleware::~FastDDSPublisherMiddleware
~FastDDSPublisherMiddleware() override
Definition
FastDDSPublisherMiddleware.h:64
carla::ros2::FastDDSPublisherMiddleware::GetTopicName
std::string GetTopicName() const override
Definition
FastDDSPublisherMiddleware.h:215
carla::ros2::FastDDSPublisherMiddleware::operator=
FastDDSPublisherMiddleware & operator=(const FastDDSPublisherMiddleware &)=delete
carla::ros2::FastDDSPublisherMiddleware::_publisher
efd::Publisher * _publisher
Definition
FastDDSPublisherMiddleware.h:221
carla::ros2::FastDDSPublisherMiddleware::InitImpl
bool InitImpl(const std::string &topic_name, const QosProfile *qos)
Shared Init body.
Definition
FastDDSPublisherMiddleware.h:98
carla::ros2::FastDDSPublisherMiddleware::_type
efd::TypeSupport _type
Definition
FastDDSPublisherMiddleware.h:224
carla::ros2::FastDDSPublisherMiddleware::on_publication_matched
void on_publication_matched(efd::DataWriter *writer, const efd::PublicationMatchedStatus &info) override
Definition
FastDDSPublisherMiddleware.h:58
carla::ros2::FastDDSPublisherMiddleware::_topic_name
std::string _topic_name
Definition
FastDDSPublisherMiddleware.h:226
carla::ros2::FastDDSPublisherMiddleware::Init
bool Init(const std::string &topic_name, const QosProfile &qos) override
Initialize with an explicit per-topic QoS profile (see QosProfile.h).
Definition
FastDDSPublisherMiddleware.h:90
carla::ros2::FastDDSPublisherMiddleware::_participant
efd::DomainParticipant * _participant
Definition
FastDDSPublisherMiddleware.h:220
carla::ros2::FastDDSPublisherMiddleware::Init
bool Init(const std::string &topic_name) override
Initialize the underlying middleware entities.
Definition
FastDDSPublisherMiddleware.h:86
carla::ros2::FastDDSPublisherMiddleware::_datawriter
efd::DataWriter * _datawriter
Definition
FastDDSPublisherMiddleware.h:223
carla::ros2::FastDDSPublisherMiddleware::msg_type
typename T::msg_type msg_type
Definition
FastDDSPublisherMiddleware.h:52
carla::ros2::FastDDSPublisherMiddleware::_reported_not_ready
std::atomic< bool > _reported_not_ready
Definition
FastDDSPublisherMiddleware.h:228
carla::ros2::FastDDSPublisherMiddleware::_topic
efd::Topic * _topic
Definition
FastDDSPublisherMiddleware.h:222
carla::ros2::FastDDSPublisherMiddleware::FastDDSPublisherMiddleware
FastDDSPublisherMiddleware()=default
carla::ros2::FastDDSSharedParticipant::release_type
static void release_type(const std::string &type_name)
Release one use of type_name.
Definition
FastDDSSharedParticipant.cpp:105
carla::ros2::FastDDSSharedParticipant::acquire
static eprosima::fastdds::dds::DomainParticipant * acquire()
Increment the refcount and return the shared DomainParticipant.
Definition
FastDDSSharedParticipant.cpp:28
carla::ros2::FastDDSSharedParticipant::retain_type
static void retain_type(const std::string &type_name)
Notify the singleton that one more endpoint is using type_name so that unregister_type() is deferred ...
Definition
FastDDSSharedParticipant.cpp:100
carla::ros2::FastDDSSharedParticipant::release
static void release()
Decrement the refcount.
Definition
FastDDSSharedParticipant.cpp:81
carla::ros2::GenericCdrPubSubType::GenericCdrPubSubType
GenericCdrPubSubType()
Definition
GenericCdrPubSubType.h:33
carla::ros2::IPublisherMiddleware
Type-erased abstract interface for a publisher middleware.
Definition
IPublisherMiddleware.h:23
carla::ros2
Definition
AutowareSteeringCompensation.h:15
carla::ros2::build_user_data_for
std::vector< uint8_t > build_user_data_for()
Convenience: build user_data from the CdrTopicInfo<T>::type_hash() of T.
Definition
UserDataFormat.h:59
carla::ros2::erc
eprosima::fastrtps::types::ReturnCode_t erc
Definition
FastDDSAliases.h:18
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::ros2::QosProfile
Middleware-neutral QoS profile passed to IPublisherMiddleware::Init / ISubscriberMiddleware::Init whe...
Definition
QosProfile.h:27
carla::ros2::QosProfile::reliability
Reliability reliability
Definition
QosProfile.h:32
carla::ros2::QosProfile::history
History history
Definition
QosProfile.h:34
carla::ros2::QosProfile::history_depth
std::int32_t history_depth
Definition
QosProfile.h:35
carla::ros2::QosProfile::durability
Durability durability
Definition
QosProfile.h:33
carla::ros2::QosProfile::Reliability::Reliable
@ Reliable
Definition
QosProfile.h:28
carla::ros2::QosProfile::Durability::TransientLocal
@ TransientLocal
Definition
QosProfile.h:29
carla::ros2::QosProfile::History::KeepLast
@ KeepLast
Definition
QosProfile.h:30
writer
Generated by
1.18.0