File tree Expand file tree Collapse file tree 3 files changed +33
-2
lines changed Expand file tree Collapse file tree 3 files changed +33
-2
lines changed Original file line number Diff line number Diff 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>();
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+
192217size_t
193218PublisherBase::get_intra_process_subscription_count () const
194219{
You can’t perform that action at this time.
0 commit comments