Skip to content

Commit 8c423ce

Browse files
Ignore local endpoints (#131)
* Refs #18846: PoC ignore local endpoints: extend RCLCPP API Signed-off-by: JLBuenoLopez-eProsima <[email protected]> * Refs #18846: PoC ignore local endpoints: modify RCLCPP publish logic Signed-off-by: JLBuenoLopez-eProsima <[email protected]> --------- Signed-off-by: JLBuenoLopez-eProsima <[email protected]> Co-authored-by: JLBuenoLopez-eProsima <[email protected]>
1 parent cf5257c commit 8c423ce

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

rclcpp/include/rclcpp/publisher.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ class Publisher : public PublisherBase
273273
// It's not possible to do that with an unique_ptr,
274274
// as do_intra_process_publish takes the ownership of the message.
275275
bool inter_process_publish_needed =
276-
get_subscription_count() > get_intra_process_subscription_count();
276+
get_non_local_subscription_count() > 0;
277277

278278
if (inter_process_publish_needed) {
279279
auto shared_msg =
@@ -351,7 +351,7 @@ class Publisher : public PublisherBase
351351
}
352352

353353
bool inter_process_publish_needed =
354-
get_subscription_count() > get_intra_process_subscription_count();
354+
get_non_local_subscription_count() > 0;
355355

356356
if (inter_process_publish_needed) {
357357
auto ros_msg_ptr = std::make_shared<ROSMessageType>();

rclcpp/include/rclcpp/publisher_base.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ class PublisherBase : public std::enable_shared_from_this<PublisherBase>
126126
size_t
127127
get_subscription_count() const;
128128

129+
/// Get non local subscription count
130+
/** \return The number of non local subscriptions. */
131+
RCLCPP_PUBLIC
132+
size_t
133+
get_non_local_subscription_count() const;
134+
129135
/// Get intraprocess subscription count
130136
/** \return The number of intraprocess subscriptions. */
131137
RCLCPP_PUBLIC

rclcpp/src/rclcpp/publisher_base.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,31 @@ PublisherBase::get_subscription_count() const
189189
return inter_process_subscription_count;
190190
}
191191

192+
size_t
193+
PublisherBase::get_non_local_subscription_count() const
194+
{
195+
size_t inter_process_non_local_subscription_count = 0;
196+
197+
rcl_ret_t status = rcl_publisher_get_non_local_subscription_count(
198+
publisher_handle_.get(),
199+
&inter_process_non_local_subscription_count);
200+
201+
if (RCL_RET_PUBLISHER_INVALID == status) {
202+
rcl_reset_error(); /* next call will reset error message if not context */
203+
if (rcl_publisher_is_valid_except_context(publisher_handle_.get())) {
204+
rcl_context_t * context = rcl_publisher_get_context(publisher_handle_.get());
205+
if (nullptr != context && !rcl_context_is_valid(context)) {
206+
/* publisher is invalid due to context being shutdown */
207+
return 0;
208+
}
209+
}
210+
}
211+
if (RCL_RET_OK != status) {
212+
rclcpp::exceptions::throw_from_rcl_error(status, "failed to get get non local subscription count");
213+
}
214+
return inter_process_non_local_subscription_count;
215+
}
216+
192217
size_t
193218
PublisherBase::get_intra_process_subscription_count() const
194219
{

0 commit comments

Comments
 (0)