From 1962306aa67e1007490a6e17c2fa579cc55c89c6 Mon Sep 17 00:00:00 2001 From: JLBuenoLopez-eProsima Date: Tue, 6 Jun 2023 11:33:54 +0200 Subject: [PATCH 1/2] Add `get_non_local_subscription_count` to PublisherBase Signed-off-by: JLBuenoLopez-eProsima --- rclcpp/include/rclcpp/publisher_base.hpp | 6 ++++++ rclcpp/src/rclcpp/publisher_base.cpp | 27 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/rclcpp/include/rclcpp/publisher_base.hpp b/rclcpp/include/rclcpp/publisher_base.hpp index 3253595361..45c21ce3eb 100644 --- a/rclcpp/include/rclcpp/publisher_base.hpp +++ b/rclcpp/include/rclcpp/publisher_base.hpp @@ -133,6 +133,12 @@ class PublisherBase : public std::enable_shared_from_this size_t get_subscription_count() const; + /// Get non local subscription count + /** \return The number of non local subscriptions. */ + RCLCPP_PUBLIC + size_t + get_non_local_subscription_count() const; + /// Get intraprocess subscription count /** \return The number of intraprocess subscriptions. */ RCLCPP_PUBLIC diff --git a/rclcpp/src/rclcpp/publisher_base.cpp b/rclcpp/src/rclcpp/publisher_base.cpp index 0e37dcf3fa..a14796a058 100644 --- a/rclcpp/src/rclcpp/publisher_base.cpp +++ b/rclcpp/src/rclcpp/publisher_base.cpp @@ -253,6 +253,33 @@ PublisherBase::get_subscription_count() const return inter_process_subscription_count; } +size_t +PublisherBase::get_non_local_subscription_count() const +{ + size_t inter_process_non_local_subscription_count = 0; + + rcl_ret_t status = rcl_publisher_get_non_local_subscription_count( + publisher_handle_.get(), + &inter_process_non_local_subscription_count); + + if (RCL_RET_PUBLISHER_INVALID == status) { + rcl_reset_error(); /* next call will reset error message if not context */ + if (rcl_publisher_is_valid_except_context(publisher_handle_.get())) { + rcl_context_t * context = rcl_publisher_get_context(publisher_handle_.get()); + if (nullptr != context && !rcl_context_is_valid(context)) { + /* publisher is invalid due to context being shutdown */ + return 0; + } + } + } + if (RCL_RET_OK != status) { + rclcpp::exceptions::throw_from_rcl_error( + status, + "failed to get get non local subscription count"); + } + return inter_process_non_local_subscription_count; +} + size_t PublisherBase::get_intra_process_subscription_count() const { From 2679eca15c5638f31808c1f5fde1dcbe01c91433 Mon Sep 17 00:00:00 2001 From: JLBuenoLopez-eProsima Date: Tue, 6 Jun 2023 11:36:36 +0200 Subject: [PATCH 2/2] Made inter-process publish depend on `get_non_local_subscription_count` Signed-off-by: JLBuenoLopez-eProsima --- rclcpp/include/rclcpp/publisher.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rclcpp/include/rclcpp/publisher.hpp b/rclcpp/include/rclcpp/publisher.hpp index 18229c7a4e..96feeca54f 100644 --- a/rclcpp/include/rclcpp/publisher.hpp +++ b/rclcpp/include/rclcpp/publisher.hpp @@ -237,7 +237,7 @@ class Publisher : public PublisherBase // It's not possible to do that with an unique_ptr, // as do_intra_process_publish takes the ownership of the message. bool inter_process_publish_needed = - get_subscription_count() > get_intra_process_subscription_count(); + get_non_local_subscription_count() > 0; if (inter_process_publish_needed) { auto shared_msg = @@ -306,7 +306,7 @@ class Publisher : public PublisherBase } bool inter_process_publish_needed = - get_subscription_count() > get_intra_process_subscription_count(); + get_non_local_subscription_count() > 0; if (inter_process_publish_needed) { ROSMessageType ros_msg;