15 #ifndef RCLCPP__GENERIC_CLIENT_HPP_
16 #define RCLCPP__GENERIC_CLIENT_HPP_
28 #include "rclcpp/client.hpp"
29 #include "rclcpp/visibility_control.hpp"
30 #include "rcpputils/shared_library.hpp"
32 #include "rosidl_typesupport_introspection_cpp/message_introspection.hpp"
39 using Request =
void *;
40 using Response =
void *;
42 using SharedResponse = std::shared_ptr<void>;
44 using Promise = std::promise<SharedResponse>;
45 using SharedPromise = std::shared_ptr<Promise>;
47 using Future = std::future<SharedResponse>;
48 using SharedFuture = std::shared_future<SharedResponse>;
50 using CallbackType = std::function<void (SharedFuture)>;
68 SharedFuture
share() noexcept {
return this->future.share();}
98 rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph,
99 const std::string & service_name,
100 const std::string & service_type,
105 create_response()
override;
108 std::shared_ptr<rmw_request_id_t>
109 create_request_header()
override;
114 std::shared_ptr<rmw_request_id_t> request_header,
115 std::shared_ptr<void> response)
override;
167 typename std::enable_if<
178 auto shared_future = promise.get_future().share();
179 auto req_id = async_send_request_impl(
182 CallbackType{std::forward<CallbackT>(cb)},
184 std::move(promise)));
195 template<
typename AllocatorT = std::allocator<
int64_t>>
198 std::chrono::time_point<std::chrono::system_clock> time_point,
199 std::vector<int64_t, AllocatorT> * pruned_requests =
nullptr)
201 return detail::prune_requests_older_than_impl(
203 pending_requests_mutex_,
241 const FutureAndRequestId & future);
252 const SharedFutureAndRequestId & future);
275 using CallbackTypeValueVariant = std::tuple<CallbackType, SharedFuture, Promise>;
276 using CallbackInfoVariant = std::variant<
277 std::promise<SharedResponse>,
278 CallbackTypeValueVariant>;
282 async_send_request_impl(
283 const Request request,
284 CallbackInfoVariant value);
286 std::optional<CallbackInfoVariant>
287 get_and_erase_pending_request(
288 int64_t request_number);
292 std::map<int64_t, std::pair<
293 std::chrono::time_point<std::chrono::system_clock>,
294 CallbackInfoVariant>> pending_requests_;
295 std::mutex pending_requests_mutex_;
298 std::shared_ptr<rcpputils::SharedLibrary> ts_lib_;
299 const rosidl_typesupport_introspection_cpp::MessageMembers * response_members_;
RCLCPP_PUBLIC bool take_type_erased_response(void *response_out, rmw_request_id_t &request_header_out)
Take the next response for this client as a type erased pointer.
RCLCPP_PUBLIC size_t prune_pending_requests()
Clean all pending requests.
RCLCPP_PUBLIC FutureAndRequestId async_send_request(const Request request)
Send a request to the service server.
RCLCPP_PUBLIC bool remove_pending_request(int64_t request_id)
Cleanup a pending request.
RCLCPP_PUBLIC bool take_response(Response response_out, rmw_request_id_t &request_header_out)
Take the next response for this client.
SharedFutureAndRequestId async_send_request(const Request request, CallbackT &&cb)
Send a request to the service server and schedule a callback in the executor.
size_t prune_requests_older_than(std::chrono::time_point< std::chrono::system_clock > time_point, std::vector< int64_t, AllocatorT > *pruned_requests=nullptr)
Clean all pending requests older than a time_point.
Pure virtual interface class for the NodeBase part of the Node API.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Options available for a rcl_client_t.
A convenient GenericClient::Future and request id pair.
~FutureAndRequestId()=default
Destructor.
FutureAndRequestId(FutureAndRequestId &&other) noexcept=default
Move constructor.
FutureAndRequestId(const FutureAndRequestId &other)=delete
Deleted copy constructor, each instance is a unique owner of the future.
FutureAndRequestId & operator=(const FutureAndRequestId &other)=delete
Deleted copy assignment, each instance is a unique owner of the future.
SharedFuture share() noexcept
See std::future::share().
FutureAndRequestId & operator=(FutureAndRequestId &&other) noexcept=default
Move assignment.
A convenient GenericClient::SharedFuture and request id pair.