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 @@ -237,7 +237,7 @@ class Publisher : public PublisherBase
237237 // It's not possible to do that with an unique_ptr,
238238 // as do_intra_process_publish takes the ownership of the message.
239239 bool inter_process_publish_needed =
240- get_subscription_count () > get_intra_process_subscription_count () ;
240+ get_non_local_subscription_count () > 0 ;
241241
242242 if (inter_process_publish_needed) {
243243 auto shared_msg =
@@ -306,7 +306,7 @@ class Publisher : public PublisherBase
306306 }
307307
308308 bool inter_process_publish_needed =
309- get_subscription_count () > get_intra_process_subscription_count () ;
309+ get_non_local_subscription_count () > 0 ;
310310
311311 if (inter_process_publish_needed) {
312312 ROSMessageType ros_msg;
Original file line number Diff line number Diff line change @@ -133,6 +133,12 @@ class PublisherBase : public std::enable_shared_from_this<PublisherBase>
133133 size_t
134134 get_subscription_count () const ;
135135
136+ // / Get non local subscription count
137+ /* * \return The number of non local subscriptions. */
138+ RCLCPP_PUBLIC
139+ size_t
140+ get_non_local_subscription_count () const ;
141+
136142 // / Get intraprocess subscription count
137143 /* * \return The number of intraprocess subscriptions. */
138144 RCLCPP_PUBLIC
Original file line number Diff line number Diff line change @@ -253,6 +253,31 @@ PublisherBase::get_subscription_count() const
253253 return inter_process_subscription_count;
254254}
255255
256+ size_t
257+ PublisherBase::get_non_local_subscription_count () const
258+ {
259+ size_t inter_process_non_local_subscription_count = 0 ;
260+
261+ rcl_ret_t status = rcl_publisher_get_non_local_subscription_count (
262+ publisher_handle_.get (),
263+ &inter_process_non_local_subscription_count);
264+
265+ if (RCL_RET_PUBLISHER_INVALID == status) {
266+ rcl_reset_error (); /* next call will reset error message if not context */
267+ if (rcl_publisher_is_valid_except_context (publisher_handle_.get ())) {
268+ rcl_context_t * context = rcl_publisher_get_context (publisher_handle_.get ());
269+ if (nullptr != context && !rcl_context_is_valid (context)) {
270+ /* publisher is invalid due to context being shutdown */
271+ return 0 ;
272+ }
273+ }
274+ }
275+ if (RCL_RET_OK != status) {
276+ rclcpp::exceptions::throw_from_rcl_error (status, " failed to get get non local subscription count" );
277+ }
278+ return inter_process_non_local_subscription_count;
279+ }
280+
256281size_t
257282PublisherBase::get_intra_process_subscription_count () const
258283{
You can’t perform that action at this time.
0 commit comments