@@ -292,8 +292,6 @@ class ServiceBase
292292 uint64_t intra_process_service_id,
293293 IntraProcessManagerWeakPtr weak_ipm);
294294
295- std::shared_ptr<rclcpp::experimental::ServiceIntraProcessBase> service_intra_process_;
296-
297295 std::shared_ptr<rcl_node_t > node_handle_;
298296 std::shared_ptr<rclcpp::Context> context_;
299297
@@ -345,14 +343,12 @@ class Service
345343 * \param[in] service_name Name of the topic to publish to.
346344 * \param[in] any_callback User defined callback to call when a client request is received.
347345 * \param[in] service_options options for the subscription.
348- * \param[in] ipc_setting Intra-process communication setting for the service.
349346 */
350347 Service (
351348 std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base,
352349 const std::string & service_name,
353350 AnyServiceCallback<ServiceT> any_callback,
354- rcl_service_options_t & service_options,
355- rclcpp::IntraProcessSetting ipc_setting = rclcpp::IntraProcessSetting::NodeDefault)
351+ rcl_service_options_t & service_options)
356352 : ServiceBase(node_base), any_callback_(any_callback),
357353 srv_type_support_handle_ (rosidl_typesupport_cpp::get_service_type_support_handle<ServiceT>())
358354 {
@@ -398,7 +394,14 @@ class Service
398394#ifndef TRACETOOLS_DISABLED
399395 any_callback_.register_callback_for_tracing ();
400396#endif
397+ // Setup continues in the post construction method, post_init_setup().
398+ }
401399
400+ // / Called post construction, so that construction may continue after shared_from_this() works.
401+ void post_init_setup (
402+ std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base,
403+ rclcpp::IntraProcessSetting ipc_setting = rclcpp::IntraProcessSetting::NodeDefault)
404+ {
402405 // Setup intra process if requested.
403406 if (rclcpp::detail::resolve_use_intra_process (ipc_setting, *node_base)) {
404407 create_intra_process_service ();
@@ -414,13 +417,11 @@ class Service
414417 * \param[in] node_base NodeBaseInterface pointer that is used in part of the setup.
415418 * \param[in] service_handle service handle.
416419 * \param[in] any_callback User defined callback to call when a client request is received.
417- * \param[in] ipc_setting Intra-process communication setting for the service.
418420 */
419421 Service (
420422 std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base,
421423 std::shared_ptr<rcl_service_t > service_handle,
422- AnyServiceCallback<ServiceT> any_callback,
423- rclcpp::IntraProcessSetting ipc_setting = rclcpp::IntraProcessSetting::NodeDefault)
424+ AnyServiceCallback<ServiceT> any_callback)
424425 : ServiceBase(node_base), any_callback_(any_callback),
425426 srv_type_support_handle_(rosidl_typesupport_cpp::get_service_type_support_handle<ServiceT>())
426427 {
@@ -440,11 +441,7 @@ class Service
440441#ifndef TRACETOOLS_DISABLED
441442 any_callback_.register_callback_for_tracing ();
442443#endif
443-
444- // Setup intra process if requested.
445- if (rclcpp::detail::resolve_use_intra_process (ipc_setting, *node_base)) {
446- create_intra_process_service ();
447- }
444+ // Setup continues in the post construction method, post_init_setup().
448445 }
449446
450447 // / Default constructor.
@@ -456,13 +453,11 @@ class Service
456453 * \param[in] node_base NodeBaseInterface pointer that is used in part of the setup.
457454 * \param[in] service_handle service handle.
458455 * \param[in] any_callback User defined callback to call when a client request is received.
459- * \param[in] ipc_setting Intra-process communication setting for the service.
460456 */
461457 Service (
462458 std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface> node_base,
463459 rcl_service_t * service_handle,
464- AnyServiceCallback<ServiceT> any_callback,
465- rclcpp::IntraProcessSetting ipc_setting = rclcpp::IntraProcessSetting::NodeDefault)
460+ AnyServiceCallback<ServiceT> any_callback)
466461 : ServiceBase(node_base), any_callback_(any_callback),
467462 srv_type_support_handle_(rosidl_typesupport_cpp::get_service_type_support_handle<ServiceT>())
468463 {
@@ -484,10 +479,7 @@ class Service
484479#ifndef TRACETOOLS_DISABLED
485480 any_callback_.register_callback_for_tracing ();
486481#endif
487- // Setup intra process if requested.
488- if (rclcpp::detail::resolve_use_intra_process (ipc_setting, *node_base)) {
489- create_intra_process_service ();
490- }
482+ // Setup continues in the post construction method, post_init_setup().
491483 }
492484
493485 Service () = delete;
@@ -553,6 +545,14 @@ class Service
553545 void
554546 send_response (rmw_request_id_t & req_id, typename ServiceT::Response & response)
555547 {
548+ if (use_intra_process_)
549+ {
550+ auto intra_response = std::make_shared<typename ServiceT::Response>(response);
551+ // The sequence number here is used as a proxy for the intra-process client ID
552+ service_intra_process_->send_response (req_id.sequence_number , intra_response);
553+ return ;
554+ }
555+
556556 rcl_ret_t ret = rcl_send_response (get_service_handle ().get (), &req_id, &response);
557557
558558 if (ret == RCL_RET_TIMEOUT) {
@@ -591,6 +591,7 @@ class Service
591591 using ServiceIntraProcessT = rclcpp::experimental::ServiceIntraProcess<ServiceT>;
592592
593593 service_intra_process_ = std::make_shared<ServiceIntraProcessT>(
594+ this ->shared_from_this (),
594595 any_callback_,
595596 context_,
596597 this ->get_service_name (),
@@ -635,6 +636,12 @@ class Service
635636 AnyServiceCallback<ServiceT> any_callback_;
636637
637638 const rosidl_service_type_support_t * srv_type_support_handle_;
639+
640+ // In order to mirror the send_response signature with a SharedResponse
641+ // of the appropriate ServiceT type, the template class is stored
642+ // as opposed to the base class which has no knowledge of ServiceT.
643+ std::shared_ptr<rclcpp::experimental::ServiceIntraProcess<ServiceT>> service_intra_process_;
644+
638645};
639646
640647} // namespace rclcpp
0 commit comments