rclcpp
master
C++ ROS Client Library API
|
Go to the documentation of this file.
15 #ifndef RCLCPP__SERVICE_HPP_
16 #define RCLCPP__SERVICE_HPP_
25 #include "rcl/error_handling.h"
26 #include "rcl/service.h"
37 #include "tracetools/tracetools.h"
143 template<
typename ServiceT>
172 const
std::
string & service_name,
175 :
ServiceBase(node_handle), any_callback_(any_callback)
177 using rosidl_typesupport_cpp::get_service_type_support_handle;
178 auto service_type_support_handle = get_service_type_support_handle<ServiceT>();
185 auto handle = weak_node_handle.
lock();
187 if (rcl_service_fini(service, handle.get()) != RCL_RET_OK) {
189 rclcpp::get_node_logger(handle.get()).get_child(
"rclcpp"),
190 "Error in destruction of rcl service handle: %s",
191 rcl_get_error_string().str);
197 "Error in destruction of rcl service handle: "
198 "the Node Handle was destructed too early. You will leak memory");
204 rcl_ret_t ret = rcl_service_init(
207 service_type_support_handle,
208 service_name.c_str(),
210 if (ret != RCL_RET_OK) {
211 if (ret == RCL_RET_SERVICE_NAME_INVALID) {
217 rcl_node_get_name(rcl_node_handle),
218 rcl_node_get_namespace(rcl_node_handle),
225 rclcpp_service_callback_added,
227 (
const void *)&any_callback_);
228 #ifndef TRACETOOLS_DISABLED
229 any_callback_.register_callback_for_tracing();
248 any_callback_(any_callback)
251 if (!rcl_service_is_valid(service_handle.
get())) {
254 std::string(
"rcl_service_t in constructor argument must be initialized beforehand."));
260 rclcpp_service_callback_added,
262 (
const void *)&any_callback_);
263 #ifndef TRACETOOLS_DISABLED
264 any_callback_.register_callback_for_tracing();
283 any_callback_(any_callback)
286 if (!rcl_service_is_valid(service_handle)) {
289 std::string(
"rcl_service_t in constructor argument must be initialized beforehand."));
297 rclcpp_service_callback_added,
299 (
const void *)&any_callback_);
300 #ifndef TRACETOOLS_DISABLED
301 any_callback_.register_callback_for_tracing();
332 return std::make_shared<typename ServiceT::Request>();
338 return std::make_shared<rmw_request_id_t>();
346 auto typed_request = std::static_pointer_cast<typename ServiceT::Request>(request);
347 auto response = std::make_shared<typename ServiceT::Response>();
348 any_callback_.dispatch(request_header, typed_request, response);
352 [[deprecated(
"use the send_response() which takes references instead of shared pointers")]]
366 if (ret != RCL_RET_OK) {
379 #endif // RCLCPP__SERVICE_HPP_
virtual void handle_request(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request)=0
Definition: any_service_callback.hpp:33
struct rcl_service_impl_t * impl
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
bool owns_rcl_handle_
Definition: service.hpp:138
Definition: service.hpp:144
Logger get_logger(const std::string &name)
Return a named logger.
void send_response(rmw_request_id_t &req_id, typename ServiceT::Response &response)
Definition: service.hpp:362
Service(std::shared_ptr< rcl_node_t > node_handle, std::shared_ptr< rcl_service_t > service_handle, AnyServiceCallback< ServiceT > any_callback)
Default constructor.
Definition: service.hpp:243
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
std::shared_ptr< rcl_service_t > service_handle_
Definition: service.hpp:137
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
std::shared_ptr< void > create_request() override
Definition: service.hpp:330
virtual std::shared_ptr< rmw_request_id_t > create_request_header()=0
virtual ~Service()
Definition: service.hpp:307
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
const char * get_service_name()
Return the name of the service.
std::string expand_topic_or_service_name(const std::string &name, const std::string &node_name, const std::string &namespace_, bool is_service=false)
Expand a topic or service name and throw if it is not valid.
ServiceBase(std::shared_ptr< rcl_node_t > node_handle)
bool exchange_in_use_by_wait_set_state(bool in_use_state)
Exchange the "in use by wait set" state for this service.
Service(std::shared_ptr< rcl_node_t > node_handle, rcl_service_t *service_handle, AnyServiceCallback< ServiceT > any_callback)
Default constructor.
Definition: service.hpp:278
std::shared_ptr< rcl_service_t > get_service_handle()
Return the rcl_service_t service handle in a std::shared_ptr.
void handle_request(std::shared_ptr< rmw_request_id_t > request_header, std::shared_ptr< void > request) override
Definition: service.hpp:342
virtual std::shared_ptr< void > create_request()=0
void send_response(std::shared_ptr< rmw_request_id_t > req_id, std::shared_ptr< typename ServiceT::Response > response)
Definition: service.hpp:354
std::atomic< bool > in_use_by_wait_set_
Definition: service.hpp:140
std::shared_ptr< rcl_node_t > node_handle_
Definition: service.hpp:135
rcl_node_t * get_rcl_node_handle()
std::shared_ptr< rmw_request_id_t > create_request_header() override
Definition: service.hpp:336
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
void throw_from_rcl_error(rcl_ret_t ret, const std::string &prefix="", const rcl_error_state_t *error_state=nullptr, void(*reset_error)()=rcl_reset_error)
Throw a C++ std::exception which was created based on an rcl error.
Definition: service.hpp:42
bool take_type_erased_request(void *request_out, rmw_request_id_t &request_id_out)
Take the next request from the service as a type erased pointer.
#define RCLCPP_ERROR(logger,...)
Definition: logging.hpp:1419
bool take_request(typename ServiceT::Request &request_out, rmw_request_id_t &request_id_out)
Take the next request from the service.
Definition: service.hpp:324